By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,583 Members | 3,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

Explaining the output for this code

Kindle Life 123
P: 7
void main()
char status[][10]={"OK","START","ACTION";}
int Turn=5;Trick;
for (int count =3; count>0; count--}
Trick =random(count);

Jun 4 '18 #1
Share this Question
Share on Google+
2 Replies

P: 120
- 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

Expert Mod 10K+
P: 12,366
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.