Compute the actual number of additions subtractions multipli

Compute the actual number of additions, subtractions, multiplications divisions and comparisons that must be performed when the algorithm segment is executed. Assume n is an integer for i = 3 to n-1 a = 3 * n + 2 i -1 for i = 1 to [n/2] a= n-i t= 0 for i = 1 to n s = 0 for j =1 to i s = s+ a[j] t = t+ s^2}

Solution

Compute the actual number of additions, subtractions, multiplications, divisions, and
comparisons that must be performed with each of these algorithms:
a. for i = 3 to n-1       //This loop runs for values 3 to n-1, i.e., a total of n-1-3+1 = n-3 times.
       a = 3 * n + 2 * i - 1;   //This equation involves, 2 multiplications, 1 addition, and 1 subtraction.
Therefore, the number of multiplications required is 2 * (n-3),
the number of additions required is (n-3),
the number of subtractions required is (n-3).

b. for i = 1 to floor(n/2)   //This loop runs for values 1 to floor(n/2).
       a = n - i;    //This equation involves only a subtraction.
Therefore, the number of subtractions required is floor(n/2).

c. t = 0;   //This is an assignment, and will be done only once.
for i = 1 to n   //This loop runs for values 1 to n, i.e., a total of n times.
{               //Any statement inside this block will be executed n times.
s = 0;           //This is an assignment. So, total n assignments.
for j = 1 to i       //This loop runs for values 1 to i, i.e., a total of i times.
    s = s + a[j]       //This statements involves a single addition, and therefore, will be executed i times where i values runs from 1 .. n.
t = t + s^2       //This statement involves an addition, and an exponentiation.
}   
Therefore, the number of additions involved in this block is: 1 + 2 + 3 + ... n i.e., n(n+1)/2 + n.

 Compute the actual number of additions, subtractions, multiplications divisions and comparisons that must be performed when the algorithm segment is executed.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site