Find frequency of digits in an int array in C

前提是你 提交于 2019-12-11 06:07:20

问题


I am trying to write a function that takes the size of an array and an int array made up of digits as input and prints the frequency of each digit.

Sample input and output:

Input: [1,2,2,3,3,3]

Output:
1 occurs 1 times.
2 occurs 2 times
3 occurs 3 times.

Here is my attempt (not the most elegant):

void freq(int size, int numArray[]) {
    int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
    int i, j;

    for (i = 0; i < size; i++) {
        for (j = 1; j < size; j++) {
            if (numArray[i] == numArray[j] && numArray[i] == 1) {
                one+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 2) {
                two+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 3) {
                thr+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 4) {
                fou+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 5) {
                fiv+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 6) {
                six+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 7) {
                sev+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 8) {
                eit+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 9) {
                nin+=1;
            }
        }
    }
    printf("1 occurs %d times.\n", one);
    printf("2 occurs %d times.\n", two);
    printf("3 occurs %d times.\n", thr);
    printf("4 occurs %d times.\n", fou);
    printf("5 occurs %d times.\n", fiv);
    printf("6 occurs %d times.\n", six);
    printf("7 occurs %d times.\n", sev);
    printf("8 occurs %d times.\n", eit);
    printf("9 occurs %d times.\n", nin);
}

This has problems. If I use the same example as above, this is what I get:

Input: [1,2,2,3,3,3]

Output:
1 occurs 0 times.
2 occurs 4 times.
3 occurs 9 times.

回答1:


The nested loop approach makes no sense, you only need to look at each digit once to count it. And of course an array makes more sense to keep the counters:

void freq(int size, const int *numbers)
{
  unsigned int counts[10] = { 0 };
  for(int i = 0; i < size; ++i)
  {
    const int here = numbers[i];
    if(here >= 1 && here <= 9)
      counts[here]++;
  }
  for(int i = 1; i < 10; ++i)
    printf("%d occurs %u times\n", i, counts[i]);
}



回答2:


your logic is flawed. what you want is to iterate once over the array and check each element. something like this:

for(int i = 0; i < size; i++){
    if (numArray[i] == NUMBER)
       NUMBER_COUNTER ++;
.
.
.

Or, using your original code:

void freq(int size, int numArray[]) {
    int one=0, two=0, thr=0;
    int i;

    for (i = 0; i < size; i++) {
            if (numArray[i] == 1) {
                one+=1;
            }
            else if (numArray[i] == 2) {
                two+=1;
            }
            else if (numArray[i] == 3) {
                thr+=1;
            }
    }
    printf("1 occurs %d times.\n", one);
    printf("2 occurs %d times.\n", two);
    printf("3 occurs %d times.\n", thr);

}

Another solution that I think is more elegant is this: create a new array with 9 elements (assuming you want to count frequencies of 9 digits...) and increment the slot of the found digit... something like this:

    void freq(int size, int numArray[]) {
    int freq_arr[size];
    int i;

    for(i = 0; i < size; i++)
        freq_arr[ numArray[i] ] ++;
    for(i = 0; i < size; i++)
        printf("i: %d = %d\n", i, freq_arr[i]);



回答3:


What do you need the inner loop for? What you need is to look at each digit once in each iteration so as to count it. It is simpler than you think. Change your code to :

void freq(int size, int numArray[]) {
    int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
    int i, j;

    for (i = 0; i < size; i++) {
        if (numArray[i] == 1) 
            one+=1;
        else if (numArray[i] == 2)
            two+=1;
        else if (numArray[i] == 3) 
            thr+=1;      
        else if (numArray[i] == 4) 
            fou+=1;
        else if (numArray[i] == 5) 
            fiv+=1;
        else if (numArray[i] == 6) 
            six+=1;
        else if (numArray[i] == 7) {
            sev+=1;
        else if (numArray[i] == 8) 
            eit+=1; 
        else if (numArray[i] == 9) 
            nin+=1;
}
    printf("1 occurs %d times.\n", one);
    printf("2 occurs %d times.\n", two);
    printf("3 occurs %d times.\n", thr);
    printf("4 occurs %d times.\n", fou);
    printf("5 occurs %d times.\n", fiv);
    printf("6 occurs %d times.\n", six);
    printf("7 occurs %d times.\n", sev);
    printf("8 occurs %d times.\n", eit);
    printf("9 occurs %d times.\n", nin);
}

Another approach instead of keeping ten int variables as counters would be to keep an array of ten ints, like this :

void freq(int size, int numArray[]) {

    int i;
    int counters[10];
    //initialize the array's elements to zero
    for (i = 0; i < 10; i++)
        counters[i] = 0;

    for(i = 0; i < size; i++)
    {
        if (numArray[i] = i)
            counters[i]++;
    }
}



回答4:


void freq(int size, const int numArray[]) {
    int cnt[10] = {0};
    for (int i=0; i<size; i++) {
         if (0 <= numArray[i] && numArray[i] <= 9)
           cnt[numArray[i]]++;
    }
    // cnt = {0, 3, 1, 0, 0, 0, 0, 0, 0, 0}
}

// test
int main(int argc, char* argv[])
{
    int arr[4] = {1,1,1,2};
    freq(4, arr);
    return 0;
}


来源:https://stackoverflow.com/questions/39245576/find-frequency-of-digits-in-an-int-array-in-c

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