c中运用数组给输入的成绩排序并且进行排名
时间: 2023-06-08 13:08:56 浏览: 52
好的,这个问题我可以回答。你可以使用冒泡排序算法来对输入的成绩进行排序,并用另一个数组存储每个成绩对应的排名。下面是示例代码:
```c
#include <stdio.h>
#define SIZE 5 // 成绩数量
void bubble_sort(int arr[], int index[])
{
int i, j, temp;
for(i = 0; i < SIZE - 1; i++) {
for(j = 0; j < SIZE - 1 - i; j++) {
if(arr[j] < arr[j + 1]) {
// 交换两个成绩值
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// 交换两个排名
temp = index[j];
index[j] = index[j + 1];
index[j + 1] = temp;
}
}
}
}
int main()
{
int scores[SIZE] = {80, 95, 70, 75, 85};
int rank[SIZE] = {1, 2, 3, 4, 5};
int i;
bubble_sort(scores, rank);
printf("Rankings:\n");
for(i = 0; i < SIZE; i++) {
printf("Score %d: Rank %d\n", scores[i], rank[i]);
}
return 0;
}
```
上述代码中,首先定义了一个包含5个成绩的数组scores和对应的排名数组rank。然后调用bubble_sort函数对scores数组进行从大到小的排序,并将排序后的排名存储在rank数组中。最后,用循环遍历scores和rank数组,并输出每个成绩对应的排名。