arrays

Hello,

I'm trying to do a homework assignment for my C++ class but I can't figure it out. We're supposed to write a program that asks user to enter 10 numbers then removes duplicates and displays the new numbers.

I can get it to ask for the 10 numbers and then display them back but I cannot get it to get rid of any numbers.

I am thinking I need to have a function loop that checks a number against the previous numbers and then displays it if its unique but i don't know how to code that.

So far this is my code.
1
2
3
4
5
6
7
8
9
10
11
const int SIZE = 9;
int a[SIZE];
cout << "Please enter 10 numbers. " << endl;

for (int i=0; i <= SIZE; i++)
  cin >> a[i];

cout << "You entered: ";
for(int i=0; i <= SIZE; i++)
 cout << a[i] << "\t";
cout << endl;


Any guidance would be much appreciated.
Cheers
I would suggest creating a function that iterates and finds the duplicates, and then creating a function that calls the aforementioned function and removes the numbers. This is an example of a search function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int unorderedListType::seqSearch(int item) const
{
	int loc = 0;
	bool found = false;
	for(loc = 0; loc < length; loc++)
		if(list[loc] == item)
		{
			found = true;
			break;
		}
    if(found)
		return loc;
	else
		return -1;
}
thank you for your fast response. However, I think that might be beyond the level of our class. most of that just went right over my head...
Try creating a loop that runs through the array with a bool variable, if the item is found, set it to true and then remove it or set it to null.
Hi! I'm trying to learn C++ too!
Here's a suggestion, though it might not be the most efficient method... but it utilizes what I know so far..
(which isn't very much =P). It might be buggy too.. but seems to work so far =P


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
 int main()
{
	const int SIZE = 10;
	int a[SIZE];
	int b[SIZE];
	cout << "Please enter 10 numbers. " << endl;
	
	int i;
	int j;
	int k = 0;
	int sum;
	for (i = 0; i < SIZE; i++)
		cin >> a[i];                                    // Read 10 numbers into a[i] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (i = 0; i < SIZE; i++)
{
sum = 0;
for (j = 0; j < SIZE; j++)    
{
if (!(a[i] == a[j]))     
{
sum++;
if (sum == 9)
{ 
b[k] = a[i];  
k++;
}	
}	
}	
}

1
2
3
4
	cout << "Entered: \n";
	for (i = 0; i < k; i++)   k
		cout << b[i] << " " << endl;		
}


Apologies about truncating the code, the website didn't allow me to put the entire code in.

Hope it helps! Look forward to advice on how to improve/make the code more efficient or better ideas!

Cheers!
Unicyclist
I am thinking I need to have a function loop that checks a number against the previous numbers and then displays it if its unique but i don't know how to code that.


That will work.


I need to have a function loop...


 
for( int index = 0; index < num-items-in-list; ++index )



that checks a number against the previous numbers


Or, in other words, loops through all the elements you've already passed and sees
if any of those elements are the same value as the one you're looking at. If it
finds the element, it should set a boolean "unique" flag to false, otherwise it sets it
to true.


and then displays it if its unique


1
2
if( unique == true )
    // output it. 

Topic archived. No new replies allowed.