C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 22)
22.
What will be the output of the following program?
#include<iostream.h> 
class TestDrive
{
    int x; 
    public:
    TestDrive(int xx)
    {
        x = xx;
    }
    int DriveIt(void);
};
int TestDrive::DriveIt(void)
{
    static int value = 0;
    int m;
    m = x % 2;
    x = x / 2;
    if((x / 2)) DriveIt(); 
    value = value + m * 10; 
    return value;
}
int main()
{
    TestDrive TD(1234);
    cout<< TD.DriveIt() * 10 << endl;
    return 0; 
}
300
200
Garbage value
400
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Vaishnavi said:   7 years ago
It's a recursive function called.

Step1
m=1234%2=0
x=1234/2=617
if(617/2)// called function again just follow by steps(x not change here)
value=40;

Step2
m=617%2=1
x=617/2=308
if(308/2)//called
value=40;

Step3 x=308.
m=0
x=154
if(154/2)//called
value=30;

Step4 x=154
m=0
x=77
if(77/2)//called
value=30;

Step5 x=77
m=1
x=38
if(38/2)//called
value=30;

Step6 x=38
m=0
x=19
if(19/2)//called
value=20;

Step7 x=19
m=1
x=9
if(9/2)//called
v=20;

Step8 x=9
m=1
x=4
if(4/2)//called
v=10;

Step9 x=4
m=0
x=2
if(2/2)//called
value=0;

Step10 x=2
m=o
x=1
if(1/2)//called
value=value+m*10// solve as per m value by order of reverse steps 10,9,8,7,6,5,4,3,2,1
value=0;

After solving recursive funtion call came back in cout,
Then TD.DriveIt()*10;
40*10 = 400.
(1)

Tanisha said:   1 decade ago
Please explain this driveit() is called again then how these values are changing?

Jeremy Lee said:   1 decade ago
m = 0; x = 617.
m = 1; x = 308.
m = 0; x = 154.
m = 0; x = 77.
m = 1; x = 38.
m = 0; x = 19.
m = 1; x = 9.
m = 1; x = 4.
m = 0; x = 2.
m = 0; x = 1.

Value = 0; m = 0; x = 1.
Value = 0; m = 0; x = 1.
Value = 10; m = 1; x = 1.
Value = 20; m = 1; x = 1.
Value = 20; m = 0; x = 1.
Value = 30; m = 1; x = 1.
Value = 30; m = 0; x = 1.
Value = 30; m = 0; x = 1.
Value = 40; m = 1; x = 1.
Value = 40; m = 0; x = 1.

= 40*10 = 400.

Aish said:   1 decade ago
How you got the value 4?

Manj said:   1 decade ago
Please can anyone explain the problem. I don't understood the above answer. !

Dhivya said:   1 decade ago
Can anyone explain how the output is 400?

Asmita said:   1 decade ago
How the value = value + m * 10 statement will be called repeatedly? please explain.

Musheer said:   1 decade ago
Value = value + m * 10;

Value = 0 + 1 * 10; --> value= 10.

Value = 10 + 0 * 10; --> value = 10.

Value = 10 + 0 * 10; --> value = 10.

Value = 10 + 1 * 10; --> value = 20.

Value = 20 + 0 * 10; --> value = 20.

Value = 20 + 1 * 10; --> value = 30.

Value = 30 + 1 * 10; --> value = 40.

Value = 40 + 0 * 10; --> value = 40.

Value = 40 + 0 * 10; --> value = 40.

Then,

TD.DriveIt()*10;

40 * 10; ---> 400.

Chandru said:   1 decade ago
@Anurag Patil.

Your concept is not fair because in main.

cout<< TD.DriveIt() * 10 << endl;

We have to multiply the return value with 10.

Its solved by Recursion method.

Anurag patil said:   1 decade ago
value = value + m * 10;
4+m*10;
4+10*10=400.
Range of value=4.

Post your comments here:

Your comments will be displayed after verification.