need explanation for its output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kavitha555
    New Member
    • Nov 2018
    • 2

    need explanation for its output

    (a) Find and write the output of the following C++ program code:Note: Assume all required header files are already included in the program.
    void Revert(int &Num, int Last=2)
    {
    Last = (Last%2==0)?Las t+1: Last-1;
    for(int C=1; C<=Last; C++)
    Num+=C;
    }
    void main()
    {
    int A=20, B=4;
    Revert(A,B);
    cout<<A<<”&”<<B <<endl;
    B--;
    Revert(A,B);
    cout<<A<<”#”<<B <<endl;
    Revert(B);
    cout<<A<<”#”<<B <<endl;
    }

    ans:
    35&4
    38#3
    38#9
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What is your question exactly?

    Usually where you need to see how code works is to compile and link the code to get an executable and then step through the code with a debugger. This will show your outputs and the state of all of your variables.

    Short of that write down on paper the path of execution.

    Comment

    • kavitha555
      New Member
      • Nov 2018
      • 2

      #3
      i am getting a different answer while substituting the values for the var. when i compiled the pgm this is the ans which i got is
      35&4
      38#3
      38#9. so can any one help me with an explanation for tht

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I compiled the code and ran it and got the same answer as in your Post #1.

        Since you can compile the code and run it, you should be able to step through the code using your debugger. You will be able to see exactly how Revert functions.

        If you don't know how to use your debugger, this is an excellent opportunity to learn how. Your debugger is your best friend.

        Comment

        Working...