Question

In: Computer Science

Consider the three following pieces of pseudocodes. In each case, - Give the number of times...

Consider the three following pieces of pseudocodes. In each case,

- Give the number of times "Hello" will be printed,
- Generalize, giving the number of times as a function of n
- Express this in theta notation in terms of n.

a)   set n = 32
set i = 1
while i<= n
   print "Hello"
   i=i*2
endwhile

b)   set n = 4
set i = 1
while i not equal to n
   print "Hello"
   i=i+2
endwhile

c)   set n = 4
set i = 1
while i<= n
   set j = 1
   while j <= i
      print "Hello"
      j=j+1
   endwhile
   i=i+1
endwhile

Solutions

Expert Solution

a)   set n = 32
set i = 1
while i<= n
   print "Hello"
   i=i*2
endwhile

This will print "Hello" for i = {1,2,4,8,16,32} = 6 times
Number of time = floor(log2n) + 1
T(n) = θ(log n)

b) set n = 4
set i = 1
while i not equal to n
   print "Hello"
   i=i+2
endwhile

if n is even number, then while Loop will never stop(It will print Infinite times)
if n is odd number, then it will print (n-1)/2 times
Number of time = (n-1)/2 times
T(n) = θ(n)

c) set n = 4
set i = 1
while i<= n
   set j = 1
   while j <= i
      print "Hello"
      j=j+1
   endwhile
   i=i+1
endwhile

when i=1, j={1} 1 times
when i=2 , j={1,2} 2 times
when i=3 , j={1,2,3} 3 times
when i=4 , j={1,2,3,4} 4 times
Total = (1 + 2 + 3 + 4) = 10 times
Number of time = n*(n+1)/2 times
T(n) = θ(n^2)


Related Solutions

Why is physical health as important as mental health? Give three pieces of evidence, And give...
Why is physical health as important as mental health? Give three pieces of evidence, And give a detailed explanation for each. Thanks!
A fair die is rolled 300 times and each time a number evenly divisble by three...
A fair die is rolled 300 times and each time a number evenly divisble by three is​ rolled, a success is recorded. Find the probability of obtaining the​ following: Between 90 and 110 successes​ (inclusive) ​(Round to four decimal ​places)
For each of the following species, give the total number of electrons, the number of electrons...
For each of the following species, give the total number of electrons, the number of electrons in the valence shell, and the number of core electrons. Mg # of electrons: # of electrons in valence shell: # of core electrons: Sn2+ # of electrons: # of electrons in valence shell: # of core electrons: I- # of electrons: # of electrons in valence shell: # of core electrons:
Consider the following short cases. Connect each case with one of the three location techniques we...
Consider the following short cases. Connect each case with one of the three location techniques we talked about in class (linear programming, centroid or factor rating), answer the related question and explain why: Lynn, Inc. is trying to locate a customer service center in a suburban area. They have population data for the various suburbs and villages around their area. They are interested in locating the center centrally with minimum distance between customers and the service center. In this technique...
Give the best Lewis structure for each of the following ions or molecules: In each case,...
Give the best Lewis structure for each of the following ions or molecules: In each case, state: The electron geometry around the central atom The VSEPR shape/molecular geometry around the central atom Whether or not the ion/molecule carries a permanent dipole. If a permanent dipole exists, use a crossed arrow to show its direction. A.) Iodine pentafluoride, IF5 B.) Selenium trifluoride ion, SeF3+
Draw Lewis structures for each of the following compounds. In each case, specify the number of...
Draw Lewis structures for each of the following compounds. In each case, specify the number of valence electrons surrounding the central atom. a) bromine dioxide (BrO2) (Assume the central atom does not contain an expanded octet.) There are ___ valence electrons surrounding the central atom. b) beryllium fluoride (BeF2) There are___ valence electrons surrounding the central atom. c) phosphorus pentabromide (PBr5) There are ___ valence electrons surrounding the central atom.
Consider each of the following systems for long times, at which their behacioor is time invariant....
Consider each of the following systems for long times, at which their behacioor is time invariant. Identify each system as equilibrium or steady state. 1) A hose supplies water at a constant rate to a bucket with a hole in it. A constant water level is maintained 2)A mixture of reacting gas molecules is constrained to a fixed temperature, e.g., H2, O2 and H2O at 600 K. 3) Ice floats in water, thermally insulated from its surroundings. 4) Molten steel...
Consider each of the following systems for long times, at which their behacioor is time invariant....
Consider each of the following systems for long times, at which their behacioor is time invariant. Identify each system as equilibrium or steady state. 1) A hose supplies water at a constant rate to a bucket with a hole in it. A constant water level is maintained 2)A mixture of reacting gas molecules is constrained to a fixed temperature, e.g., H2, O2 and H2O at 600 K. 3) Ice floats in water, thermally insulated from its surroundings. 4) Molten steel...
Consider each of the following independent and material situations. In each case: • the financial report...
Consider each of the following independent and material situations. In each case: • the financial report date is 31 December 2019; • the field work was completed on 12 February 2020; • the directors declaration and the audit report were signed on 19 February 2020; and • the completed financial report accompanied by the signed audit report were mailed to shareholders on 18 March 2020 A. You are an auditor pf PP Limited (PP), a company specialising in industrial property...
Consider each of the following independent and material situations. In each case: • the financial report...
Consider each of the following independent and material situations. In each case: • the financial report date is 31 December 2019; • the field work was completed on 12 February 2020; • the directors declaration and the audit report were signed on 19 February 2020; and • the completed financial report accompanied by the signed audit report were mailed to shareholders on 18 March 2020 A. You are an auditor pf PP Limited (PP), a company specialising in industrial property...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT