Consider the following algorithm. x ← 1 for i is in {1, 2, 3, 4} do for j is in {1, 2, 3} do x ← x + x for k is in {1, 2, 3, 4, 5, 6} do x ← x + 1 x ← x + 5 Count the number of + operations done by this algorithm.
Each outside loop will run the insider loop for every iteration. the "k" loop is inside the "j" loop, which in turn is inside the "i" loop. As it turns out, for every iteration of "j", we will have 1 + (6*2) = 13 operations. As we have 3 "j" iterations, this gives 39 "+" operations for each "i" iteration. So, for 4 "i" iterations, we have 4*39 = 156 "+" operations.
got it!! the answer is 60
How's it? Please give your solution.
value of i = 4 for j = 3 for k = 6 x 2 4(3 + (6x2)) =60
i don't know if it makes sense. but I got it right!
Nope. I just tested it. Check it at http://ideone.com/GsQ3pj You should be able to understand. The program output is Success time: 0 memory: 3096 signal:0 156
Do you understand any programming languages?
Join our real-time social learning platform and learn together with your friends!