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

运行结果

相关推荐
scx2013100410 分钟前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~19 分钟前
冒泡排序算法
c语言·开发语言·算法·排序算法
看到我,请让我去学习2 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779132 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
励志不掉头发的内向程序员3 小时前
STL库——string(类函数学习)
开发语言·c++
一百天成为python专家3 小时前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian3 小时前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
hqwest3 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
前路不黑暗@4 小时前
C语言:操作符详解(二)
c语言·开发语言·经验分享·笔记·学习·学习方法·visual studio
深盾科技4 小时前
Kotlin Data Classes 快速上手
android·开发语言·kotlin