Question

In: Computer Science

Determine a) the output of each algorithm below b) the number of assignment operations in each...

Determine
a) the output of each algorithm below
b) the number of assignment operations in each (show work)
c) the number of print operations in each (show work)
d) the complexity of each algorithm in terms of Big O notation (show work)
1.
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 do
{
for each index k from 1 to n do
{
​myList[ i ] [ j ] [ k ] = i;
Print the element at myList[ i ] [ j ] [ k ];
}
}
}
2.
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/2 do
{
for each index k from 1 to n/2 do
{
​myList[ i ] [ j ] [ k ] = i;
Print the element at myList[ i ] [ j ] [ k ];
}
}
}
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 ];
}
}
}

Solutions

Expert Solution

Solution of 1.

a) For the outermost loop i.e for each i , j varies from 1 to n and for each j , k varies from 1 to n,

Hence, the output will be

n2 times 1 , n2 times 2, n2 times 3, ................. n2 times n

b) Lets say n=2,the numbers present inside the 3d array will be -> 1,1,1,1,2,2,2,2 and the number of assignment operations = 8 = 23

As we can say that for every element of the 3d array, one assignment operation is being performed.

Hence, total number of assignment operations = n3

c)

When j=1, Print operation will be performed n times i.e when k varies from 1 to n.

When j=2, Print operation will be performed n times i.e when k varies from 1 to n.

...........

When j=n, Print operation will be performed n times i.e when k varies from 1 to n.

Similarly the loop goes for i,

When i=1, Print operation will be performed n2 times i.e when j varies from 1 to n and for every j, k varies from 1 to n.

When i=2, Print operation will be performed n2 times i.e when j varies from 1 to n and for every j, k varies from 1 to n.

...........

When i=n, Print operation will be performed n2 times i.e when j varies from 1 to n and for every j, k varies from 1 to n.

  

Hence, the number of Print operations = n*n*n = n3

)

d) As seen in the example mentioned in section c, the number of assignment operations = n3 and number of Print operations=n3.

Since a single print statement and assignment operations take constant time to be done i.e time taken to perform both the operations i.e O(1) is constant and is independent from the number of elements in the 3d array,

Hence, the time complexity is solely dependent on the number of times the print and assignment operation has been done.

So, time complexity = n3 + n3 = 2n3 = O(n3)

Solution of 2.

a) n2/4 times 1 , n2/4  times 2, n2/4 times 3, ................. n2/4 times n.

b) As described in the section b of the above answer,

total number of assignment operations = n*n/2*n/2 = n3/4

c) As described in the section c of the above answer,

number of Print operations = n*n/2*n/2 = n3/4

d) With the same logic as discussed in section c of the above answer,

  time complexity = n3/4 + n3/4  = n3/2 = O(n3)

Solution of 3.

a) n2/16 times 1 , n2/16  times 2, n2/16 times 3, ................. n2/16 times n.

b) As described in the section b of the above answer,

total number of assignment operations = n*n/4*n/4 = n3/16

c) As described in the section c of the above answer,

number of Print operations = n*n/4*n/4 = n3/16

d) With the same logic as discussed in section c of the above answer,

  time complexity = n3/16 + n3/16 = n3/8 = O(n3)


Related Solutions

Determine the output of the algorithm below the number of assignment operations in each (show work)...
Determine the output of the algorithm below the number of assignment operations in each (show work) the number of print operations in each (show work) 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...
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
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
Determine the number of moles of the compound and determine the number of moles of each...
Determine the number of moles of the compound and determine the number of moles of each type of atom in each of the following: (a) 2.12 g of potassium bromide, KBr (b) 0.1488 g of phosphoric acid, H3PO4 (c) 23 kg of calcium carbonate, CaCO3 (d) 78.452 g of aluminum sulfate, Al2(SO4)3 (e) 0.1250 mg of caffeine, C8H10N4O2
Determine the appropriate level of measurement for each variable: a. Attendance at a game. b. Number...
Determine the appropriate level of measurement for each variable: a. Attendance at a game. b. Number of goals scored by the two teams, together, during a game. c. Primary color of a team’s jersey (or shirt) during a game. d. Pre-World Cup ranking of each team. e. Home state of each player on the U.S. team. f. Temperature, degrees Celsius, at the kick-off (meaning, start) of a game. g. Whether (or not) a player attended a college or university. h....
For the compounds and ions below determine the oxidation number of each at in molecule/ion po33-...
For the compounds and ions below determine the oxidation number of each at in molecule/ion po33- and cah2
Determine the oxidation number for each of the bolded elements: (a) PO33- P= (b) CaSO4 S=...
Determine the oxidation number for each of the bolded elements: (a) PO33- P= (b) CaSO4 S= (c) NaHCO3 C= (d) BrO4- Br=
Describe and analyze an algorithm to determine the number of shortest paths from a source vertex...
Describe and analyze an algorithm to determine the number of shortest paths from a source vertex s to a target vertex t in an arbitrary directed graph G with weighted edges. You may assume that all edge weights are positive and that all necessary arithmetic operations can be performed in O(1) time. [Hint: Compute shortest path distances from s to every other vertex. Throw away all edges that cannot be part of a shortest path from s to another vertex....
Determine the number of valence elections in each of the atoms listed below: C, O, N,...
Determine the number of valence elections in each of the atoms listed below: C, O, N, F
What is the output of the StackQueueMystery algorithm (below) when we have enqueued 1, 2, 5,...
What is the output of the StackQueueMystery algorithm (below) when we have enqueued 1, 2, 5, 6, 3, 7, 4, 10, 8, 9 on q and pushed 8, 6, 9, 7, 10, 5, 4, 3, 2, 1 on stk (in that order) before calling this algorithm. Show your work. StackQueueMystery: Input: stk: stack with integers 1-n Input: q: queue with integers 1-n Input: n: size and range of stk and q Pseudocode: count = 0 while stk and q are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT