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...
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
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 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=
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
1. Determine whether each statement below is True (T) or False (F). a. ____ If A,B...
1. Determine whether each statement below is True (T) or False (F). a. ____ If A,B and C are elements whose electronegativities satisfy xA > xB > xC, then then (ΔAC)1/2 = (ΔAB)1/2 + (ΔBC)1/2 if the Pauling electronegativity scale is valid. b. ____ There is no interelectronic repulsion in the H2+ molecule. c. ____ The wave function N(1sA + 1sB) is the exact electronic wave function for the ground electronic state of H2+. d. ____ The plane perpendicular to...
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed...
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed below. Please type your answers into this word document and submit it as an attachment in the assignment tab. Q1. Researchers were interested in determining whether background music helped or hindered students’ performance on a math test. Students were randomly assigned to 1 of 3 groups: 1) no music; 2) music only; and 3) music with lyrics. Students were then given a math exam,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT