C++上机|编写函数invert实现对一维数组的倒序

cpp 复制代码
//编写函数invert实现对一维数组的倒序
# include <iostream>
using namespace std;

class invert_array
{
    private:
    //int a[10]={0,1,2,3,4,5,6,7,8,9};
 
public:
void invert(int A[], int n){
    int i,j,temp;
    int m = (n-1)/2;
    for(i=0;i<=m;i++){
    j=n-1-i;
    temp=A[i];
    A[i]=A[j];
    A[j]=temp;
    }
    cout<<"数组已经倒排!数组为形参"<<endl;
}
//用指针作为形参实现invert(int *A,int n),函数调用时实参为数组。
// void invert(int *A, int n){
//     int *i,*j,temp,*p;
//     int m = (n-1)/2;
//     i = A;
//     j = A+n-1;
//     p=A+m;
//     for(;i<p;i++,j--){
//         temp=*i;
//         *i=*j;
//         *j=temp;
//     }
//       cout<<"数组已经倒排!指针为形参"<<endl;
// }
void show_array(int a[],int n){

      int i=n;
    for (i=0;i<10;i++)
        cout <<a[i]<<"\t";
        cout<<endl;        
}

};

int main()
{
    int a[10]={0,1,2,3,4,5,6,7,8,9};
    int n=10;
    invert_array ia;
    ia.invert(a,n);
    ia.show_array(a,n);
     ia.invert(a,n);
    ia.show_array(a,n);
        system("pause");
        return 0;
}

运行结果

相关推荐
繁依Fanyi24 分钟前
用 CodeBuddy 实现「IdeaSpark 每日灵感卡」:一场 UI 与灵感的极简之旅
开发语言·前端·游戏·ui·编辑器·codebuddy首席试玩官
虾球xz28 分钟前
游戏引擎学习第292天:实现蛇
c++·学习·游戏引擎
duapple4 小时前
Golang基于反射的ioctl实现
开发语言·后端·golang
Dxy12393102164 小时前
Python 条件语句详解
开发语言·python
jjkkzzzz6 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
prinrf('千寻)6 小时前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
pystraf6 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
m0_555762907 小时前
Qt缓动曲线详解
开发语言·qt
Funny-Boy7 小时前
菱形继承原理
c++
揽你·入怀8 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言