Problem Statement - Random Number Based Guessing of output of a 2D Array printing
Look at the following C++ code and find the possible output(s) from the
options (i) to (iv) following it. Also, write the maximum values that can
be assigned to each of the variables N and M.
Note:
Assume all the required header files are already being included in
the code.
The function random(n) generates an integer between 0 and n-1
randomize(); //This allows the code to run random function
int N=random(3),M=random(4);
Here N can contain value 0,1,2 and M can contain value 0,1,2,3
int DOCK[3][3] = {{1,2,3},{2,3,4},{3,4,5}};
This is 3×3 two dimensional array declaration along with initial values
for(int R=0; R<N; R++) { //This is external loop for counting rows up to maximum of N
for(int C=0; C<M; C++) //This is internal loop for counting columns up to maximum of M cout<<DOCK[R][C]<<” “; // This prints the array member cout<<endl; }//This allows to go to next row
Watch that due to maximum possible value of row count which is 2 any possible output can not go above 2 rows so options (ii) and (iii) are correct and (i) and (iv) are incorrect as they are three row outputs.
Special Note:
While testing the actual output of this program one may get 0 row output (no output) 1 row output or two row output with 0 columns, 1 columns, 2 columns or 3 columns
So please be prepared to see nothing as output. Once may have to run this program again and again to get desired output.
Common Errors
Learners are advised to remember that outer loop is always for row printing and inner loop is for column printing in a row major array.