How to check the number of occurrences of integers in an array?

Hi guys. This is a homework problem . I need help.

Write a program that inputs ten integers in an array. It displays the number of occurrence of each number in the array .
Hint:
3 is stored 4 times in the array
1 is stored 3 times in the array

I would've tried it but I am unable to understand the statement of the problem.

Can I get any help for it's code?

For example, you input this array:
2, 3, 4, 5, 6, 1, 2, 3, 2, 5

After this your program must output:
1 is stored 1 time in the array
2 is stored 3 times in the array
3 is stored 2 times in the array
4 is stored 1 time in the array
5 is stored 2 times in the array
6 is stored 1 time in the array
what if I enter a two digit integer at one location?
For example, you input this array:
42, 43, 44, 45, 46, 41, 42, 43, 42, 45

After this your program must output:
41 is stored 1 time in the array
42 is stored 3 times in the array
43 is stored 2 times in the array
44 is stored 1 time in the array
45 is stored 2 times in the array
46 is stored 1 time in the array
All right ! but at least give some hint for this .
I've written this code. It works good unless a number repeats. Can it be fixed?
#include<iostream>
using namespace std;
int main()
{
const int size=10;
int num[size],i,j=0,k,count=1;
for(i=0;i<size;i++)
{
cout<<"Enter integer # "<<i+1<<" :";
cin>>num[i];
}
for(i=0;i<size;i++)
{
j=num[i];
for(k=i+1;k<size;k++)
{
if(num[k]==j)
count++;
}
cout<<"Number "<<num[i]<< " is stored "<<count<<" times in the array."<<endl;
}
return 0;
}
I think skaa is overcomplicating the requirements a little bit. From what I can see, there is no mention that the output must be sorted in the description given of the problem.

skaa wrote:
For example, you input this array:
2, 3, 4, 5, 6, 1, 2, 3, 2, 5

After this your program must output:
1 is stored 1 time in the array
2 is stored 3 times in the array
3 is stored 2 times in the array
4 is stored 1 time in the array
5 is stored 2 times in the array
6 is stored 1 time in the array

So instead of must this should say might, because the following is also valid:
2 is stored 3 time(s) in the array
3 is stored 2 time(s) in the array
4 is stored 1 time(s) in the array
5 is stored 2 time(s) in the array
6 is stored 1 time(s) in the array
1 is stored 1 time(s) in the array

And the second option seems to be the route that the OP has chosen.

@sabbag learner: When posting code blocks, please use the code tags button on the right -> <>
Code is always fixable :) If you were to move this section
1
2
3
4
5
6
for(k=i+1;k<size;k++)
{
if(num[k]==j)
count++;
}
cout<<"Number "<<num[i]<< " is stored "<<count<<" times in the array."<<endl;

into an if statement that depends on whether the number comes earlier in the array, that would solve your problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
j=num[i];
bool numberWasCountedEarlier = false; //innocent until proven guilty
for(k=i-1; k>0; k--) {
    if(num[k]==j) {
        numberWasCountedEarlier = true; //guilty!
    }
}
if(! numberWasCountedEarlier) //was it ! counted earlier?
    for(k=i+1;k<size;k++)
    {
        if(num[k]==j)
            count++;
    }
    cout<<"Number "<<num[i]<< " is stored "<<count<<" times in the array."<<endl;
}
That worked pretty good. Thanks @kevinkjt2000 :)
Topic archived. No new replies allowed.