Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C++ generates a dynamic two-dimensional array through dynamic memory allocation, which needs to multiply rows and columns into even numbers.
C++ generates a dynamic two-dimensional array through dynamic memory allocation, which needs to multiply rows and columns into even numbers.
# Contains? " stdio.h "

# Contains? & ltstdlib.h & gt

# Contains? "Time. h"

int? main(int? argc,char? *argv[]){

int? m,n,I,j,**p,* q;

Srand ((unsigned) time (empty));

m = rand()% 10+ 1;

while(m *(n = rand()% 10+ 1)& amp; 1); //Make sure that m*n is an even number.

q=(int? *)malloc(sizeof(int)* m * n);

if(q==NULL? ||? (p=(int? **)malloc(sizeof(int? *)*m))==NULL){

Printf ("Application? Memory? Failed ... \ n ");

Return? 0;

}

for(I = 0; I & ltm;; p[I]= q+n * i++); //organized into a two-dimensional array

for(I = 0; I & ltm;; i++){

for(j = 0; j & ltn; j++)

printf("%2d ",p[I][j]= rand()% 5+ 1);

printf(" \ n ");

}

Free (q);

Free (p);

Return? 0;

} running example: