Plz, if you do not know what u are doing do not answer this question. I do have
ID: 3589386 • Letter: P
Question
Plz, if you do not know what u are doing do not answer this question. I do have a clue of what is going on and will not give you a good rating just bc you answered it. I am trying to check my answer.
Trace the for loops below in the table to the right. If the loop is an infinite loop, trace three iterations and write “infinite loop”.
a) int sum = 0;
for (int count = 3; count >= 17; count = count + 4)
{
sum = sum + count;
}
b) int sum = 0;
for (int index = 1; index < 5; ++index)
{
sum = sum + (1 /index);
}
c) int product = 2;
for (int count = 0; product > 0; ++count)
{
product = product * product;
}
Explanation / Answer
a) int sum = 0;
for (int count = 3; count >= 17; count = count + 4)
{
sum = sum + count;
}
Answer: sum will be 0
Explanation:
As 3 >= 17, condition will fail and control will not go inside the loop so sum will remain as 0
b) int sum = 0;
for (int index = 1; index < 5; ++index)
{
sum = sum + (1 /index);
}
Answer: sum will be 1 at end of loop see explanation below
Explanation:
index = 1 , sum = 0 + 1 = 1 => sum = 1
index = 2 , sum = 1 + 0 = 1 => sum = 1 , because index is integer so 1/2 = 0
index = 3 , sum = 1 + 0 = 1 => sum = 1 , because index is integer so 1/3= 0
index = 4 , sum = 1 + 0 = 1 => sum = 1 , because index is integer so 1/4 = 0
index = 5 control will come out of loop
c) int product = 2;
for (int count = 0; product > 0; ++count)
{
product = product * product;
}
Answer: product = 4 , product = 16 , product = 256 ...infinite loop, see explanation below
Explanation:
count = 0 , product = 2*2 = 4 => product = 4
count = 1 , product = 4*4 = 16 => product = 16
count = 2 , product = 16*16 = 16 => product = 256
.. .. . .. . ..
Keep going on and On , Its infinite loop
Thanks, please RATE if you think its helpful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.