以下代码是如何确认输入的数字是否在特定范围内的示例。在C编程中会做什么:++ group [(int)(value [i] +0.5)/ 10]
例如,如果我输入数字说N,然后我想增加计数器,它说,如果它是在范围:
1-10 11-20 ... 91-100
Here is the code snip from one of the text book:
#define MAXVAL 50
#define COUNTER 11
main()
{
float value[MAXVAL];
int i, low, high;
static group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0}
*/READING AND COUNTING*/
for(i=0; i<MAXVAL; i++)
{
/* READING OF VALUES*/
scanf("%f", &value[i]);
/* COUNTING FREQUENCY OF GROUPS */
++group[ (int) (value[i]+0.5)/10] <<< I would like to understand how this will find if number is in specific ranges?
/* PRINTING OF FREQUENCY TABLE */
printf("\n");
printf(" GROUP RANGE FREQUENCY\N\N");
for(i=0; i< COUNTER; i++)
{
low = i*10;
if (i==10)
high =100;
else
high=low + 9;
printf(" %2d %3dto%3d %d)\n", i+1, low,high,group[i]);
}
}
这是什么会做在上述C程序:++组[(int)的(值[I] 0.5)/ 10]
由于
如果数字是10.5,该怎么办?你的范围有差距。 – Alnitak
你应该对答案投票,没有人会答复未来的答案。 –