cin with arrays and finding distinct elements

Here is the prompt for my assignment.

Read a series of numbers into an integer array, until -1 is entered. The input will have at least one number (not counting -1) and may consist of up to 100 numbers. Print to the screen all distinct numbers in the array, sequentially. In other words, every number that appears in the array will be printed to the screen exactly once, in order by its first appearance, even if it appears multiple times in the array.

ex. Enter integer values into the array, or -1 to stop:
3
5
2
5
3
1
4
4
2
-1

Distinct values in the array:
(3,5,2,1,4)

Im getting a compile error for line 17 on the scope of x in nums[x] but im not sure why.

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 <iomanip>
using namespace std;

int main(){
	
	int size = 0;
	int nums[size];
	int input = 0;
	int z = 0;
	cout << "Enter integer values into the array, or -1 to stop:";
	
	while(input != 1){
		for(int x = 0; x < 100; x++)
		cin >> nums[x];
		size++;
		input = nums[x];
	}
	
	for(int j = 0; j < size; j++){
		for(z = 0; z < j; z++){
			if(nums[j] == nums [z])
			break;
			if(j == z)
			cout << nums[j] << " ";
			
		}
	}
	
	return 0;
}
Last edited on
1
2
int size = 0;
int nums[size];

That is not legal in C++. The size of array must be a constant value that is known already before the code is compiled.

The constness aside, how many elements are in array int nums[0];?
How many is ?

The input may consist of up to 100 numbers.

You need room for at most 100 numbers. Make it so:
1
2
constexpr int MAX = 100;
int nums[MAX];


until -1 is entered
while(input != 1)

Do you have a typo?

That is not all in:
1
2
3
4
5
6
7
while ( input != 1 ) {
  for ( int x = 0; x < 100; x++ )
    cin >> nums[x];

  size++;
  input = nums[x];
}

That code does not compile. Read your compiler's error messages carefully.

You do repeat the
1
2
3
4
5
  for ( int x = 0; x < 100; x++ ) {
    cin >> nums[x];
  }
  size++;
  input = nums[x]; // error: x is unknown 

until input becomes 1. On every iteration you do read 100 values (to array that has no room).

Test input before you use it:
1
2
3
4
5
6
int size = 0;
int input = 0;
while ( size < MAX && std::cin >> input && input != -1 ) {
  // never store the sentinel into the array
  nums[size++] = input;
}



One way to skip duplicates is to mark them unusable.
We do know that the array won't have any -1.

FOR EACH candidate in nums
  IF candidate is good
  THEN
    show candidate
    mark duplicates bad in the rest of the nums
Last edited on
I reconstructed taking in my integer array with the constant 100 and that works and cuts off when -1 enters but now im struggling to pull and print te distinct elements heres what i have. When i run it nothing prints so i assume i've made an error in the loops.

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

int main(){

	int array[100];
    int num;
    int size = 0;
	
	cout << "Enter integer values into the array, or -1 to stop:";
    
	for(int i = 0; i < 100; i++)
    {
        cin >> num;
        if(num == -1)
        {
            break;
        }
        array[i] = num;
        size++;
    }
    
    for(int j = 0; j < size; j++)
	{
		for(int z = 0; z < j; z++){
			if(array[j] == array[z])
			break;
			if(j == z)
			cout << array[j] << " ";
			
		}
	}
	
}
Last edited on
while(input != 1){

that looks like it should be -1, to stop on -1 right?
it current will stop the loop on 1 (positive 1)

are the numbers 0-9? if so, this is one approach that keeps it simple. Its not complete
...
1
2
3
4
5
6
7
8
9
10
11
12
unsigned int smarty[10] = {0};
do   
{
cin >> input;
if(input != -1)
smarty[input]++; 
}
while(input  != -1)
for(int i = 0; i < 10; i++)
 if(smarty[i])
   cout << i << endl;
Last edited on
the most recent code i posted takes the array in the way i want it to i believe but its the finding and printing distinct elements.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

	int array[100];
    int num;
    int size = 0;
	
	cout << "Enter integer values into the array, or -1 to stop:";
    
	for(int i = 0; i < 100; i++)
    {
        cin >> num;
        if(num == -1)
        {
            break;
        }
        array[i] = num;
        size++;
    }
    cout << array[0] << " ";
    for(int j = 0; j < size; j++)
	{
		for(int z = 0; z <= j; z++){
			if(array[j] == array[z])
			break;
			
			if(z+1 == j)
			cout << array[j] << " ";
			
		}
	}
	
}
yes. you need to sort the input, and then once it is sorted your assumption that you can look at the (current and next or current and previous as you see fit) to find duplicates will work. But I don't see any sorting code in there, so that assumption isnt true.

what I tried to show you 1)sorts it 2) counts the duplicates (not used) 3) can print the original (not shown) 4) can print without duplicates (shown).

Last edited on
thanks everybody.
Topic archived. No new replies allowed.