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

本文转自:http://blog.youkuaiyun.com/zhy_cheng/article/details/10813573
[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <ctime>  
  3. #include <cstdlib>  
  4. using namespace std;  
  5.    
  6. void oneArray()  
  7. {  
  8.        
  9.     int size=rand()%10;  
  10.     int* pint=new int[size];  
  11.     for(int i=0;i<size;++i)  
  12.     {  
  13.         pint[i]=rand()%100;  
  14.     }  
  15.     cout<<"one array is "<<endl;  
  16.     for(int i=0;i<size;++i)  
  17.     {  
  18.         cout<<pint[i]<<"\t";  
  19.     }  
  20.     cout<<endl;  
  21.     delete []pint;  
  22. }  
  23.    
  24. void twoArray()  
  25. {  
  26.     int row=rand()%10;  
  27.     int column=rand()%10;  
  28.     cout<<"row is "<<row<<" column is "<<column<<endl;  
  29.     cout<<"two array is"<<endl;  
  30.     int** p=new int*[row];  
  31.     for(int i=0;i<row;++i)  
  32.     {  
  33.         p[i]=new int[column];  
  34.     }  
  35.     for(int i=0;i<row;++i)  
  36.     {  
  37.         for(int j=0;j<column;++j)  
  38.         {  
  39.             p[i][j]=rand()%100;  
  40.         }  
  41.     }  
  42.        
  43.     for(int i=0;i<row;++i)  
  44.     {  
  45.         for(int j=0;j<column;++j)  
  46.         {  
  47.             cout<<p[i][j]<<"\t";  
  48.         }  
  49.         cout<<endl;  
  50.     }  
  51.     cout<<"delete array"<<endl;  
  52.     for(int i=0;i<row;++i)  
  53.     {  
  54.         delete []p[i];  
  55.     }  
  56.     delete []p;  
  57. }  
  58. void threeArray()  
  59. {  
  60.     cout<<"three array"<<endl;  
  61.     int height=rand()%10;  
  62.     int row=rand()%10;  
  63.     int column=rand()%10;  
  64.     cout<<"height is "<<height<<" row is "<<row<<" column is "<<column<<endl;  
  65.     int*** p=new int**[height];  
  66.     for(int i=0;i<height;i++)  
  67.     {  
  68.         p[i]=new int*[row];  
  69.     }  
  70.     for(int i=0;i<height;++i)  
  71.     {  
  72.         for(int j=0;j<row;++j)  
  73.         {  
  74.             p[i][j]=new int[column];  
  75.         }  
  76.     }  
  77.    
  78.     for(int i=0;i<height;++i)  
  79.         for(int j=0;j<row;++j)  
  80.             for(int k=0;k<column;++k)  
  81.             {  
  82.                 p[i][j][k]=rand()%100;  
  83.             }  
  84.    
  85.     for(int i=0;i<height;++i)  
  86.     {  
  87.         for(int j=0;j<row;++j)  
  88.         {  
  89.             for(int k=0;k<column;++k)  
  90.             {  
  91.                 cout<<p[i][j][k]<<"\t";  
  92.             }  
  93.             cout<<endl;  
  94.         }  
  95.         cout<<endl<<endl;  
  96.     }  
  97.     cout<<"delete three array"<<endl;  
  98.     for(int i=0;i<height;++i)  
  99.     {  
  100.         for(int j=0;j<row;++j)  
  101.         {  
  102.             delete []p[i][j];  
  103.         }  
  104.     }  
  105.     for(int i=0;i<height;i++)  
  106.     {  
  107.         delete []p[i];  
  108.     }  
  109.     delete []p;  
  110. }  
  111.    
  112.    
  113. void main()  
  114. {  
  115.     srand(unsigned(time(NULL)));  
  116.     oneArray();  
  117.     twoArray();  
  118.     threeArray();  
  119.     int aa;  
  120.     cin>>aa;  
  121. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值