I am having the user enter in a series of numbers into an array. I am then wanting to order that set of numbers from lowest to highest while deleting and duplicate numbers they may have entered.
I have been able to order it from lowest to highest just fine. However, when I use 'unique' and then use a loop to print the array after I use 'unique' things go wrong.
Also I am needing to count how many times a number was entered. For example.. if i entered: 1 2 3 2 5 6. I need it to print out something like...
1 occurred 1 time(s)
2 occurred 2 time(s)
3 occurred 1 time(s)
5 occurred 1 time(s)
6 occurred 1 time(s)
something to that effect. The part I need help with is finding out how to actually count how many times a number was entered in an array.
Here is what I have.
Since unique deletes those numbers in the array when I try to print it out at the end it fills in the spots at the end with other numbers. If you run the program you'll see what I mean.Code:#include<iostream> #include<algorithm> using namespace std; int main () { int x; int anArray[99999]; /*Ask user for size of array and have them fill that array*/ cout << "How many integers will you enter? "; cin >> x; cout << "Enter integers between 0-100" << endl; for(int i = 0; i < x; i++) { cin >> anArray[i]; } /*Sorts the integers from lowest to highest*/ sort(anArray, anArray+x); /*Deletes the duplicates*/ unique(anArray, anArray+x); /*Prints out the new array...This is where the trouble is.*/ for(int i = 0; i < x; i++) { cout <<anArray[i] << endl; }