Question #53136

How many elementary operations are used in the following algorithm?
Step 1 Set a=1, b=1 c=2, and k=1.
Step 2 while k<n
(a) Replace c with a+b
(b) Replace a with b
(c) Replace b with c
(d) Replace k with k+1
endwhile
Step 3 Print b.
1

Expert's answer

2015-06-30T08:34:02-0400

Answer on Question #53136 – Math – Algorithms | Quantitative Methods

How many elementary operations are used in the following algorithm?

Step 1 Set a=1, b=1 c=2, and k=1.

Step 2 while k (a) Replace c with a+b

(b) Replace a with b

(c) Replace b with c

(d) Replace k with k+1

endwhile

Step 3 Print b.

Solution

4 types of elementary operations (assigning values, comparing k with 0, addition and print), and infinite number of them (because condition for endwhile will never be false).

If condition would be "while k<=N", it will take 4 operations in step 1,

N*(1 comparison+2 additions + 4 replacing)+1 comparison=7N+1 operations in step 2 and 1 operation (print) in step 3.

Thus, it will be (7N+6) elementary operations in total.

www.AssignmentExpert.com


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS