用c++ 写 输入数字个数和数字并排序
时间: 2024-05-10 08:20:40 浏览: 9
以下是使用C语言编写输入数字个数和数字并排序的示例代码:
```
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sorting the array using bubble sort
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;
}
}
}
printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
在此示例中,我们首先要求用户输入要排序的数字的数量。然后,我们使用scanf函数从用户输入中读取数字,并将它们存储在一个数组中。接下来,我们使用冒泡排序算法对数组进行排序。最后,我们打印已排序的数组。