### One-dimensional array output problem.

This is a class assignment from the book "C++ How to Program 8/E" by Deitel. Problem 7.15

Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it isn’t a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

It will compile fine, run fine, give all the proper errors if you enter the wrong number etc. The problem is with the output. It will output the unique values but then it continues to output garbage until in crashes Microsoft Visual Studio 2010.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````# include using namespace std; int main(void) { const int SIZE = 20; // size of array int a[SIZE] = { 0 }; int subscript = 0; int duplicate; int value; // number entered by user cout << "Enter 20 numbers between 10 and 100:\n"; // get 20 nonduplicate numbers in the range between 10 and 100 for ( int i = 0; i < SIZE;) { duplicate = 0; cin >> value; // validate input and test if tehre is a duplicate if ( value >= 10 && value <= 100 ) { for ( int j = 0; j < subscript; j++ ) { if ( value == a[ j ] ) { duplicate = 1; break; } // end if } // end for // if number is not a duplicate enter it in array if ( !duplicate ) { a[subscript++ ] = value; i++; } // end if else cout << "Duplicate number.\n"; } // end if else cout << "Invalid number.\n"; } // end for cout << "\nthe nonduplicate values are:\n"; // display array of nonduplicates for ( int i = 0; 1 < SIZE; i++ ) cout << a[ i ] << ", "; cout << endl; return 0; } ``````

Any ideas? Sorry but I don't know how to include the output into this forum post.
The for loop on line 49 will run forever because `1 < SIZE` is always true. Did you mean `i < SIZE`?
That was it. I changed the "1" to an "i" and it works great.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````// Exercise 7.15 Page 320 #include using namespace std; int main( void ) { const int MAX = 20; int a[ MAX ] = { 0 }; // array for user input int i; // loop counter int j; // loop counter int k = 0; // number of values currently entered int duplicate; // flag for duplicate values int value; // current value cout << "Enter 20 numbers between 10 and 100.\n"; // get 20 numbers from user for ( i = 0; i <= MAX - 1; i++ ) { duplicate = 0; cin >> value; // validate and test if number is a duplicate if ( value >= 10 && value <= 100 ) { for ( j = 0; j < k; j++ ) { // if duplicate, raise flag and break loop if ( value == a[ j ] ) { duplicate = 1; cout << "Duplicate number.\n"; break; } // end raise flag } // end for // if number is not a duplicate, enter it in array if ( !duplicate ) { a[ k++ ] = value; } // end if - not duplicate } // end if - validate and test else cout << "invalid number.\n"; } // end for - get 20 numbers cout << "\nThe non-duplicate values are:\n"; // display array of nonduplicates for ( i = 0; a[ i ] != 0; i++ ) { cout << a[ i ] << ", "; } // end for - display array cout << "\n"; return 0; // indicate successful termination } // end main ``````
I suggest adding `&& i < MAX` to the condition.