423,822 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,822 IT Pros & Developers. It's quick & easy.

Explaining the output for this code

Kindle Life 123
P: 7
#include<iostream.h>
#include<stdlib.h>
void main()
{
randomize();
char status[][10]={"OK","START","ACTION";}
int Turn=5;Trick;
for (int count =3; count>0; count--}
{
Trick =random(count);
cout<<Turn+Trick<<Status[Trick}<<'@';
}

Options:
1.5START@6START@7ACTION@
2.6START@6ACTION@6START
3.7OK@6START@5ACTION@
4.6START@5OK@5ACTION@
Jun 4 '18 #1
Share this Question
Share on Google+
2 Replies


P: 27
- random(value) generates a random number from 0 to 'value-1'.

Possible outcomes:

-When, count =3,
Trick can be 0, 1 or 2
Turn+Trick can be (i.e. 5+trick) 5, 6 or 7
Status[Trick] can be Status[0], Status[1] or Status[2] i.e. OK, START OR ACTION
Valid options: 1, 2, 3 and 4

-When count is 2
Trick can be 0 or 1
Turn+Trick can be 5 or 6
Status[Trick] can be OK or START
Valid options: 1, 3 and 4

-When count is 1
Trick will be 0
Turn+Trick will be 5
Status[Trick] can be OK only

Hmm.., but there seems no option matching with the predicted output. I believe there should be a OK either in the 3rd or 4th option (1st one has been eliminated because of having 7).
Jun 4 '18 #2

Rabbit
Expert Mod 10K+
P: 12,279
Also, that code looks like it has syntax errors so it's not going to run in the first place. Therefore, it won't produce any output
Jun 4 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.