Array HW problem is buggy

"Use a one-dimensional array to solve the following problem. Read in 30 numbers, each of which is between 10 and 100, inclusive, As each m=number is read, validate it and store it int 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 the problem."

It outputs junk, when I want it to output the non-zero table contents.

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
#include <iostream>
using namespace std;

int main()
{
	const int arraySize = 19;
	int n = -1, a[ arraySize ] = {}, prompt(), linearSearch( const int [], int, int ); //Declare variables, array, and function prototypes

	for ( int counter = 1; counter <= arraySize; counter++ ) // Input numbers
	{
		n = prompt();
		if ( linearSearch( a, n, arraySize ) !=-1 ) // Only store if parameters are met
			a[ counter ] = n;
	} 

	cout << "Unique and valid values you entered:" << endl;

	for ( int counter2 = 0; counter2 <= arraySize; counter2++ )
	{
		if ( a[ counter2 ] != 0 ) // Only print element value if non-zero
			cout << a[ counter2 ] << "\t";
	}

	cout << endl << endl;
	system("pause");
}

int prompt ()
		{
			int x = 0;
			do
			{
				cout << "Please enter an integer no greater than 100 and no less than 10. \t";
				cin >> x;
				cout << endl;
			}
			while ( x <=10 || x >= 100 );
			return x;
		}

int linearSearch( const int array[], int key, int size_of_array ) // I copied this right out of the book.
{
	for ( int counter3 = 0; counter3 < size_of_array; counter3++ )
		if ( array[ counter3 ] == key )
			return counter3;
	return -1;
}


Thank you.
The problem statement is faulty. The first line says read in 30 numbers.
The last line states all 20 numbers can be different. Which is it 20 or 30?

Problems with your code:
line 6: If there are 20 numbers that can be different, an array of 19 isn't going to hold them.
line 9: You start counter at 1. a[0] is going to be uninitialized.
line 12: passes arraySize to linearSearch. ArraySize is fixed at 19. LinearSearch gets called as each number is entered, therefore only <counter> cells are valid.
Line 13: You're storing into <a> based on <counter>, not the number of unique values encountered so far. linearSearch returns -1 if the value is unique (not found), but you're storing into a if the value was found (not -1).
line 18: Again assumes exactly 19 values have been entered.
line 20: Assumes a has been initialized to 0, which it has not.
Thanks a lot, AA! I really appreciate the help. I fixed what I could before the submission deadline. Apparently I wasn't supposed to use dynamic allocation (linear search), so I just put another loop in there to do the same thing. Thanks again!
Topic archived. No new replies allowed.