Frequency Count of Number in an array in C is this code effective and efficient [closed]

依然范特西╮ 提交于 2019-12-08 15:15:10

问题


I have the following code in c for counting frequency of number from array:

#define MAX 10
int flag=0;

void display(int no,int cnt,int visi[]);//function declaration 

int main()
{
    int arr[]={1,1,1,2,3,4,2,2,3,1};//asume any array or we can enter from user
    int visited[MAX];
    int i,j,no,cnt=1;
    clrscr();
    for(i=0;i<10;i++)//loop
    {
        no=arr[i];
        cnt=1;
        for(j=i+1;j<10;j++)
        {
            if(no==arr[j])
                cnt++;
        }
        display(no,cnt,visited);
    }
    return 0;
}

void display(int no,int cnt,int visited[])
{
  int static i;
  int j;

  if(flag==1)
    for(j=0;j<=i;j++)
    {
      if(visited[j]==no)
        goto a;
    }
  i++;
  flag=1;
  printf("\n%d=%d",no,cnt);
  visited[i]=no;
  a:
}

Please help to improve my code or suggest any other technology for effectiveness is this algorithm effective and efficient or not please give feedback.


回答1:


You can sort the array first by merge sort (O(n log n)) and then calculate the frequency of a number by single loop like this-:

int j=0;    
for( i = 0; i < 9; i++ )
{    
    if (arr[i] == arr[i+1]) 
        cnt++;
    else 
    {
        visited[j] = cnt; 
        cnt = 0;
        j++;
    }        
}



回答2:


to count frequency of numbers in array, try this code

#include <stdio.h>
#define MAX 10

int countArray[MAX];

int main()
{
    int arr[]={1,1,1,2,3,4,2,2,3,1},i;
    for(i=0;i<MAX;i++)
         countArray[i]=0;
    for(i=0;i<MAX;i++)
        countArray[arr[i]]++;
    for(i=0;i<MAX;i++)
    {
        if(countArray[i])
            printf("%d %d\n",i,countArray[i]);
    }

    return 0;
}



回答3:


You don't say this explicitly, but it looks as if you had an array of non-negative numbers whsoe values is smaller than MAX.

If the range of the numbers is known, you can create an array of counts. Visit each element of the array once and increment the count for that element. Then pass through the array of counts and output it as appropriate.

This method works well if the range of valid numbers and therefore the size of the count array is small. (You have the same problem for your visited array, whose size is the same as the size of an array of counts.)

The example below implements counting with an array of counts. The code also takes care of values that fall outside the valid range of MIN to MAX inclusively.

#include <stdio.h>

#define MIN 1
#define MAX 10

int main()
{
    int arr[] = {1, 1, 1, 2, 3, 4, 2, 2, 3};
    int narr = sizeof(arr) / sizeof(arr[0]);

    int count[MAX + 1 - MIN] = {0};
    int uncounted = 0;
    int i;

    for(i = 0; i < narr; i++) {
        if (arr[i] < MIN || arr[i] > MAX) {
            uncounted++;
        } else {
            count[arr[i] - MIN]++;
        }
    }

    for(i = MIN; i < MAX + 1; i++) {
        if (count[i - MIN]) {
            printf("element %d ocurs %d times.\n", i, count[i - MIN]);
        }
    }

    if (uncounted) {
        printf("%d elements were not accounted for.\n",  uncounted);
    }

    return 0;
}



回答4:


int main()

Is implementation dependant. You do not wan't to use this, some compilers will not accept it.

Use

int main(void)

instead

for(i=0;i<10;i++)//loop

You already defined MAX, so why not use it there. Stuff like this makes code harder to maintain.

The efficiency is dependant on the input values you have. For small arrays with small numbers, this works fine otherwise.



来源:https://stackoverflow.com/questions/33473716/frequency-count-of-number-in-an-array-in-c-is-this-code-effective-and-efficient

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!