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

运行结果

相关推荐
艾莉丝努力练剑2 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
还债大湿兄4 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜36 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian6 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼7 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上7 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang7 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc7 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇7 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀7 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc