| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 577 人关注过本帖
标题:求值的加权平均数;发生错误,错误见附图
只看楼主 加入收藏
chenziguo
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2013-10-25
结帖率:58.33%
收藏
已结贴  问题点数:8 回复次数:5 
求值的加权平均数;发生错误,错误见附图
#include <stdio.h>
#include <stdlib.h>

void main()
{
int i=1,j;
float score[5],radio[4]={0.1,0.2,0.3,0.4};
char key=' ';
while(key==' ')//按其他键盘则退出
{
    printf("please input the %d student",i++);
    printf(" the score:");
    for(j=0;j<4;j++)
    {
        scanf("%f",&score[j]);//输入4个值
        score[4]+=score[j]*radio[j];//将其加权后的值赋值给score【4】中
    }
    printf("the average score is %.2f\n",score[4]);//输出secore【4】的值
    printf("please press the space continue or other exit");
    key=getchar();
}
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: please include average 键盘 平均数 
2015-04-07 11:29
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:3 
程序代码:
#include <stdio.h>

int main(void)
{
    float score[4] = {0.0};
    const float radio[4] = {0.1, 0.2, 0.3, 0.4};
    float weighted = 0.0;
    int i = 0;
    int num = 4;
    while (i < num)
    {
        printf("Please input No. %d student's score: ", i + 1);
        scanf("%f", &score[i]);
        weighted += score[i] * radio[i];
        i++;
    }
   
    printf("\nThe weighted score is %f\n", weighted);

    return 0;
}


我感觉这样清晰一些
2015-04-07 12:08
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
你的退出方法我感觉是有问题的
2015-04-07 12:08
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:3 
float score[5],数组没有初始化,
score[4]+=score[j]*radio[j];//将其加权后的值赋值给score【4】中
score[4]没有初值。

总有那身价贱的人给作业贴回复完整的代码
2015-04-07 15:07
happy两棵树
Rank: 2
等 级:论坛游民
帖 子:28
专家分:62
注 册:2013-7-18
收藏
得分:3 
score[4] = 0;
2015-04-07 16:11
chenziguo
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2013-10-25
收藏
得分:0 
非常感谢“happy两棵树”和“embed_xuel”;问题出在初值问题上面,但是顺便问一个问题,不是数组未赋值的情况下都是0吗?不知道我的说法的前提条件是是什么
2015-04-08 11:41
快速回复:求值的加权平均数;发生错误,错误见附图
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021771 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved