数组值反转

来源:互联网 发布:linux启动java服务 编辑:程序博客网 时间:2024/05/27 10:42


源码

#include <iostream.h>void inv (int x[], int n){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;}}void inv (int *x, int n){int *p, temp, *i, *j, m = (n-1)/2;i=x;j=x+n-1;p=x+m;for ( ; i<=p; i++, j--){temp = *i;*i = *j;*j = temp;}}int main(){int i, a[10] = {3,7,9,11,0,6,7,5,4,2};cout<<"the original array :"<<endl;for (i=0; i<10; i++){cout<<a[i]<<',';}cout<<endl;inv (a, 10);cout<<"the array has been inverted :"<<endl;for (i=0; i<10; i++){cout<<a[i]<<',';}cout<<endl;return 0;}

效果



0 0