In the program, can you explain why y = 108 when compiled please?: #include<iost
ID: 3730000 • Letter: I
Question
In the program, can you explain why y = 108 when compiled please?:
#include<iostream>
#include<cmath>
using namespace std;
double Times2(double);
double Times3(double);
int main(void)
{double x = 4.0, y = 6.0,z;
z = Times2(x);
x = Times3(x);
y = Times3(Times2(x)+Times2(y));
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
cout<<"z= "<<z<<endl;
return 0;
}
double Times2(double x)
{double y = 2*x;
return y;
}
double Times3(double y)
{double x = 3*y;
return x;
}
Explanation / Answer
Explanation:
We can see here y = Times3(Times2(x)+Times2(y));
so first we need to look at what is the value of x here
we have initially x = 4.0
then x = Times3(x);
Times3(x) returns the value x = 3*4 = 12
So, value of x is 12.0
Now we have inital value of y as 6.0
so, y = Times3(Times2(x)+Times2(y)); will be evaluated as:
Times2(x) where x = 12.0 will return value 24
Times2(y) where y = 6.0 will return value 12
so, the parameter value passed in Times3( ) will be 36 i.e.(Times2(x)+Times2(y)) = (24 +12)
So, y = Times3(36) will return value 108
Hence final value of y is 108
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.