### Help..Occurences and Sort.Dynamic array

I have this hw that ask for a program that will ask the user to input integers and sort them in ascending order and write the occurrences for each.
This is what i've done so far. I don't know how to put same elements in one array.

here is the sample form the book:
Sample input: 9 17 9 8 9 5 5
Output:
N count
3 1
5 2
8 1
9 3
17 1

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int main() { int *N,a,i,j,tmp; cout<<"Enter the size of array: "; cin>>a; N = new int[a]; cout<<"Enter "<>N[i]; } for(i=0;iN[j]) { tmp = N[i]; N[i] = N[j]; N[j] = tmp; } } } system("pause"); return 0; }``````
Last edited on
a bad habit the book teaches you is using single letters for Int, try using words that describe what they are for
N=sizeofarray in your for loop it is usually better to declare your variable there instead of making them global so maybe like this
 ``123`` `````` for (int i=0; i

then when the for loop is done the variable is release. and keeps your memory cleaner.

your elements are in one array N[a] your N[i] and N[j] are just positions in your array N[ ] N[a] is just the size or how many spots there are in your array the array is N[ ].
After the array has been sorted, it's a relatively simple matter to loop though the array and compare each element with the next.

If they are the same, add 1 to a count.

If they are different, output the current array value, and the current count, then reset the count to 1.

The very last element is simpler, there is no "next" element, so just output it and the current count.
Topic archived. No new replies allowed.