Removing user input duplicates from an array, and displaying results as a new array.

Hello everyone. I am working on a homework assignment for my C++ class where I have to create a one dimensional array that takes in 20 numbers from the user which must be between 10 and 100, and stores them if they are not duplicates. After all 20 numbers have been input, the program must then display a new array with only the unique values. Here is the exact wording of the question:

(Duplicate Elimination with array) 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.

What I have in my program that I have so far is the definition of the array followed by the main which first asks for the user's input. If the input is outside of the range, the program gives an error and stores that input as 0. If the number is within range it will be stored at that spot in the array. After all 20 numbers have been input, I am trying to have the program sort the array, compare each individual number with the one after it, and setting the number to 0 if it is the same as the next number. This is where I keep having the problem. I was going to try to just start a new array with a new variable having it store any non-zero number from the first array, but at this point in the program I keep getting an error when I run it. I think it is saying that I am out of the array's bounds, but I can't figure out how. Any help would be appreciated.

Thanks

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
  // Exercise 7.13

#include <iostream>
#include <array>
#include <algorithm>
using namespace std;

array <int, 20> numbers = {};

int main()
{
	cout << "Enter 20 integers from 10 through 100:" << endl;
	for (int input : numbers)
	{
		for (int i = 0; i <= 19; i++)
		{
			cin >> input;
			if (input < 10 || input > 100)
			{
				cout << "Error: Input must be between 10 and 100" << endl;
				numbers[i] = 0;
			}
			else
				numbers[i] = input;
		}
		sort(numbers.begin(), numbers.end());

		for (int i = 0; i <= 19; i++)
			if (numbers[i] != 0)
				if (numbers[i] = numbers[i + 1])
				{
					numbers[i] = 0;
				}
		sort(numbers.begin(), numbers.end());

	
		for (int i = 0; i <= 19; i++)
			cout << numbers[i] << " ";
	}
	
	
}
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
#include <iostream>
#include <algorithm>
using namespace std;

const int ARRAY_SIZE = 20;

bool findDuplicateNumber(const int numbers[ARRAY_SIZE], int value, int size)
{
	int i;
	for(i = 0; i < size; i++) if(numbers[i] == value) return true; return false;
}

int main()
{
	int i;
	int num;
	int size = 0;
	int numbers[ARRAY_SIZE];

	cout << "Enter 20 integers from 10 through 100 : " << endl;
	for(i = 0; i < 20; i++)
	{
		cout << i + 1 << "." << " Enter an integer : "; 
		if(cin >> num && num >= 10 && num <= 100)
		{
			if(!findDuplicateNumber(numbers, num, size)) numbers[size] = num, size++;
		}
		else
		{
			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');

				cout << "Invalid value.\n\n";
			}
			else
			{
				cout << "Invalid integer range.\n\n";
			}
		}
	}

	cout << endl;
	cout << "The unique values : " << endl;
	for(i = 0; i < size; i++) cout << numbers[i] << endl;
	
	cin.ignore();
	cin.get();
	return 0;
}


Enter 20 integers from 10 through 100 :
1. Enter an integer : 25
2. Enter an integer : 75
3. Enter an integer : 65
4. Enter an integer : 35
5. Enter an integer : 30
6. Enter an integer : 60
7. Enter an integer : 65
8. Enter an integer : 25
9. Enter an integer : 75
10. Enter an integer : 25
11. Enter an integer : 10
12. Enter an integer : 64
13. Enter an integer : 60
14. Enter an integer : 35
15. Enter an integer : 85
16. Enter an integer : 50
17. Enter an integer : 70
18. Enter an integer : 40
19. Enter an integer : 45
20. Enter an integer : 25

The unique values :
25
75
65
35
30
60
10
64
85
50
70
40
45


http://cpp.sh/8mlee
Last edited on
@SakurasouBuster/BoxBird: Try to actually teach the OP instead of simply providing the entire program. You did not even use the same container as the OP. It looks like you just read the problem and wrote the program without even looking at the OP's attempt.
Last edited on
@Arslan7041
This is my reference. So that the OP cannot copy the code that easily.
Here is an option. Sort the array and test the current value with next value.
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
#include <iostream>
#include <algorithm>
#include <array>

using namespace std;

array <int, 10> A = {2, 10, 1, 23, 2, 7, 2, 3, 8, 23};

int main()
{
	sort(A.begin(), A.end());
	int j = 0;

	for (int i = 0; i < A.size(); i++)
	{
		cout << A[i] << " ";
		if (A[j] == A[i]){}

		else {
			    j++;
				A[j] = A[i];
			}
	}
	cout << endl;
	for (int i = 0; i <= j; i++)
	{
		cout << A[i] << " ";
	}

}


NOTE: This code is meant as an example. There are a few flaws in it.
Topic archived. No new replies allowed.