-3
int responses[] = { 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 };

       int frequency[] = new int[ 11 ]; // array of frequency counters

        // for each answer, select responses element and use that value
        // as frequency index to determine element to increment
        for ( int answer = 0; answer < responses.length; answer++ )
           **++frequency[ responses[ answer ] ];**

        System.out.printf( "%s%10s", "Rating", "Frequency" );

        // output each array element's value
4

4 回答 4

1

answer 是数组的索引,response[answer] 将给出该索引处的值,然后我们将以频率递增索引的值(即 response[answer])。

answer   response[answer]     frequency[response[answer]]    ++frequency[response[answer]]
 0             1             frequency[1]=0                      frequency[1]=1
 1             2             frequency[2]=0                      frequency[2]=1                 
 2             6             frequency[6]=0                      frequency[6]=1
 3             4             frequency[4]=0                      frequency[4]=1
 4             8             frequency[8]=0                      frequency[8]=1
 5             5             frequency[5]=0                      frequency[5]=1
 6             9             frequency[9]=0                      frequency[9]=1
 7             7             frequency[7]=0                      frequency[7]=1
 8             8            **frequency[8]=1**                 **frequency[8]=2**

伪代码:

给定:从 1 到 10 的响应数组

初始化空频率数组,默认为 0 (frequency[0],frequency[1],frequency[2],frequency[3],.....,frequency[9],frequency[10])

对于响应数组的每个值

    increment the value of frequency array 

在最后一行,现在 8 的频率增加了。所以它会告诉每个响应的频率。

于 2013-04-12T11:26:22.763 回答
0

首先:您创建具有值responses的数组:40

   int responses[] = { 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 };

然后你frequency用长度 =创建11,为什么是 11?因为值的范围在responses数组中的值范围从1to11并且您想计算数字重复的次数(例如:数字 1 重复 2 次,数字 6 重复 11 次):

   int frequency[] = new int[ 11 ]; // this array with 0 values at each index

然后,您将在responses数组上循环

for ( int answer = 0; answer < responses.length; answer++ )

++frequency[ responses[ answer ] ];这意味着当 index = 1 时,您将得到which responses [1]will equal2所以您将添加1which到 frequency[2];which 意味着你找到 number等等2responses

要更了解运行此代码:

 int responses[] = {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};
        int frequency[] = new int[11]; // array of frequency counters

        for (int answer = 0; answer < responses.length; answer++) {
            ++frequency[ responses[ answer]];
        }

        for (int i = 0; i < frequency.length; i++) {
            System.out.println(i + "   "  + frequency[i]);

        }

输出是:

0    0/////////// mean there is no 0 in responses array
1    2//////////mean you have number 1 in responses array 2 times
2    2
3    2
4    2
5    5
6    11//////// mean you have number 6 in responses array 11 times
7    5
8    7
9    1
10   3
于 2013-04-12T11:37:13.303 回答
0

您需要使用循环来显示结果:

for ( int i = 0; i < responses.length; i++ )
    System.out.printf( "Number of %d: %d\n", i, frequency[i] );

否则删除注释外的“*”,它将编译。

它的作用是简单地使用数组的值作为索引来计算它们。因此,当遇到“5”时,频率数组中的第 5 个元素将递增,因为至少有一个“5”。

于 2013-04-12T11:25:17.643 回答
0

由于未使用操作的值,因此预递增和后递增给出相同的结果。所以这:

++frequency[ responses[ answer ] ];

与此相同:

frequency[ responses[ answer ] ]++;

与以下内容相同:

frequency[ responses[ answer ] ] = frequency[ responses[ answer ] ] + 1;

代码所做的是递增frequence数组中与数组中的项目相对应的项目responsesresponses它只是计算数组中每个值的数量。

于 2013-04-12T11:32:12.580 回答