| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 664 人关注过本帖
标题:利用数组求平均值
只看楼主 加入收藏
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
结帖率:71.43%
收藏
 问题点数:0 回复次数:1 
利用数组求平均值
Write a program that uses an array to hold course grades.
Array is initialized to zero in the beginning.
Program then asks user to enter student number and then asks
user to enter grade for the student in question.
Program must check that array bounds are not violated.
User terminates input by entering -1 as student number.
In the end program calculates and prints the average of
course grades.


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int grades[20];
    int index;
    int value;
    int sum;
    int i=0;
    float average;
    while(1){
    printf("Enter student number: ");
    fflush( stdin );
    scanf("%d", &index);
    if (index >= 0 && index < 20)
    {
        printf("Enter grade for student number %d:", index);
        scanf("%d", &value);
        grades[index] = value;
    }
    else if(index==-1)
    break;
    else
    {
        printf("You entered an invalid student number\n");
    }
    sum+=grades[i];
        i++;
    }
printf("the average grade is %8.3f",average=sum/i);
    return 0;
}
利用数组求平均值
输出的数字好大啊
=。=
搜索更多相关主题的帖子: 平均值 
2010-02-01 02:11
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:0 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int grades[20];
    int index;
    int value;
    int sum=0;
    int i=0;
    float average;
    while(1){
        printf("Enter student number: ");
      fflush( stdin );
      scanf("%d", &index);
       if (index >= 0 && index < 20)
        {
            printf("Enter grade for student number %d:", index);
            scanf("%d", &value);
            grades[index] = value;
        }
       else if(index==-1)
            break;
        else
        {
            printf("You entered an invalid student number\n");
        }
        sum+=grades[index];
        i++;
    }
    printf("the average grade is %8.3f",average=sum/i);
    return 0;
}

★★★★★为人民服务★★★★★
2010-02-01 07:38
快速回复:利用数组求平均值
数据加载中...
 
   



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

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