Well I got my code to delete duplicates when they are right next to each other (ex. 10,11,11,12,13,14 | prints out 10,11,12,13,14), but doesnt find the duplicates when they are spread apart (ex. 10,11,12,11,13,14 | prints out 10,11,12,11,13,14). Any help would be great!
the program sent out yesterday also takes care of non-adjacent duplicates, but you probably you'd like to understand why this program doesn't work as well which is fair enough
you can do it with the bubble sort algorithm, but its horrible if you ever need to do it for more than a small array.
basically, grab the first guy in the array, and compare it to every other value. Delete them as you find dupes. Move to second element, repeat... N*N effort loops. Don't forget to loop using size and update size as you delete stuff...!!
if you sort it first, its NlgN much much faster for large N. But you lose your ordering.
There are some other tricks you can play, but the first answer, while brute force, is the easy one.
To be completely honest, I didn't really understand the other responses
... that's not what you said yesterday ...
Thanks so much. I got it to work!
http://www.cplusplus.com/forum/beginner/211980/#msg991799
anyways, my point is not to give you a hard-time but just make sure if you have trouble understanding anything you make it clear upfront rather than declare pre-mature satisfaction. there's no shame in that, we're all learning new stuff all the time
#include <iostream>
int main()
{
constint SIZE = 20 ; // *** const
constint MINV = 20 ;
constint MAXV = 100 ;
int number[SIZE] {} ; // initialise to all zeroes
int cnt = 0 ; // count of numbers entered by the user
int cnt_unique = 0 ; // count of unique numbers entered into the array
std::cout << "enter " << SIZE << " integers between " << MINV << " and " << MAXV << '\n' ;
int n ;
while( cnt < SIZE && std::cout << '#' << cnt+1 << "? " && std::cin >> n )
{
if( n < MINV || n > MAXV )
{
std::cout << n << " is not between " << MINV << " and " << MAXV
<< "\nplease enter another another number\n" ;
}
else
{
++cnt ;
// check if n is a duplicate; if not add it to the array
bool unique = true ;
for( int i = 0 ; i < cnt_unique ; ++i )
{
if( n == number[i] ) { unique = false ; break ; }
}
if(unique) number[cnt_unique++] = n ;
}
}
// print out the unique numbers
std::cout << "the unique numbers are: " ;
for( int i = 0 ; i < cnt_unique ; ++i ) std::cout << number[i] << ' ' ;
std::cout << '\n' ;
}