Write a program to count the frequency of each element in array , as example if we have
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
the output should be
number frequency
5 3
10 2
2 3
50 1
1 1
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
the output should be
number frequency
5 3
10 2
2 3
50 1
1 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
using namespace std; | |
int main(int argc, char** argv) { | |
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2}; | |
int flag[10]={0}; | |
//count frequency of each number | |
int counter=1; | |
for(int i=0;i<=9;i++) | |
{ | |
for(int j=i+1;j<=9;j++) | |
{ | |
if (a[i]==a[j]){counter++;flag[j]=1;} | |
} | |
if(flag[i]!=1){cout<<a[i]<<"\t"<<counter<<endl;} | |
counter=1; | |
} | |
return 0; | |
} |
No comments:
Post a Comment