将成绩数组按照从高到低进行排序,要求用指针实现
时间: 2024-05-15 10:17:55 浏览: 9
好的,我知道了,下面是使用指针对成绩数组进行从高到低排序的代码:
```
#include <stdio.h>
void sort(int *grades, int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (*(grades + j) > *(grades + i)) {
temp = *(grades + i);
*(grades + i) = *(grades + j);
*(grades + j) = temp;
}
}
}
}
int main() {
int grades[] = {90, 80, 70, 85, 95};
int n = sizeof(grades) / sizeof(grades[0]);
sort(grades, n);
int i;
for (i = 0; i < n; i++) {
printf("%d ", *(grades + i));
}
printf("\n");
return 0;
}
```
这个程序首先定义了一个名为 sort 的函数,它的参数包括一个整数型指针 grades 和一个整数 n,其中 grades 指向了待排序的成绩数组,n 表示数组中元素的个数。sort 函数使用冒泡排序算法将数组从高到低排序。程序的主函数定义了一个名为 grades 的数组,然后调用 sort 函数进行排序,最后输出排序后的结果。