数组遍历及求和(C语言)

最近学习了C语言的入门,作为数组的综合练习,在此写下相关经验及代码。

题目:在一个长度为10的整型数组里面,保存了班级10个学生的考试成绩。要求编写5个函数,分别实现计算考试的总分,最高分,最低分,平均分和考试成绩降序排序。

代码部分:

#include <stdio.h>
int totalClass(int score[])	//求班级总分
{
    int sum = 0;
    for(int i=0;i<=9;i++)	//数组中每门成绩遍历加求和
    {
        sum += score[i];
    }
    return sum;
}
int maxClass(int score[])	//求班级分数最高分
{
   for(int i=0; i<=9;i++)	//只需要用一组冒泡,最后一个即为最大值
   {
        if(score[i]>score[i+1])	//大的数换到最后

           {
                int temp; 
                temp = score[i];
                score[i] = score[i+1];
                score[i+1] = temp;
           }
    }
    return score[9];
}
   
int minClass(int score[])	//求班级分数最低分

{
    for(int i=0;i<=9;i++)	//冒泡同理,求最小值,并替换
    {
        if(score[i]>score[i+1])
           {
                int temp; 
                temp = score[i];
                score[i] = score[i+1];
                score[i+1] = temp;
           }
    }
    return score[1];
}
    
int aveClass(int score[])	//求班级平均分,直接在求和基础上除以班级人数
{
    int sum = 0;
    for(int i=0;i<=9;i++)
    {
        sum += score[i];
    }
    return sum/10;
}
int seqClass(int score[])	//班级考试分数降序排序;
{
    printf("班级10个学生的考试降序排序为: ");
       for(int i=9; i>=0;i--)	//几轮循环
   {
       for(int j=0;j<=i;j++)	//找最小值放后面
       {
           if(score[j]>score[j+1])
           {
                int temp; 
                temp = score[j];
                score[j] = score[j+1];
                score[j+1] = temp;
           }
           
       }
       printf("%d,",score[i]);
   }
 
}
int main()
{
    int score[10]={67,98,75,63,82,79,81,91,66,84};
    printf("班级10个学生的考试总分为:%d\n",totalClass(score));
    printf("班级10个学生的考试最高分为:%d\n",maxClass(score));
    printf("班级10个学生的考试最低分为:%d\n",minClass(score));
    printf("班级10个学生的考试平均分为:%d\n",aveClass(score));
    seqClass(score);
    return 0;
}
运行结果显示如下:

班级10个学生的考试总分为:786
班级10个学生的考试最高分为:98
班级10个学生的考试最低分为:63
班级10个学生的考试平均分为:78
班级10个学生的考试降序排序为: 98,91,84,82,81,79,75,67,66,63,


只要你认真学习,仔细揣摩,所有的事情都会很简单。



  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值