Conditionals: Review and re-cap

Flowchart: What is the look of this flowchart when finished?

Screen Shot 2014-10-15 at 12.51.31 PM

  1. What does the program for that flowchart do? Write the full program.
  2. What is a pseudorandom number? What is the code to generate a random number?
  3. In the Gambler’s ruin, when does the user exit the program?
  4. In the Gambler’s ruin, how many while-loops are needed? Explain.
  5. How do you use a while structure to print the integers from 34 through 41 inclusive?
  6. How do you add all the integers from 34 through 41 inclusive? Show code.
  7. Write the code to prompt the user for an integer N. Your program will display a square pattern of asterisks N by N
  8. Write the code to print all even integers from 36 through 12 inclusive.
  9. Write the code for a while loop to prompt the user if he or she wants to continue to input more integers.
  10. The following questions are related to this flowchart:

Screen Shot 2014-10-15 at 12.51.23 PM

a. What is the oval shape used for?

b. What is the code for each instruction in the flowchart?

c. Draw the flowchart to find the two digits in the middle of a 4-digit integer.

Write the code to calculate and display the following output:

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

Mystery: What does this program do?

for ( int i =1; i < 21 ; i++) 
  {
   System.out.printf( "%10d" ,(int)(1 + Math.random( ) * 6));

   if ( i % 5 == 0 )   # print newline every 5 rolls
    System.out.println();
  }

Simulation: What does this program do? How would you finish it up?

int frequency1 = 0
int frequency2 = 0
int frequency3 = 0
int frequency4 = 0
int frequency5 = 0
int frequency6 = 0

for ( int i =1; i < 6001 ; i++)     // 6000 die rolls
  {
   face = (int)(1 + Math.random( ) * 6);
    
   if (face == 1)                       
    frequency1 += 1                 // frequency counted
   else
   if ( face == 2 )
    ...
   ...
  }



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

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