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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# include <iostream>
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.

Thanks for the quick reply.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Exercise 7.15 Page 320
#include <iostream>
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
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.
Last edited on
That did the trick. Thanks. Now if I can just stop overthinking the assignments.
Topic archived. No new replies allowed.