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;
}

运行结果

相关推荐
hdsoft_huge6 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘6 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务7 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
混分巨兽龙某某7 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
小冯记录编程7 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther8 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_8 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan8 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L8 小时前
MFC仿真
c++·mfc
阿幸软件杂货间9 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#