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

运行结果

相关推荐
CodeCraft Studio6 小时前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
小超爱编程6 小时前
纯前端做图片压缩
开发语言·前端·javascript
曼巴UE58 小时前
UE5 音效系统
c++·游戏·ue5·虚幻·音效
KIDAKN8 小时前
java--怎么定义枚举类
java·开发语言
海天胜景8 小时前
C# 中常用的 字符串截取方法
开发语言·c#
无影无踪的青蛙9 小时前
[C++] list双向链表使用方法
c++·链表·list
tkevinjd10 小时前
C++中线程库的基本操作
开发语言·c++
CodeWithMe10 小时前
【C/C++】不同防止头文件重复包含的措施
c语言·开发语言·c++
Fre丸子_10 小时前
C++定长内存块的实现
c++
子豪-中国机器人10 小时前
C++ 信息学奥赛总复习题答案解析
开发语言·c++·算法