Consider the following functions: int secret (int x) { int i, j; i = 2 * x; if (
ID: 3651580 • Letter: C
Question
Consider the following functions:int secret (int x)
{
int i, j;
i = 2 * x;
if (i > 10)
j = x / 2;
else
j = x / 3;
return j - 1;
}
int another(int a, int b)
{
int i, j;
j = 0;
for (i = a; i <= b; i++)
j = j + i;
return j;
}
Here are my answers based on the following program segments (Assume that x, y, and k are int variables.)
b. x = 5; y = 8;
cout << another(x, y) << endl;
ANSWER: 26
c. x = 10; k = secret(x);
cout << x << " " << k << " " << another(x, k) << endl;
ANSWER: 10 4 0
d. x = 5; x = 8;
cout << another(y, x) << endl;
ANSWER: 0
I have the answers, but for each of these, please explain to me how these are the answers so I can get a better understanding of this problem.
Explanation / Answer
For x=5,y=8 when you call the another function... for the first time it goes into the loop,for i=5,j=5+0=5. for the second time,for i=6,j=6+5=11 for the third time,for i=7,j=7+11=18 for fourth time,for i=8 and this is last time,j=8+18=26 And finally returns a value of 26 ////////////////////////////////////////////////////////// for secret(x) where x=10,i=2*10=20 (>10) =>j= x/2= 10/2 =5 return j-1 = 5-1 =4 you can find another(10,4).you would get a value of 0 //////////////////////////////////////////////////////////
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.