简体   繁体   English

需要帮助理解这个 C 代码(数组)

[英]Need help understanding this C Code (Array)

I need help to understand this code clearly, please help.我需要帮助才能清楚地理解这段代码,请帮忙。 I can't figure out how this program keep track of how many number has given in responses array.我无法弄清楚该程序如何跟踪响应数组中给出了多少个数字。 I don't understand what's going on the for loop and specially this line ++frequency[responses[answer]];我不明白 for 循环发生了什么,特别是这条线 ++frequency[responses[answer]];

    #include<stdio.h>
    #define RESPONSE_SIZE 40
    #define FREQUENCY_SIZE 11

    int main(void)
    {
        int answer; /* counter to loop through 40 responses */
        int rating; /* counter to loop through frequencies 1-10 */

        /* initialize frequency counters to 0 */

        int frequency[FREQUENCY_SIZE] = {0};

        /* place the survey responses in the responses array */

        int responses[RESPONSE_SIZE] = {1,2,6,4,8,5,9,7,8,10,1,6,3,8,6,10,3,8,2,7,6,5,7,6,8,6,7,5,6,6,5,6,7,5,6,4,8,6,8,10};

        /* for each answer, select value of an element of array responses
        and use that value as subscript in array frequency to determine element to increment */

        for(answer = 0 ; answer < RESPONSE_SIZE; answer++){
            ++frequency[responses[answer]];
        }

        printf("%s%17s\n", "Rating", "Frequency");

        /* output the frequencies in a tabular format */
        for(rating = 1; rating < FREQUENCY_SIZE; rating++){
            printf("%6d%17d\n", rating, frequency[rating]);
        }

        return 0;
    }
for(answer = 0 ; answer < RESPONSE_SIZE; answer++){
    ++frequency[responses[answer]];     //  <---
}

This above loop just counts the number of times a number appear in array responses and that is stored at that number's index in array frequency .上面的循环只计算一个数字出现在数组responses的次数,并存储在数组frequency中该数字的索引处。 This line does that in first loop -此行在第一个循环中执行此操作 -

 ++frequency[responses[answer]];

So , it increments value at index responses[answer] of array frequency .因此,它在数组frequency索引responses[answer]处增加值。

Lets say responses[answer] has value 1 , then value at index 1 of array frequency is incremented.假设responses[answer]值为1 ,然后数组frequency索引1的值递增。

Second for loop is just for output as mentioned.如前所述,第二个for循环仅用于输出。

++frequency[responses[answer]] is a dense way of writing ++frequency[responses[answer]]是一种密集的写作方式

int r = response[answer];
frequency[r] = frequency[r] + 1;

with the caveat that frequency[r] is only evaluated once.需要注意的是frequency[r]只评估一次。

So, if answer equals 0 , then responses[answer] equals 1 , so we add 1 to frequency[1] .因此,如果answer等于0 ,则responses[answer]等于1 ,因此我们将1添加到frequency[1]

Edit编辑

The following table shows what happens to frequency through the loop (old value => new value):下表显示了循环中frequency的变化(旧值 => 新值):

answer    response[answer]    frequency[response[answer]]
------    ----------------    ---------------------------
     0                   1           frequency[1]: 0 => 1
     1                   2           frequency[2]: 0 => 1
     2                   6           frequency[6]: 0 => 1
     3                   4           frequency[4]: 0 => 1
   ...                 ...           ...
    10                   1           frequency[1]: 1 => 2

etc.等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM