在C中的数组数的频率计数是这样code有效和高效 [英] Frequency Count of Number in an array in C is this code effective and efficient

查看:101
本文介绍了在C中的数组数的频率计数是这样code有效和高效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C以下code从数组数计数频率:

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:
}

请有利于提高我的code或建议的任何其它技术的有效性
是该算法有效和高效与否,请给予反馈。

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

推荐答案

要计数数字阵列频率,试试这个code

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;
}

这篇关于在C中的数组数的频率计数是这样code有效和高效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆