<代码收藏>动态生成数组

代码收藏 专栏收录该内容
5 篇文章 0 订阅
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
 
void oneArray()
{
     
    int size=rand()%10;
    int* pint=new int[size];
    for(int i=0;i<size;++i)
    {
        pint[i]=rand()%100;
    }
    cout<<"one array is "<<endl;
    for(int i=0;i<size;++i)
    {
        cout<<pint[i]<<"\t";
    }
    cout<<endl;
    delete []pint;
}
 
void twoArray()
{
    int row=rand()%10;
    int column=rand()%10;
    cout<<"row is "<<row<<" column is "<<column<<endl;
    cout<<"two array is"<<endl;
    int** p=new int*[row];
    for(int i=0;i<row;++i)
    {
        p[i]=new int[column];
    }
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<column;++j)
        {
            p[i][j]=rand()%100;
        }
    }
     
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<column;++j)
        {
            cout<<p[i][j]<<"\t";
        }
        cout<<endl;
    }
    cout<<"delete array"<<endl;
    for(int i=0;i<row;++i)
    {
        delete []p[i];
    }
    delete []p;
}
void threeArray()
{
    cout<<"three array"<<endl;
    int height=rand()%10;
    int row=rand()%10;
    int column=rand()%10;
    cout<<"height is "<<height<<" row is "<<row<<" column is "<<column<<endl;
    int*** p=new int**[height];
    for(int i=0;i<height;i++)
    {
        p[i]=new int*[row];
    }
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<row;++j)
        {
            p[i][j]=new int[column];
        }
    }
 
    for(int i=0;i<height;++i)
        for(int j=0;j<row;++j)
            for(int k=0;k<column;++k)
            {
                p[i][j][k]=rand()%100;
            }
 
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<row;++j)
        {
            for(int k=0;k<column;++k)
            {
                cout<<p[i][j][k]<<"\t";
            }
            cout<<endl;
        }
        cout<<endl<<endl;
    }
    cout<<"delete three array"<<endl;
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<row;++j)
        {
            delete []p[i][j];
        }
    }
    for(int i=0;i<height;i++)
    {
        delete []p[i];
    }
    delete []p;
}
 
 
void main()
{
    srand(unsigned(time(NULL)));
    oneArray();
    twoArray();
    threeArray();
    int aa;
    cin>>aa;
}

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值