Random Number Based Guessing of output of a 2D Array printing – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Exam Questions-CBSE12A-2017-01F #CPP#5322    siteicon   siteicon  

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.


  • 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
void main()
  int N=random(3),M=random(4);
  int DOCK[3][3] = {{1,2,3},{2,3,4},{3,4,5}};
  for(int R=0; R<N; R++)
    for(int C=0; C<M; C++)
      cout<<DOCK[R][C]<<" ";

1 2 3
2 3 4
3 4 5

1 2 3
2 3 4

1 2
2 3

1 2
2 3
3 4


TC++ #5322

Option (ii) and option (iii) are possible outputs

Maximum value for N = 2
Maximum value for N = 3



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.


CSKC| Created: 7-Dec-2018 | Updated: 7-Dec-2018|CBSE12A-2017

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues