Conditionals: Control Structures & Flowcharts

A) What does the program for this flowchart do?

  1. Finish the flowchart.
  2. Write the full program.

Screen Shot 2014-10-15 at 12.51.31 PM
Flowchart maker: https://app.diagrams.net/
complete-flowchart

B) Next answer, explain, and/or write code snippets:

  1. What is a pseudorandom number?
  2. In the game of “guess a number”, when does the user exit the program?
  3. In the game of “guess a number”, how many loops are needed? Explain.
  4. How do you use the range function to print the integers from 34 through 41 inclusive?
  5. How do you add all the integers from 34 through 41 inclusive? Show code.
  6. Write the code to prompt the user for an integer, N. Your program will display a square pattern of asterisks N by N
  7. Write the code to print all even integers from 36 through 12 inclusive.
  8. Write the code for a while loop to prompt the user if he or she wants to continue to input more integers.

C) The following questions are related to this flowchart:

Screen Shot 2014-10-15 at 12.51.23 PM

  1. What is the oval shape used for?
  2. What is the code for each instruction in the flowchart?
  3. Draw the flowchart to find the two digits in the middle of a 4-digit integer.

D) Write the code to calculate and display:

 “     1        1”  
 “     2        4” 
 “     3        9”  
 “     4       16”  
 “     5       25”      

E) What does this program do?

import random
for i in range( 1, 22 ): 
   print ("%6d" % ( random.randrange( 1, 7 ) ), end = ""),
   
   if i % 3 == 0:  # print newline every ?? rolls
    print("")

F) What does this program do?

nterms = int(input("Enter a number "))

# first two terms
n1, n2 = 0, 1
count = 0
if nterms <= 0:
   print("Enter a positive number ")
elif nterms == 1:
   print(n1)
else:
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1

What is the output?

G) Two more

  1. What structure can be used to repeat a block of statements when the termination of the loop is not known? Explain your answer.
  2. What structure can be used to repeat a block of statements when the termination of the loop is known? Explain your answer.