来检查每个元素有多少次出现在一个数组的函数 [英] A function that checks for how many times each element appears in an array

查看:123
本文介绍了来检查每个元素有多少次出现在一个数组的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做,列出一个数组,并说每一个元素出现多少次的函数。

I am making a function that lists an array and says how many times each element appears.

我已经想到了我自己至今是我应该遍历数组,应该保持它的出现次数的轨道柜台,然后第二个阵列放置在对应的计数器的值到第一阵列中的值。

What I have thought of on my own so far is i should loop through the array and there should be a counter to keep track of the number of times it appears and then a second array to place the value of that counter in correspondence to the value in the first array.

但我不能找出一种算法来搜索,看看是否重复每个值内循环。

But i cant figure out a algorithm to search to see if each value was repeated inside the loop.

推荐答案

code为例进行了简化,并有更多的评论。

Code example has been simplified, and has more comments

下面是一些建议的步骤:结果
1)假设 int类型的[] 排序(为便于计数)(升序或降序,它不物)。结果
2)创建单独的数组保持的找到结果的结果在哪里
第一个一, NUM [] 存储的独特价值和结果
第二 CNT [] 卖场发现,价值数。结果
3)通过分类数组循环。结果
4)在循环,店内独特的价值,和发生的计数保持阵列。

Here are some suggested steps:
1) assuming int a[] is sorted (for ease of counting) (ascending or descending, it does not matter).
2) Create separate arrays to keep found results where
first one, num[] stores the unique value, and
second cnt[] stores the number of that value found.
3) loop through sorted array.
4) in loop, store unique value, and count of occurrence in keep array.

的排序例程的qsort()是一个概念,以后你会学到如果你是刚刚起步,(没有得到它分心现在)但确实观察到的这个例子解决你的问题的一部分,查找评论的看这里的。如上所述,它遍历数组,存储有关数字时,变化信息,以及每个许多有。

The sorting routine qsort() is a concept you will learn later if you are just getting started, (don't get distracted with it for now) but do observe the part of this example addressing your question, look for the comment "Look Here". As described above, it loops through the array, and stores information about when numbers change, and how many of each there are.

这里是一个小code例如:

Here is a small code example:

看评论就知道哪些地方需要集中注意力于设置专柜等。

Look at the comments to know where to focus attention on setting counters etc.

#include <stdio.h>
#define sizea 100 //to make variable declarations easier and consistent

    int num[sizea];//unless array has all unique numbers, will never use this many
    int cnt[sizea];//same comment

int cmpfunc (const void * a, const void * b);//DISREGARD for now (it just works)

int main(void)
{    //a[] is created here as an unsorted array...
    int a[sizea]={1,3,6,8,3,6,7,4,6,9,0,3,5,12,65,3,76,5,3,54,
                  1,3,6,89,3,6,7,4,6,9,0,4,5,12,65,3,76,5,3,54,
                  1,9,6,8,3,45,7,4,6,9,0,89,5,12,65,3,76,5,3,54,
                  6,3,6,8,3,6,7,4,6,9,0,23,5,12,65,3,76,5,3,54,
                  1,3,6,90,3,6,7,4,6,9,0,5,5,12,65,3,76,5,3,54};

    int i, j, ncount;

    for(i=0;i<sizea;i++) cnt[i] = -1;
    for(i=0;i<sizea;i++) num[i] = -999;

    //sort array (AGAIN - DON'T spend time on this part, it just sorts the array)
    qsort(a, sizea, sizeof(int), cmpfunc);

    // a is NOW SORTED, in ascending order, now loop through...
    j=0; //start num and cnt arrays at first element and set ncount to 1
    num[j] = a[0];
    cnt[j] = 1;
    ncount = 1;//start off with at least one of the first number
    //***Look Here***//
    for(i=0;i<sizea-1;i++)//"sizea - 1" so we do not go past a[sizea-1] elements
    {                     //a has sizea elements, indexed from 0 to sizea-1
                          //or a[0] to a[99]
        if(a[i+1] != a[i])
        {
            j++;  //new unique number, increment num[] array
            num[j] = a[i+1];
            ncount = 1; //different number start over
            cnt[j] = ncount;//initialize new cnt[j] with 1
        }
        else
        {
            cnt[j] = ++ncount; //increment cnt, apply it to array
        }
    }
    i=0;

    //We now have a list of unique numbers, and count of each one.  Print it out
    for(i=0;i<j;i++)
    {
        printf("number %d occurs %d times\n", num[i], cnt[i]);
    }
    getchar(); //so results will show. 

    return 0;
}
//Note num[j] and cnt[j] correspond to each other
//num contains a unique number
//cnt contains the number of occurrences for that number

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

有关包括阵列例如,下面是使用这种code的结果:

For the array example included, here are the results using this code:

这篇关于来检查每个元素有多少次出现在一个数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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