Write a program that count the occurrence of each element in an int array
of size 25
Sample Input: 1 13 1 4 20 5 1 5 13 1 5 20 4 3 5
Sample Output:
occurrence of 1 : 4
occurrence of 13 : 2
occurrence of 4 : 2
occurrence of 20 : 2
occurrence of 5 : 4
occurrence of 3 : 1
Code:
#include<stdio.h>
main()
{
int a[25];
int i,j,length,count=0,input;
printf("Enter length of array:\n");
scanf("%d",&length); //here input total length if array
printf("Enter elements of array:");
for(i=0;i<length;i++) //here this loop will run while not equal to array length
{
scanf("%d",&input); //here input elemnets in array
a[i]=input;
}
printf("\n");
for(i=0;i<length;i++)
{
count=1;
for(j=i+1;j<length;j++)//here I set j to i+1 bcz it wiil cheak the a[1] index with other index and so on
{
if(a[i]==a[j] && a[i]!='\0')//here I cheak if a[1] is equal to this so run this condition
{
count++;//if condtion is true so increment in count
a[j]='\0'; //set a[j] to null or I can also change it to special character
}
}
if(a[i]!='\0'){
printf("occurrence of %d:%d\n",a[i],count);
}
}
return 0;
}
Program Output:
For more problems click here
No comments:
Post a Comment