Random number

This is for my exams.

Answer is 19:16:15:16
PLease help me to understand how to get this answer

The following code is from a game, which generates a set of 4 random numbers.
Yallav 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.

#include <iostream.h>
#include <stdlib.h>
const int LOW=15;
void main ( )
{
randomize( ) ;
int POINT=5, Number;
for (int 1=1;I<=4;I++)
{
Number=LOW+random(POINT) ;
cout<<Number<<“:” ;
POINT--;
}
}
Your program should not compile. It is ridden with errors.
#include <iostream.h> ←should not work
#include <stdlib.h> Use cstdlib in C++
void main ( )Invalid in C++
randomize( ) ;What that function is?
int 1=1;invalid statement
random(POINT) ;random()? what is it?
Last edited on
This is one question in Board Exam.
maybe he is using turbo c++ 3.1 .the fuest three errors are not errors in turbo c++.
Last edited on
Well, I would run away from school which teaches that.
Answer is 19:16:15:16
from context of your code, I would say that there cannot be set output (because it is random).
I say that possible range of output depend of random() function definition (inclusive/exclusive, from 0/from 1).
If it is generates-from-0-to-parameter-exclusive then minimum number which can be generated is LOW and maximum is LOW+POINT-1.
So your example output can in theory be generated.
I just want to know why there is no possibility for the answer 19:16:15:18
There is possibility to have output of 19:16:15:18. All numbers are in renge 15-19, so it is completely legal output.
but in exam sample paper 19:16:15:18 is wrong answer and the correct answer is 19:16:15:16
Then you need to provide what the functions randomize(); and random(POINT); are defined as. Otherwise, we're going to assume that it's completely random and there is no way to tell what the possible outputs are.
this is the complete question in the paper and the have given four choices to select.
Then I would ask your instructor how you could possibly know since both answers are valid. As long as the four numbers are between 15 and 20 (maybe other numbers, but it's hard to tell), it is a valid answer according to the given information. Without any other information, there is no feasible way to differentiate between two potentially correct answers.

Edit: MiiNiPaa got it right in the below post. This is why it's so important for you to post your code within the [code][/code] tags.
Last edited on
Ow, damn, i didn't noticed POINT--. Well. Initial possible range of generated numbers is 15-19. Each next number have max range 1 less. So possible max values — 19:18:17:16
one more random question.
In the following program, if the value of Guess entered by the user is 65, what will be the expected output(s) from the following options (i), (ii), (iii) and (iv)?

#include <iostream.h>
#include <stdlib.h>
void main()
{
int Guess;
randomize();
cin>>Guess;
for (int I=1;I<=4;I++)
{
New=Guess+random(I);

cout<<(char)New;
}
}
Options are:
(i) ABBC
(ii) ACBA
(iii) BCDA
(iv) CABD
Ugh. Another non-compiling code.

But answer in your case is (i)
answer is correct. How you got tht answer?
value of Guess entered by the user is 65
65 is ASCII code of A
1
2
for (int I=1;I<=4;I++){
New=Guess+random(I);
Eanch letter has random number in [0;I] addad to it.
So first letter can be only A, second one A-B, third one A-B-C and so on.
Thank you very much. doubts cleared now very well.
Topic archived. No new replies allowed.