以下代碼是如何確認輸入的數字是否在特定範圍內的示例。在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
你應該對答案投票,沒有人會答覆未來的答案。 –