array problem

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.
I understand the logical step:
1.ask user input value
2.check value in the range
3.check value repeat or not
4.display only unique value
I try the small size array and see how it works
the difficult of this problem is remove the duplicate number.
I try at least 2 nights, hopefully someone can help me

my attempt: let the input in the range as show below
but compiler gives me some wire numbers after I run.

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
 void Dea::Input()
{
	cout<<"Enter any number between 10 and 100, 4 times \n";
 do{

	  cin>> input;//user input
           //check input the range
        if (10>=input && input <=100)
         a[count]=input;
            ++count;


	} while(count<4);



}


void Dea::display()
{

	for(int count=0;count<4;count++)
	 	 cout<<a[count]<<" \n";
}
Each time you input something, loop through the array and see if it matches anything thats already there.

1
2
3
4
5
6
7
8
9
10
11
12
int arr[5] = {2,4,5,6,7};

int x;

cout << "Enter Number: ";
cin >> x;

for(int i = 0; i < 5; i++) // i < 5 because array size is 5
{
    if(x != arr[i]) // if the inputted number is not equal to a number inside the array
      // store it in the array
}


Hope this gives you a good idea
ok, I need another loop for checking repeat.
The first part, I constrain the input value in a range, but when I run the code and type some values, the display function shows other irrelevant values....
Last edited on
closed account (D80DSL3A)
The first part, I constrain the input value in a range...

Yes you do, but that range is input < 10. Line 8 condition is incorrect.
If you are entering numbers 10-100 then none are being recorded in the (must be global) array.
count gets incremented anyways since it isn't in the if statement body. Use {} to contain 2 or more statements.
Last edited on
Thanks for pointing that out
do some update but still not solve checking repeating value inside the loop
In addition, I don't store the wrong value in array, but in the display doesn't satisfy the question requirement, since the element of wrong value inside array is 0 as it run.

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
void Dea::Input()
{
	cout<<"Enter any number between 10 and 100, 4 times \n";
	 do{

		  cin>> input;//user input
	           //check input the range
	        if (input>9 && input <101)
	           {
	        	//a loop for check, I don't know how to do in here
                      // I want to use loop check without include any library

	        	a[count]=input;


	           }

	        else  // not right to store wrong value
	        	a[count]=!input;


	            ++count;


		} while(count<4);






	}



}


void Dea::display()
{

	for(int count=0;count<4;count++)
	 	 cout<<a[count]<<" \n";
}


Last edited on
closed account (D80DSL3A)
You can use a for loop to check if the value just entered already exists in the array.
One way:
1
2
3
4
5
6
7
8
9
int i;// declared outside of for loop because i will be needed after the loop
for(i=0; i<uniqueEntries; ++i)
    if( input == a[i] ) break;// exit loop if match found

if( i == uniqueEntries )// for loop went all the way. No match was found.
{
    a[uniqueEntries]=input;// record the entry
    ++uniqueEntries;
}            

Your solution is off course in another way though. 20 entries are to be made, but some may be duplicates of earlier entries. You should keep track of the totalEntries made (goes to 20) and the number of uniqueEntries made (<=20). This is why I used uniqueEntries in the above code. It should be incremented only after a unique entry has been made. totalEntries would be incremented every time an entry is made, like you're using count now.

uniqueEntries will be the upper limit when you loop in the display function.
update, for numbers no in the range, what do I deal with them. Code has some problems,please point out .
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
65
66

Dea::Dea()
{
	count=0;//initialize as 0
	input=0;//initialize as 0
    count=0;//initialize as 0
   unique=0;//
   repeat=0;
    i=0;
}


void Dea::Input()
{
	cout<<"Enter any number between 10 and 100, 4 times \n";
	 for(int count=0;count<4;count++)
	 {
		  repeat=0;
		  cin>> input;//user input
	           //check input the range
	        if (input>9 && input <101)
	           {

	        	//a loop for check
	        	for(int i=0;i<count;i++)
	        	{   //repeat not go inside
	        		if(input==a[i])
	        	     {
	        			repeat=1;
	        		    break;
	        	       }//end of if
	        	}//end of for

	        	//without repeat pass to array
	        		if(!repeat)
	        		{
	        			a[unique]=input;
	        			++count;
	        		}//end if

	           }//end if




		}






	}






void Dea::display()
{

	for(int j=0;j<count;j++)
	 	 cout<<a[j]<<" \n";
}
closed account (D80DSL3A)
Try substituting unique for count on lines 25, 38 and 63.
Sorry man, doesn't work, counter will go forever
Topic archived. No new replies allowed.