【C语言】【指针】将数组中n个整数按相反顺序存放

#include <stdio.h>

int main()

{

void inv(int x[],int n);      //inv函数声明//  

int  i,a[10] = {3,5,7,9,2,3,0,1,6,4};  //定义数组,注意不要溢出,元素可以自定义//  

printf("The array has been inverted:n");

for(i = 0;i<10;i++)

printf("%2d",a[i]);        //输出未交换时数组各元素的值 //  

printf("n");

inv(a,10);          //调用inv函数//  

printf("The array has been inverted:n");  //输出交换后的各元素的值 //

for(i = 0;i<10;i++)

printf("%2d",a[i]);        //输出格式2d便于查看,个人习惯//  

printf("n");

return 0;

}

void inv(int x[],int n)      //形参x是数组名//  

{

int temp,i,j,m = (n-1)/2;

for(i=0;i<=m;i++)

{

j = n-1-i;

temp = x[i];         //此处也可以写成一行,这样写纯属层次清晰//  

x[i] = x[j];

x[j] = temp;

}

return;

发表评论

相关文章


Warning: file_get_contents(): Filename cannot be empty in /www/wwwroot/news.hzrhlnu.cn/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 3431
==========================