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.
# include <iostream>
usingnamespace std;
int main(void)
{
constint 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.
After further thought, I realized I was looking at the assignment wrong. So I rewrote the code to better address the requirements of the assignment. But I'm still getting errors.
It works fine if I input 20 numbers where 1 or more are out of range or are duplicates. When I input 20 unique numbers that are within range, the output gets messed up. It will output the 20 numbers but then it outputs garbage numbers. I'm sure it's something simple, but I can find it.
// Exercise 7.15 Page 320
#include <iostream>
usingnamespace std;
int main( void )
{
constint 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
If you enter 20 unique values, a will be filled, and have no zero values, making the for loop on line 54 run forever (at least until it finds a 0 in some memory location way out of bounds of a).
I suggest adding && i < MAX to the condition.