この例C帳をプログラムする方法から調査を要約する配列を使用すると、
四十学生は(1がひどい意味1〜10のスケールの 学生食堂で食品の品質を評価してもらった結果10は平均で を意味します)。 40個の応答を整数配列に置き、 を投票結果に要約します。
私はあなたが事前に
// Analyzing a student poll.
#include <stdio.h>
#define RESPONSES_SIZE 40 // define array sizes
#define FREQUENCY_SIZE 11
int main(void) {
size_t answer; // counter to loop through 40 responses
size_t 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[ 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 };
// 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 < RESPONSES_SIZE; ++answer)
{
frequency[ responses [ answer ] ]=frequency[ responses [ answer ] ]+1;
} // end for
// display results
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 ]);
} // end for
// end main
return 0;
}
これはかなり自明です。コードは役に立つコメントで埋められています。私はあなたがもっと良い説明を得ることはできないと思います。誰でも試してみると、最高でコメントに書かれていることを繰り返すだろう。 –
あなたが理解していないのは正確には何ですか?この部分 "頻度[応答[回答]] =頻度[応答[回答]] +1; ? – dreamcrash
ちょうどこれは、@ dreamcrashを理解するために必要な説明です。 – Elhaw