Ask your own question, for FREE!
Mathematics 8 Online
OpenStudy (anonymous):

For the algorithm below, compute the number of multiplications performed, as well as the final value of s. p ← 1 s ← 0 for i is in {1, 2, 3, 4} do p ← p · 3 s ← s + p number of multiplications final value of s s =

OpenStudy (anonymous):

@oldrin.bataku

OpenStudy (anonymous):

The for loop construct will go through \(i=1,2,3,4\) so our loop code will occur \(4\) times, meaning we multiply \(4\) times since each iteration does \(p\leftarrow p\cdot3\)

OpenStudy (anonymous):

First, consider that iteration, \(p\) is raised to a higher power of the base \(3\). At initialization time, we have \(p=1\). In the first iteration, we get \(p=3^1\), in the second we get \(p=3^2\), etc. giving us \(p=3^4\) in our fourth iteration. These values of \(p\) each iteration are accumulated in \(s\) so at the end we just have the sum of four powers of \(3\):$$s=3^1+3^2+3^3+3^4=3+9+27+81=120$$

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!