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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhy_cheng/article/details/10813573
#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;
}

猜你喜欢

转载自blog.csdn.net/zhy_cheng/article/details/10813573