跪求:C语言课程设计题目 SOS!!!!!

来源:百度知道 编辑:UC知道 时间:2024/09/26 13:31:28
将数组中的数按颠倒的顺序重新存放并输出原数组和新数组。在操作时,只能借助一个临时存储单元而不能另外开辟数组。

/*reverse函数就是你要的函数*/
#include <stdio.h>
typedef int ElementType;

void swap(ElementType *L, ElementType *R )
{
ElementType Tmp = *L;
*L = *R;
*R = Tmp;
}

void print(ElementType A[], int n)
{
for(int i = 0; i < n; i++)
printf("%d ", A[i]);
printf("\n");
}

void reverse(ElementType A[], int n)
{
print(A, n);
for(int i = 0; i < n-i; i++)
{
swap(&A[i], &A[n-i-1]);
}
print(A, n);
}

void main()
{
ElementType A[10];
int n = 10;
for(int i = 0; i < n; i++)
A[i] = i;
reverse(A, 10);

getchar();
}