#include<stdio.h>
#define RESPONSES_SIZE 40
#define FREQUENCY_SIZE 11
int main() {
int frequency[FREQUENCY_SIZE] = { 0 };
int responses[RESPONSES_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 };
//answer增加,responses[i]就会改变,frequency[p](p=responses[i])就会改变了(计数)
for (int answer = 0; answer < RESPONSES_SIZE; ++answer) {
++frequency[responses[answer]];
}
printf("%s%17s\n", "Rating", "Frequency");//打印表头
for (int rating = 1; rating < FREQUENCY_SIZE; ++rating) {
printf("%6d%17d\n", rating, frequency[rating]);
}
return 0;
}
之所以frequency定义成拥有11个元素,是因为出现分数1时,对frequency[1]而非frequency[0]增值。这样可以直接将分数当成下标来访问数组frequency中的元素。(忽略元素0)(书p167)