定义一个数组 输入十个数 排序输出
时间: 2023-07-20 09:26:05 浏览: 54
好的,这是一个简单的排序题目,下面是使用C语言实现的示例代码:
```
#include <stdio.h>
int main() {
int arr[10], i, j, temp;
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这段代码首先定义了一个长度为10的数组,然后通过循环输入10个整数,接着使用冒泡排序算法对数组进行排序,最后输出排序后的结果。
相关问题
定义一个数组,输入10个数,排序输出。提交源代码,操作系统
好的,下面是使用C语言定义一个数组,输入10个数,排序输出的源代码:
```c
#include <stdio.h>
int main() {
int arr[10], i, j, temp;
// 输入10个数
printf("请输入10个数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// 冒泡排序
for (i = 0; i < 10 - 1; i++) {
for (j = 0; j < 10 - i - 1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// 输出排序结果
printf("排序结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这份代码使用了冒泡排序算法对输入的10个数进行排序,最后输出排序结果。该代码可以在各种操作系统上运行,只要你的电脑上安装了C语言编译器即可。
1. 简答题定义一个数组,输入10个数,排序输出。提交源代码,写出操作步骤,运行
好的,这是一个简单的排序算法题目,下面是操作步骤和源代码:
1. 首先定义一个长度为10的整型数组。
2. 然后循环输入10个数到数组中。
3. 使用冒泡排序算法对数组进行排序。
4. 输出排序后的数组。
下面是完整的源代码:
```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 arr[10], i;
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, 10);
printf("排序后的数组为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
运行结果示例:
```
请输入10个整数:
10 8 6 4 2 9 7 5 3 1
排序后的数组为:
1 2 3 4 5 6 7 8 9 10
```
相关推荐
![](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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)