Int m, n;
Printf (" need to enter m rows value: ");
The scanf (" % d ", & amp; M);
Printf (" need to input n column value: ");
The scanf (" % d ", & amp; N);
Int arr [m] [n].
int i,j;
//loop input;
for(i=0; i
for(j=0; J
Printf (" please enter the first line % d % d column value: ", I + 1, j + 1);
The scanf (" % d ", & amp; Arr [I] [j]);
}
}
//loop output;
for(i=0; i
for(j=0; J
Printf (" % d \ t ", arr [I] [j]);
}
printf("\n");
}
Int b [100];
for(i=0; i
for(j=0; J
Printf (" % d ", arr [j] [I]);
}
}
CodePudding user response:
Int m, n;Printf (" need to enter m rows value: ");
The scanf (" % d ", & amp; M);
Printf (" need to input n column value: ");
The scanf (" % d ", & amp; N);
Int arr [m] [n].
int i,j;
//loop input;
for(i=0; i
for(j=0; J
Printf (" please enter the first line % d % d column value: ", I + 1, j + 1);
The scanf (" % d ", & amp; Arr [I] [j]);
}
}
//loop output;
for(i=0; i
for(j=0; J
Printf (" % d \ t ", arr [I] [j]);
}
printf("\n");
}
Int b [100];
for(j=0; J
for(i=0; i
Printf (" % d ", arr [j] [I]);
}
}
CodePudding user response:
The final output of the loop is wrong, both inside and outside layer cyclic changeCodePudding user response:
Define an array, the array dimensions must be a constant expression, because m and n are variable, soint arr [m] [n].
Code needs to be modified, use the new keyword to dynamic allocation,
# include & lt; Stdio. H>
Int main ()
{
Int m, n;
Printf (" need to enter m rows value: ");
The scanf (" % d ", & amp; M);
Printf (" need to input n column value: ");
The scanf (" % d ", & amp; N);
//int arr [m] [n].
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the following code for the dynamic allocation, pointer to a pointer, can be regarded as a two-dimensional array -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Int arr=new int * * * [m].
Int I, j;
For (I=0; i{
Arr [I]=new int [n].
}
//loop input;
For (I=0; i{
For (j=0; J{
Printf (" please enter the first line % d % d column value: ", I + 1, j + 1);
The scanf (" % d ", & amp; Arr [I] [j]);
}
}
//loop output;
For (I=0; i{
For (j=0; J{
Printf (" % d \ t ", arr [I] [j]);
}
printf("\n");
}
Int b [100];
For (I=0; i{
For (j=0; J{
Printf (" % d \ t ", arr [j] [I]);//add \ t -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
}
printf("\n");//-- -- -- -- -- -- -- -- -- -- -- -- -- add a line break -- -- -- -- -- -- -- -- -- -- --
}
//-- -- -- -- -- -- -- -- -- -- -- - the following code for the release of the dynamically allocated space -- -- -- -- -- -- -- -- -- -- -- -- --
The delete [] * arr;
The delete [] arr;
return 0;
}
VS2015 debugging results as shown in the following
For the use of a two-dimensional array, please refer to "the use of two dimensional array in c + +" and "two dimensional array c + +"
Wish I could help you!
CodePudding user response:
Release spacewith the following code
for (I=0; i{
The delete [] arr [I];
}
The delete [] arr;
CodePudding user response:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Description: dynamic application of two-dimensional array
Author: wowpH
Csdnid: pfdvnah
Date: 2019-11-9 15:38:25
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include & lt; Stdio. H>
#include
Int main (void) {
Int the row, col.
Printf (" input rows and columns: ");
Scanf_s (" % d % d ", & amp; The row, & amp; Col);
Int * * matrix=NULL;//initialized to NULL
//remove C6085 and C6086 warning
If (row & lt;=0 | | col & lt;=0) {
Printf (" the number of rows or columns illegal! \n");
exit(-1);
}
//dynamic application memory
Matrix=(int) * * malloc (row * sizeof (int *));
//remove C6011 warning
If (NULL==matrix) {
Printf (" can't dynamic application memory! \n");
exit(-1);
}
for (int i=0; iMatrix [I]=(int *) malloc (col * sizeof (int));
//remove C6011 warning
If (NULL==matrix [I]) {
Printf (" can't dynamic application memory! \n");
exit(-1);
}
}
//to two-dimensional array assignment
for (int i=0; iFor (int j=0; J & lt; Col. + + j) {
Matrix [I] [j]=I * col + j;
}
}
//output two-dimensional array
for (int i=0; iFor (int j=0; J & lt; Col. + + j) {
Printf (" % 2 d ", matrix [I] [j]);
Putchar ((j & lt; Col - 1)? "' : '\ n');
}
}
//release the memory
for (int i=0; iFree (matrix [I]);
}
Free (matrix);
return 0;
}
CodePudding user response: