Question: (a) Consider the following algorithm segment. for i:=1 to n-1 p:=1 q:=1 for j:=i+1 to n p:=p*c[j] q:=q*(c[j])^(2) next j r:=p+q next i comparisons

(a) Consider the following algorithm segment.\

for i:=1 to n-1\ p:=1\ q:=1\ for j:=i+1 to n\ p:=p*c[j]\ q:=q*(c[j])^(2)\ next j\ r:=p+q\ next i

\ comparisons that occur within if-then statements, and ignore those implied by for-next loops. following table, which shows the values of

i

and

j

for which the inner loop is executed.\ \\\\table[[i,1,,,,2,,,,

cdots

,

n-2

,,

n-1
(a) Consider the following algorithm segment. fori:=1ton1p:=1q:=1forj:=i+1tonp:=pc[j]q:=q(c[j])2nextjr:=p+qnexti comparisons that occur within if-then statements, and ignore those implied by for-next loops. To arrive at the answer, observe that there multiplications for each iteration of the inner loop, and there additional addition for each iteration of the outer loop. The number of iterations of the inner loop can be deduced from following table, which shows the values of i and j for which the inner loop is executed. n1 n2 2 1 Hence, by Theorem 5.2.1, the total number of iterations of the inner loop is (n1)+(n2)++2+1=2n(. Because multiplications are performed for each iteration of the inner loop, the number of operations that are performed when the inner loop is executed is )2n(n1)=(2)n(2)n

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!