定义一个函数,可以数组逆序
时间: 2024-01-03 07:06:02 浏览: 38
以下是一个示例代码,用于将输入的数组逆序:
```c
#include <stdio.h>
void reverseArray(int arr[], int n) {
int i, j, temp;
for (i = 0, j = n - 1; i < j; i++, j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
int arr[100], n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements in the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, n);
printf("The reversed array is: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
在这个示例中,`reverseArray()` 函数用于将输入的数组逆序。主函数中首先读取数组的大小 `n`,然后逐个读取输入的数字,并将它们存入数组中。接着调用 `reverseArray()` 函数来逆序数组。最后,程序输出逆序后的数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)