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
constint 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;
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;
elsereturn -1;
}
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
int main()
{
constint 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]
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.