Write a function that accepts an array and constant parameters and count how many elements of array are less than the constant, equal to it and greater than it. Return these three values either by way of pass by reference parameter or a three element array parameter.

#include<stdio.h>
int* count(int a[],int n,int ele)
{
    int i,c[3]={0,0,0};
    for(i=0;i<n;i++)
    {
        if(a[i]==ele)
            c[0]++;
        else if(a[i]<ele)
            c[1]++;
        else
            c[2]++;
    }
    return c;
}

main()
{
    int a[100],n,i,*cnt,item;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the integers=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\nThe array elements are=");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\nEnter a number=");
    scanf("%d",&item);
    cnt=count(a,n,item);   
    printf("\nNumber of elements equale to %d=%d\nNumber of elements less than %d=%d\nNumber of elements greater than %d=%d\n",item,cnt[0],item,cnt[1],item,cnt[2]);   
}

No comments:

Post a Comment