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

need explanation for its output

P: 2
(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)?Last+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
1 Week Ago #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,186
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.
1 Week Ago #2

P: 2
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
1 Week Ago #3

weaknessforcats
Expert Mod 5K+
P: 9,186
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.
1 Week Ago #4

Post your reply

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