C++ Program To Count Frequency each number in an array using random inputs

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

void main()
{
int a[100], freq[100], n, i, j, cnt, max, min, maxp, minp;
clrscr();
cout<<"Program to count Number of Occurence in an array\n";
cout<<"=================================================\n\n";
cout<<"Enter No. of Elements in Array (Max. 100): ";
cin>>n;

randomize();
for(i=0;i<n;i++)
{
//Store the Randome Values in Array between 1 to 20.
a[i]=random(5)+101;
//initialize the frequency values with -1
freq[i]=-1;
//Print the randomly generated array values.
cout<<setw(5)<<a[i];
}
cout<<endl;

//Logic to look duplicate values and count total nos.
for(i=0;i<n;i++)
{
cnt=1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j]) //If duplicate value found
{
cnt++;
//Set Freq[i] to 0 which has -1, to confirm
//that the same element not to count again
freq[j]=0;
}
}
//If the number at position is unique set its count value to 1
if(freq[i]!=0)
freq[i]=cnt;
}

cout<<"=============\t=========\n";
cout<<"Element Value\tOccurence\n";
cout<<"=============\t=========\n";
//Printing the frequency of each array element
for(i=0;i<n;i++)
{
if(freq[i]!=0)
cout<<"\t"<<a[i]<<"\t   "<<freq[i]<<endl;
}
cout<<"=============\t=========\n";
getch();
}


No comments

Post your comments

Powered by Blogger.