Hi guys, I'm hoping someone can help me with this problem. I think I have the code basically figured out, but I have to the output like this
Please enter 10 integers, hitting return after each one:
0 1 2 3 4 5 6 7 8 9
we have to make a code that eliminates duplicates that the user enters, but the user enters the numbers first and that's the output up there. I'm sorry if this sounds confusing I wasn't sure how to word it. My code works, the output is out of order?
int main ()
{
constint SIZE = 10 ;
int integers [ SIZE ] ;
int index ;
cout << "Please enter 10 integers, hitting return after each one: \n" << endl;
for ( index = 0 ; index < SIZE ; ++index )
cin >> integers [ index ] ;
int count = 0 ;
for ( int i = 0 ; i < SIZE ; ++i )
{ bool seen = false ;
for ( int j = 0 ; ! seen && j < count ; ++j )
if ( integers [ j ] == integers [ i ] )
seen = true ;
if ( ! seen )
integers [ count++ ] = integers [ i ] ;
}
return 0 ;
}
Your problem is that you set the current value where you detected that it actually has a duplicate at the start of the array. How could this possibly eliminate the duplicate?
Instead set count = SIZE. Both loops go to count (and not SIZE). When you detect a duplicate swap that value with the last one (count - 1) and decrease count