q15) CONSIDER THE FOLLOWING CODE: void Question() { int x = 12; int y = 12; doub
ID: 3810043 • Letter: Q
Question
q15)
CONSIDER THE FOLLOWING CODE:
void Question()
{
int x = 12;
int y = 12;
double sum = y + x;
for (int i = 1; i < 7; i++)
{
if (i % 2 == 1)
sum += 4 * y;
else
sum += 2 * y;
}
double Area = (y - x) / 3 * sum;
cout << "The area under the curve is : " << Area << endl;
}
Select one:
a. The area under the curve is : 24
b. The area under the curve is : 0
c. The area under the curve is : 10
d. The area under the curve is : 144
please explain how you get the answer. Thank you
Explanation / Answer
the answer is b.The area under the curve is : 0
in the Question, x=12,y=12, sum=x+y, therefore sum=24
then for loop, this loop which will be executed for 6 times.
inside the for loop, in the if condition, we checking for i % 2 == 1,that is dividing counter variable by 2 and
checking for remainder 1, if this condition is true,that is remainder is 1,then we are doing sum=sum+4 *y,
otherwise, that is,if the remainder is not equal to 1, then we are doing sum=sum+2*y
after exiting from the loop, variable sum will have some value(sum=240). Then area is calculated by the (y-x)/3*sum formula,
since x and y are equal, x-y in the formula gives zero, therefore result is zero.
then result is printed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.