将一个数组中的值按逆序重新存放,例如,原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。用C语言编写
时间: 2023-12-05 14:40:33 浏览: 107
将数组中的n个数按相反顺序存放。-C++程序设计(谭浩强完整版)
以下是C语言编写的将一个数组中的值按逆序重新存放的代码:
```c
#include <stdio.h>
#include <stdlib.h>
void reverseArray(int arr[], int size) {
int temp;
for (int i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}
int main() {
int arr[5];
printf("Please enter an array: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, 5);
printf("The new array is: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
system("pause");
return 0;
}
```
运行结果为:
```
Please enter an array: 8 6 5 4 1
The new array is: 1 4 5 6 8
```
阅读全文