Question

In: Computer Science

Determine the output of the algorithm below the number of assignment operations in each (show work)...

Determine

  1. the output of the algorithm below
  2. the number of assignment operations in each (show work)
  3. the number of print operations in each (show work)
  4. the complexity of each algorithm in terms of Big O notation (show work)

3.

Let n be a given positive integer, and let myList be a three-dimensional array with capacity n for each dimension.

for each index i from 1 to n do

{

for each index j from 1 to n/4 do

{

for each index k from 1 to n/4 do

{

                myList[ i ] [ j ] [ k ] = i;

Print the element at myList[ i ] [ j ] [ k ];
}

}

}

can't provide anything else. This is the whole question

Solutions

Expert Solution

Let us solve each part one by one :

1. Output

The Output of given program will be

floor(n/4)*floor(n/4) times 1

floor(n/4)*floor(n/4) times 2

.

.

.

floor(n/4)*floor(n/4) times n

where floor(n) denotes largest integral number less than n. for example floor(2.5) = 2 .

If you take n = 10 in above code, the output will be :

1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10

Reason : As stated above output will be

floor(10/4)*floor(10/4) times 1 till floor(10/4)*floor(10/4) times 10

now floor(10/4)*floor(10/4) = 4 that's why numbers 1 to 10 comes 4 times each.

___________________________________________________________

2. Number of assignments

let x1 = Number of assignments (i=1)

let x2 = Number of assignments in increment of i (i=i+1)

let y1 = Number of assignments (j=1)

let y2 = Number of assignments in increment of j (j=j+1)

let z1 = Number of assignments (k=1)

let z2 = Number of assignments in increment of k (k=k+1)

let w1 = Number of assignments ( myList[i][j][k] = i )

Now, x1 = 1

x2 = n (i goes 1 to n)

y1 = n (depends on its outer loop)

y2 = n/4

z1 = n/4 (depends on its outer loop)

z2 = n/4

w = n * n/4 * n/4

So,total number of assignments = x1 + x2 + y1 + y2 + z1 + z2 + w = 1 + n + n + n/4 + n/4 + n/4 n* n/4 * n/4

= 1 + (11 * n) / 4 + n * n/4 * n/4

____________________________________________

3. Number of print statements

As i goes from 1 to n (n times)

j goes from 1 to n/4 (n/4 times)

k goes from 1 to n/4 (n/4 times)

So, print statement will run as many times nested loops runs that is : n * n/4 * n/4

________________________________________________

4. Time Complexity

Time complexity will be equal to Total Number of assignment statements + Total Number of print statements

that is 1+ 11*n/4 + n * n/4 * n/4 + n * n/4 * n/4

In terms of big O , complexity will be O(n * n/4 * n/4) or simply O(n * n * n)

_______________________________________________

Please comment below in case you have any doubt or anything is not clear.


Related Solutions

Show all work please, do not just give a number! Determine the number of ATP derived...
Show all work please, do not just give a number! Determine the number of ATP derived from each component and name the additional enzymes necessary to recognize the double bonds and account for the cost in ATP or NADH. a) palmitic acid (even number of carbons) b) oleic acid (even number of carbons, one double bond) c) linoleic acid (even number of carbons, two double bonds)
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output...
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output please. A blank line (may contain spaces) Postal abbreviation (2 letters) for State followed by a space and then the 5-digit zip code A KU student username (Ex. lpork247) A “valid” email address (also explain how you defined “valid”) A SSN pattern (ddd-dd-dddd)
For each of the event below, show the short-run effects on output, price and unemployment and...
For each of the event below, show the short-run effects on output, price and unemployment and explain how the Fed should adjust the money supply and interest rates to stabilize output A. President Bush’s tax cut the individual income tax by $2000 per household in 2003. B. Federal government issues the treasury bonds to finance the war in Iraq, and sell them to U.S citizens. C. Illinois state government increases income tax for the residents in Illinois to pay for...
Monetary Policy For each of the event below, show the short-run effects on output, price and...
Monetary Policy For each of the event below, show the short-run effects on output, price and unemployment and explain how the Fed should adjust the money supply and interest rates to stabilize output    A. FED increases the money supply to stimulate economy from recession. B. FED increased the fed fund rate from 0.25 to 1.25% to fight against the potential high inflation rate. C. Without any intervention by FED, people holds more cash and banks hold more excess reserves due...
Fiscal Policy For each of the event below, show the short-run effects on output, price and...
Fiscal Policy For each of the event below, show the short-run effects on output, price and unemployment and explain how the Fed should adjust the money supply and interest rates to stabilize output    A. President Bush’s tax cut the individual income tax by $2000 per household in 2003. B. Federal government issues the treasury bonds to finance the war in Iraq, and sell them to U.S citizens. C. Illinois state government increases income tax for the residents in Illinois to...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
Please show all work: Assignment: You will be answering problems #1 - #4. For each problem,...
Please show all work: Assignment: You will be answering problems #1 - #4. For each problem, you will need to complete parts A through F. Please make sure you answer each part completely and you offer justification where necessary. Do all work "by hand" (not using StatCrunch) as good practice for your next exam! A) Write the claim mathematically and state the null and alternative hypotheses. B) Determine whether the hypothesis test is left-tailed, right-tailed, or two tailed and whether...
Determine the convergence or divergence if each integral by using a comparison function. Show work using...
Determine the convergence or divergence if each integral by using a comparison function. Show work using the steps below: A. Indicate the comparison function you are using. B. Indicate if your comparison function is larger or smaller than the original function. C. Indicate if your comparison integral converges or diverges. Explain why. D. State if the original integral converges or diverges. If it converges, you don’t need to give the value it converges to. 16. integral from 0 to 1((e^(-x))/(√x))...
For the following reaction show all work: 1. Give the oxidation number for each element in...
For the following reaction show all work: 1. Give the oxidation number for each element in the following equation. Fe2O3 (s) + CO (g) = Fe (s) + CO2 (g) a. Write the balanced oxidation half reaction. b. Write the balanced reduction half reaction. c. What is the oxidizing agent? d. What is the reducing agent? e. What is the net ionic equation?
II. Monetary Policy For each of the event below, show the short-run effects on output, price...
II. Monetary Policy For each of the event below, show the short-run effects on output, price and unemployment and explain how the Fed should adjust the money supply and interest rates to stabilize output B. FED increased the fed fund rate from 0.25 to 1.25% to fight against the potential high inflation rate. C. Without any intervention by FED, people holds more cash and banks hold more excess reserves due to market uncertainty. III. Fiscal Policy For each of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT