c语言-数组-排序(简单冒泡排序)-案例-cqupt题库

该程序使用C语言编写,通过ReadScore函数获取学生课程成绩,DataSort函数实现冒泡排序对成绩进行降序排列,PrintScore函数打印排序后的成绩。程序支持最多40名学生的成绩输入,输入负值表示结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组排序

DataSort函数为简单冒泡排序算法

从键盘输入某班学生某门课的成绩(每班人数最多不超过40人),当输入为负值时,表示输入结束,试编程将分数按从高到低顺序进行排序输出。

排序功能需要自定义函数实现。

**输入格式要求:"%d" 
**输入提示信息:"Input score:" "Total students are %d\n" "Sorted scores:" 
**输出格式要求:"%4d"
程序的运行示例如下:
Input score:84
Input score:83 
Input score:88
Input score:87
Input score:61
Input score:-1
Total students are 5
Sorted scores:  88  87  84  83  61
#include <stdio.h>
#define N 40
int ReadScore(int score[]);           /* ReadScore()函数原型 */
void DataSort(int score[], int n);   /* DataSort()原函数型 */
void PrintScore(int score[], int n); /* PrintScore()函数原型 */
int main()
{  	 	    		 
    int score[N], n;
    n = ReadScore(score);      /*调用函数ReadScore()输入成绩,返回学生人数*/
    printf("Total students are %d\n", n);
    DataSort(score, n);        /* 调用函数DataSort()进行成绩排序 */
    printf("Sorted scores:");
    PrintScore(score, n);      /* 调用函数Printscore()输出成绩排序结果 */
    return 0;
}  	 	    		 
/* 函数功能:输入学生某门课的成绩,当输入负值时,结束输入,返回学生人数 */
int ReadScore(int score[])     /* ReadScore()函数定义 */
{  	 	    		 
    int i = -1; /* i初始化为-1,可保证循环体内i增1后数组下标从0开始 */
    do
    {  	 	    		 
        i++;
        printf("Input score:");
        scanf("%d", &score[i]);
    }
    while (score[i] >= 0);    /* 输入负值时结束成绩输入 */
    return i;                    /* 返回学生人数 */
}  	 	    		 
/* 函数功能:按交换法将数组score的元素值元素按从高到低排序 */
void DataSort(int score[], int n) /* DataSort()函数定义 */
{  	 	    		 
    int i, j, temp;
    for (i = 0; i < n - 1; i++)
    {  	 	    		 
        for (j = i + 1; j < n; j++)
        {  	 	    		 
            if (score[j] > score[i]) /* 按数组score的元素值从高到低排序 */
            {  	 	    		 
                temp = score[j];
                score[j] = score[i];
                score[i] = temp;
            }
        }
    }
}  	 	    		 
/* 函数功能: 打印学生成绩 */
void PrintScore(int score[], int n) /* PrintScore()函数定义 */
{  	 	    		 
    int i;
    for (i = 0; i < n; i++)
    {  	 	    		 
        printf("%4d", score[i]);
    }
    printf("\n");
}  	 	    		 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾有所爱,其名华夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值