Answer to Question #332069 in C for Mum

Question #332069

Write a program in C to find out the frequency of elements in an array

1
Expert's answer
2022-04-22T04:43:49-0400
#include <stdio.h>

int main()
{
    int i, j, n, temp, cnt;
    printf("Enter n: ");
    scanf("%d", &n);
    int array[n];    
    printf("Enter array: \n");
    for (int i=0; i<n; i++)
        scanf("%d", &array[i]);
    printf("\nArray:\n");
    for (i=0; i<n; i++)
        printf("%d ", array[i]);
    for (i=0; i<n-1; i++)
        for (j=i+1; j<n; j++)
            if (array[i]> array[j])
            {
                temp = array[i];
                array[i]= array[j];
                array[j] = temp;    
            }
    for (i=0; i<n; i++)
    {
        cnt = 0;
        for (j=i; j<n; j++)
            if(array[i] == array[j])
                cnt++;
            else break;
        printf("\nElement: %d  frequency: %d", array[i], cnt);
        i = j-1;
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS