Output writing – Possible Random Numbers – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Exam Questions-CBSE12A-2018-01F #CPP#5224    siteicon   siteicon  

Problem Statement - Output writing – Possible Random Numbers

Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the highest and lowest values that can be assigned in the array A.
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.

 

void main()
{
  randomize();
  int A[4], C;
  for(C=0; C<4; C++)
  A[C]=random(C+1)+10;
  for(C=3; C>=0; C--)
  cout<<A[C]<<"@";
}

 

(i)   13@10@11@10@             (ii)   15$14$12$10$
(iii) 12@11@13@10@             (iv)  12@11@10@10@

Solution

TC++ #5224

Possible outputs could be
(i)  13@10@11@10@
and
(iv) 12@11@10@10@

A (Min)  = 10
A (Max) = 13

 

Notes

randomize();
This will allow random number generation process.

int A[4], C;
Integer array A with four members and and a counter variable C has been declared.

for(C=0; C<4; C++) //This will handle the array in forward order
A[C]=random(C+1)+10; 
Since value of C would be 0 to 3 in increasing order the possible random values on R.H.S could be 10 to 13. When it is 0 number could be 1o, when it is 3 the number could be 10/11/12/13 on R.H.S. . So the array would be filled by numbers from 10 to 13. The possible value chart could be

index 0  |  index 1  | index 2      | index 3
10,            10/11,     10/11/12,    10/11/12/13

for(C=3; C>=0; C–)   cout<<A[C]<<“@”;
This will print the possible values in the array but from the last member (at index 3)to the first member at index 0. Based on possible value chart only option (i) and option (iv) are possible. Remember that you are reading array from last to the beginning.

  • Remember that random number generation is 1 less than the maximum value passed to random function. So random(1) will be 0, random(2) would be 0 or 1, random(3) would 0, 1 or 2, random(4) would 0, 1, 2 or 3

Common Errors

  • Student may forget that while printing the array is being read in the reverse order.


Share

CSKC| Created: 3-Dec-2018 | Updated: 4-Dec-2018|CBSE12A-2018









×
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


Back