用c语言输入一个班十个学生成绩用数组进行排序
时间: 2024-03-01 18:49:47 浏览: 182
以下是使用 C 语言实现对一个班十个学生成绩进行排序的示例代码:
```c
#include <stdio.h>
int main() {
int i, j, temp;
int scores[10] = {80, 90, 70, 85, 60, 95, 75, 88, 65, 92}; // 学生成绩数组
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (scores[j] > scores[j+1]) {
temp = scores[j];
scores[j] = scores[j+1];
scores[j+1] = temp;
}
}
}
// 输出排序后的学生成绩数组
printf("排序后的学生成绩:\n");
for (i = 0; i < 10; i++) {
printf("%d ", scores[i]);
}
return 0;
}
```
以上代码中,我们使用冒泡排序算法对学生成绩数组进行排序,从小到大输出排序后的学生成绩数组。你可以将这个示例代码复制到你的编译器中运行测试,也可以根据你的实际需求对代码进行修改。
相关问题
一个班有n个同学,通过键盘输入成绩,对n个学生的成绩进行排序。使用c语言数组编程
以下是使用冒泡排序算法来对学生成绩进行排序的C语言代码实现:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, i;
printf("请输入学生人数:");
scanf("%d", &n);
int scores[n];
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的成绩:", i + 1);
scanf("%d", &scores[i]);
}
bubbleSort(scores, n); // 对成绩进行排序
printf("排序后的成绩为:\n");
for (i = 0; i < n; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}
```
代码中,我们先通过 `scanf` 函数从键盘输入学生人数 `n`,然后使用 `scores[n]` 定义一个长度为 `n` 的整型数组来存储学生成绩。
接着,我们使用 `for` 循环依次读入每个学生的成绩,并通过调用 `bubbleSort` 函数对成绩进行排序。`bubbleSort` 函数使用冒泡排序算法,从小到大地对数组 `arr` 进行排序。
最后,我们再次使用 `for` 循环输出已经排好序的成绩数组 `scores` 。
一个班有n个同学,通过键盘输入成绩,对n个学生的成绩进行排序。用c语言数组编程
可以使用冒泡排序法来对学生成绩进行排序。具体代码如下:
```c
#include <stdio.h>
int main()
{
int n, i, j, temp;
printf("请输入学生人数n:");
scanf("%d", &n);
int score[n];
printf("请输入%d个学生的成绩:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &score[i]);
}
// 冒泡排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (score[j] > score[j + 1]) {
temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
printf("排序后的学生成绩为:\n");
for (i = 0; i < n; i++) {
printf("%d ", score[i]);
}
printf("\n");
return 0;
}
```
上述代码中,首先要输入学生的人数n和各位同学的成绩。然后使用冒泡排序对学生成绩进行排序,最后输出排序后的学生成绩。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)