我制作了一个计算二维数组频率的程序,我希望它给我每一行的结果(对于每个 i)。但是当我添加for循环for(i=0;i<S1;i++)
时,它会计算第一行的频率,例如第二行的频率,它将第一行的频率添加到第二行,依此类推。我怎样才能防止它添加频率?有什么建议吗? ? 谢谢 :)
编辑:使我的问题更清楚:如果我添加数字 {10 20 30 30 30 30 20 30 10}
我应该期望收到以下结果:
1st row 0-9 0
10-19 1
20-29 1
30-39 1 from the row {10 20 30}
2nd row 0-9 0
10-19 0
20-29 0
30-39 3 from the row {30 30 30}
3rd row 0-9 0
10-19 1
20-29 1
30-39 1 from the row {20 30 10}
我没有得到这些结果
1st row
0-9 0
10-19 1
20-29 1
30-39 1
from the row {10 20 30}
2nd row 0-9 0
10-19 2
20-29 2
30-39 2 from the row {30 30 30}
3rd row 0-9 0
10-19 3
20-29 3
30-39 3 from the row {20 30 10}
#include <stdio.h>
#define S1 3
#define COUNTER 11
#define S2 3
int main() {
float A[S1][S2];
int i, j, k, low, high;
int group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0};
for (i = 0; i < S1; i++) {
for (j=0; j < S2; j++) {
scanf("%f", &A[i][j]);
}
}
for(i=0; i < S1; i++) {
for(j=0; j < S2; j++) {
++ group[(int)(A[0][j] + 0.5 ) / 10];
}
printf("\n");
printf("GROUP RANGE FREQUENCY\n\n") ;
for(k = 0; k < COUNTER; k++) {
low = k * 10;
if(k == 10)
high = 100;
else
high = low + 9;
printf("%2d %3d to %3d %d\n",
k+1, low, high, group[k]);
}
}
getch();
}