用c语言数组编写程序将一维数组中的元素重新按逆序存放在原数组中,且只定义一个数组
时间: 2023-09-18 07:12:11 浏览: 156
以下是用C语言实现将一维数组中的元素重新按逆序存放在原数组中的程序:
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, temp;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
// Reversing the array
for(i=0; i<n/2; i++)
{
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
printf("The array after reversing is:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
```
在这个程序中,我们使用了一个名为`arr`的一维整型数组,它的大小为`MAX_SIZE`。用户输入数组的大小和元素,然后程序将数组按逆序重新排列,最后输出排列后的数组。
我们使用一个`for`循环来遍历数组,并使用一个`temp`变量来交换数组的元素,从而实现数组的逆序排列。
阅读全文
相关推荐
![](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)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)