一条耐人寻味的语句

shiguojie19892 2012-11-14 11:12:33
/***********************************************
里面有一句:++ group[ (int) (student[i]+0.5)/10];
请问什么含义?请详解,谢谢!
***********************************************/
#include <stdio.h>

#define MAX 50
#define COUNTER 11

void main(void)
{
float student[MAX];
int group[COUNTER] = {0};

int i,low,high;

printf("please enter information..\n");
for(i=0; MAX > i; i++)
{
scanf("%f",&student[i]);

++ group[ (int) (student[i]+0.5)/10];
}

printf("\n");
printf(" GROUP RANGE FREQUENCY\n\n");

for(i=0;COUNTER>i;i++)
{
low = i*10;
if (10 != i)
{
high = low+9;
}
else
{
high = 100
}

printf("%4d %3d to %2d %d\n",i+1,low,high,group[i]);
}
return;
}
...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Michael_g 2012-11-14
  • 打赏
  • 举报
回复
(int)(student[i]+0.5)/10 这句的意思就是计算当前这个学生的分数段。分数段取值为 0~10 ,10表示100分,其它的为分数段(就是group 的索引)的范围是 index*10 ~ index* 10+9
Michael_g 2012-11-14
  • 打赏
  • 举报
回复
把(int)(student[i]+0.5)/10当成数组group的下标 ? 答 是的 ++ group[ ....]; 对group[ ....] 值累加 1
Michael_g 2012-11-14
  • 打赏
  • 举报
回复
++ group[ (int) (student[i]+0.5)/10]; 是的 ++ group[ ....]; 对group[ ....] 值累加 1
shiguojie19892 2012-11-14
  • 打赏
  • 举报
回复
引用 1 楼 Michael_g 的回复:
这个是一个统计分数段的代码,每10分一个分数段,100 分单独一个分数段。 ++ group[ (int) (student[i]+0.5)/10]; student[i]+0.5 是将学生的分四舍五入 (int) (student[i]+0.5) 取整数部分,如 85.6 ,结果得86. (int) (student[i]+0.5)/10 是取十分位的数,……
把(int)(student[i]+0.5)/10当成数组group的下标,对group[(int)(student[i]+0.5)/10]进行赋值? ++ group[ ....]; 是对group[.]中的值累计加1吗?
漫步者、 2012-11-14
  • 打赏
  • 举报
回复
++ group[ (int) (student[i]+0.5)/10]
//(student[i]+0.5)/10->(取得的student[i]然后加0.5)/10=n
//再取int类型,也就是得到group下标为n的值
//++group[n]下标为n的数组自增1
转角天边 2012-11-14
  • 打赏
  • 举报
回复
首先group和student都是数组 ++ group[ (int) (student[i]+0.5)/10];等于 group[(int)(student[i]+0.5)/10]=group[(int)(student[i]+0.5)/10]+1; 再来看(int)(student[i]+0.5)/10; student[i]这个不用说了,是数组student第i+1个元素的值, student[i]+0.5就是student第i+1个元素的值加上0.5 (student[i]+0.5)/10就是student第i+1个元素的值加上0.5后再把它除以10 (int)(student[i]+0.5)/10就是student第i+1个元素的值加上0.5后再把它除以10,然后强制转换成int类型 再把(int)(student[i]+0.5)/10当成数组group的下标,对group[(int)(student[i]+0.5)/10]进行赋值
Michael_g 2012-11-14
  • 打赏
  • 举报
回复
这个是一个统计分数段的代码,每10分一个分数段,100 分单独一个分数段。 ++ group[ (int) (student[i]+0.5)/10]; student[i]+0.5 是将学生的分四舍五入 (int) (student[i]+0.5) 取整数部分,如 85.6 ,结果得86. (int) (student[i]+0.5)/10 是取十分位的数,86 取 8 group[ (int) (student[i]+0.5)/10]; 这个学生就被划进 80~89 的分数段。 ++ group[ (int) (student[i]+0.5)/10]; 将这个分数段的人数 +1

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧