Explaining the output for this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kindle Life 123
    New Member
    • May 2018
    • 7

    Explaining the output for this code

    #include<iostre am.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+Tric k<<Status[Trick}<<'@';
    }

    Options:
    1.5START@6START @7ACTION@
    2.6START@6ACTIO N@6START
    3.7OK@6START@5A CTION@
    4.6START@5OK@5A CTION@
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    - 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).

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      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

      Comment

      Working...