Question: Convert the for loop to a while loop and calculate its time complexity, please include the cost for each step. a sample output is provided

Convert the for loop to a while loop and calculate its time complexity, please include the cost for each step. a sample output is provided below.

```code

int sum=1; for (int a=0;a0) sum--;

Convert the for loop to a while loop and calculate its time

Example 2 Find the time complexity of function findmax in Figure 2 that finds the maximum value in the array of integer given by the two parameters A and n int findmax(int All, int n) f int max-A[0]; for (int i-1;ikn;i++) if (A [i]>max) max=A[i]; return max, Figure 2. Program for Example 2. Solution: Complete the following steps 1. Create a three column table putting line numbers in the first column, statements of the program in the second column, and the cost of each statement in the third column. Sum the cost of the individual statements to find the cost of the program. 2. Line Statement Cost 1 int findmax(int All, int n) { int max=A[O]; int i1; while (imax) max-AM; 2k return max, Total Total Total T(n) 4k + 3 T(n)-4(n - 1) +3 T(n) = 4n-1

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!