Sie sind auf Seite 1von 4

Computer Science Practice Assignment: random( ) Function

S.NO. Q1.

Sub Part (a) Study the following program and select the correct option(s) out of the four choices given below as the possible set of such numbers generated from the program code. Justify your answer. 2 #include #include const int MIN = 25 ; void main( ) { randomize ( ); int SCORE = 10 , Num; for (int i = 1 ; i <= 4 ; i ++ ) { Num = MIN + random (SCORE ); cout << Num << ":"; } } (i) 34:31:30:33: (ii) 29:33:30:31: (iii) 34:31:30:31: (iv) 34:31:29:33: (b) The following code is from a game which generates a set of 4 random numbers. Praful is playing this game,help him to identify the correct option(s) out of the four choices given below as the possible set of such numbers generated from the program code so that he wins the game. Justify your answer. Note: Assume all the necessary header files are included. const int LOW=25; void main() { randomize(); int point =5,number; for(int i=1;i<=4;i++) { number= LOW+ random(point); cout<<number<<:; point--; } } (i) 29:26:25:28: (ii) 24:28:25:26: (iii) 29:26:24:28: (iv) 29:26:25:26:

Marks (2)

(2)

Nita Arora, KHMS, Ashok Vihar

Computer Science Practice Assignment: random( ) Function


(c) Consider the following program code. Select the expected output(s) from the options(i) to (iv) #include<iostream.h> #include<stdlib.h> void main() { clrscr(); int N=20,guess; randomize(); for(int i=1;i<=3;i++) { guess=random(N-10)+10; cout<<guess<<" "; } } (d) i) 9 12 14 ii) 14 12 17 iii) 12 14 11 15 iv) 11 14 22 Mark the correct answer(s) with justification # include<iostream.h> # include<stdlib.h> # include<conio.h> void main() { randomize(); int num,rndnum; num=7; rndnum=random(num)+7 ; for(int n=1;n<=rndnum;n++) cout<<n<<" "; getch(); } 3 3

i) 1 2 3 4 5 6 7 8 ii) 1 2 3 4 5 6 7 8 9 10 11 12 13 iii) 0 1 2 3 4 5 6 7 8 9 iv) 1 2 3 4 5 6 7 8 9 10 11 12 13 14

(e)

In the following program, find the correct possible output(s) from the options: #include<stdlib.h> #include<iostream.h> void main() { randomize(); char City[][10]={DEL,CHN,KOL,BOM,BNG}; int Fly; for(int I=0;I<3;I++)

(2)

Nita Arora, KHMS, Ashok Vihar

Computer Science Practice Assignment: random( ) Function


{ Fly=random(2)+I; cout<<City[Fly]<<:; } } Outputs: i) DEL: ii) CHN: iii) KOL: iv) KOL: (f)

CHN: KOL: BOM: CHN:

KOL: CHN: BNG: KOL: (2)

Consider the following program code. Select the expected output from the options(i) to (iv) Also justify your answer. # include <iostream.h> # include <stdlib.h> void main( ) { randomize(); int dice = 6, res; res = random(dice) + 1; for (int i = 1 ; i<res ; ++i) cout << i << ; } (i ) 0 1 2 3 4 (iii) 2 3 4 ii) 1 2 3 4 5 iv) 0 1 2

(g)

Read the following program and select the possible output and explain also. #include<iostream.h> #include<stdlib.h> const int max = 3; void main() { randomize(); int number; number = 50 + random (max); for(int p=number ; p>=50 ; p--) cout<<p<<#; cout<<endl; } (a) 53#52#51#50# (c) 50#51# (b) 50#51#52# (d) 51#50#

(2)

(h)

Observe the following program SCORE.CPP carefully, if the value of Num entered by the user is 8, choose the correct possible output(s) from the options from (i) to (iv) and justify your option . //program : SCORE.CPP #include<stdio.h>

(2)

Nita Arora, KHMS, Ashok Vihar

Computer Science Practice Assignment: random( ) Function


#include<iostream.> void main( ) { randomize ( ) ; int num , rndnum ; cin>>num ; rndnum=random( num ) + 5; for(int n =1 ; n < = rndnum ; n ++ ) cout<<n<< ; } output options : i) 1 2 3 4 5 6 7 iii) 1 2 3 4 5 6 7 8 9 (i)

ii ) 1 2 iv ) 1 2 3 4 5 6 (2)

What will be the possible output of the following function, also give reasons: void main() { randomize( ); int x=random(3)+1; for(int i=0;i<=x;i++) cout<<i+2<<,; } (i) 0,1,2,3,4,,5 (ii) 1,2,3,4,5 (iii) 2,3,4,5 (iv) 2,3,4,5,6,

(j)

Select the correct output(s) from the options (i) to (iv). Also justify your answer. (Assume all header files are included) void main( ) { randomize( ); int a[ ] = {10,20,30,40,50}; int x = random(3)+1; for(int i=0;i<=x;i++) { cout<< a[i]; } } i) 10,20,30,40,50 ii) 10,20,30 iii) 20,30,40 iv)10,20,30,40

(2)

Nita Arora, KHMS, Ashok Vihar

Das könnte Ihnen auch gefallen