For each of the algorithm segments in 6—19, assume that n is a positive integer. (a) Compute the actual number of elementary operations (additions, subtractions, multiplications, divisions, and comparisons) that are performed when the algorithm segment is executed. For simplicity, however, count only comparisons that occur within if then statements; ignore those implied by for-next loops. (b) Use the theorem on polynomial orders to find an order for the algorithm segment.
15. for
(a)
To compute the actual number of elementary operations (addition, subtractions, multiplication, division and comparisons) that are performed when the algorithm segment is executed.
Given:
for
for
Next
Next
Calculation:
Each iteration of the inner loop requires four multiplication and outer loop requires one addition.
So the total number of elementary operation is four times the number of iterations of the inner loop plus one times the number of iterations of the outer loop.
Now the inner loop is iterated
The outer loop is iterated
Hence the total number of iteration of the inner loop is
(b)
To find an order for the algorithm segment by using the theorem on polynomial orders.
for
for
Next
Next