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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
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 "<<a<<" integers: ";
	for(i=0;i<a;i++)
	{
		cin>>N[i];
	}
	for(i=0;i<a-1;i++)
	{
		for(j=i+1;j<a;j++)
		{
			if(N[i]>N[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
1
2
3
 for (int i=0; i<a; i++){
your code 
}

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.