3. Consider the following functions: int func1(int x) { int r, s; r = 2 * x; if
ID: 3532542 • Letter: 3
Question
3. Consider the following functions:
int func1(int x)
{
int r, s;
r = 2 * x;
if (r > 10)
{
s = x / 2;
}
else
{
s = x / 3;
}
return s - 2;
}
int func2(int a, int b)
{
int r, s;
s = 0;
for(r = a; r < b; r++)
{
s++;
}
return s;
}
What is the output from the following program fragments?
int a, b;
a. a = 10;
cout << func1(a) << endl;
b. a = 5; b = 12;
cout << func2(a, b) << endl;
c. a = 8;
b = func1(a);
cout << a << " " << b << " " << func2(a, b) << endl;
Explanation / Answer
/*******please rate with 5 stars******/
a. 4.
//explanation:
fun1(10)==> r= 2*10=20==> s=20/3 =6 return value = 6-2 = 4;
b. 7. //explanation : fun2(5,12)==> s=s+1 for 7 times(i.e, 12-5) so output=7.
c. 8 3 0 //explanation:
fun1(8)==> r=2*8=16==> s=16/3=5 so b=5-2=3.
now fun2(8,3) ==> s=s+1 for negative time (i.e, 3-8) which is not possible , so output = 0(i.e, initial value of s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.