Consider the following algorithm. How many times does a beep statement get execu
ID: 643920 • Letter: C
Question
Consider the following algorithm.
How many times does a beep statement get executed?
fori {1, 2, 3, 4, 5}
do beep forj {1, 2, 3}
do beep fork {1, 2, 3, 4}
do forl {1, 2, 3, 4, 5, 6}
do beep form {1, 2, 3, 4}
do beep Consider the following algorithm. for i epsilon {1, 2, 3, 4, 5} do beep for j epsilon {1, 2, 3} do beep for k epsilon {1, 2, 3, 4} do for / epsilon {1, 2, 3, 4, 5, 6} do beep for m epsilon {1, 2, 3, 4} do beep How many times does a beep statement get executed?Explanation / Answer
for
i is in {1, 2, 3, 4, 5}
do
beep // A
for
j is in {1, 2, 3}
do
beep // B
for
k is in {1, 2, 3, 4}
do
for
l is in {1, 2, 3, 4, 5, 6}
do
beep // C
for
m is in {1, 2, 3, 4}
do
beep // D
The outermost loop(loop variable: i) runs 5 times.
So, the beep in line A runs 5 times.
The beep in line B runs 3 times for every value of i.
So, total number of times beep in line B runs = 5 * 3 = 15.
The beep in line C runs 6 times for every value of k.
So, total number of times beep in line C runs = 5 * 4 * 6 = 120.
The beep in line D runs 4 times for every value of k.
So, total number of times beep in line C runs = 5 * 4 * 4 = 80.
Total number of times beep statement gets executed = 5 + 15 + 120 + 80 = 225