CSci 1001 Problem set 4. Due Tuesday, April 3.

Problem 1: loops and random numbers

Write a program that simulates five rolls of a 6-sided die using a random number generator. Print out the result of each roll and find the sum of all rolls (hint: you will need a variable sum that is set to zero before the loop, keep adding the results to that variable, and print it after the loop).

Problem 2: functions and loops

Write afunction that prints out one verse of the song 10 bottles of beer on the wall. The function takes one number as a parameter For instance, if you pass 5 as a parameter, it would print

5 bottles of beer on the wall, 5 bottles of beer.
Take one down and pass it around, 4 bottles of beer on the wall.
Use a loop to call the function to print out the entire song starting at 10 bottles.

A good way of approaching the problem is:

  1. Write a function that takes a number and prints the first line of the verse
  2. Write the function call with just a fixed number (say, 5). The function should display the first line of the verse.
  3. Finish writing the function. It should display the entire verse, as above.
  4. Write a loop that calls the function repeatedly with numbers from 10 down to 1 (see examples of loops counting down)

Problem 3: Written part

Ex. 11 p. 153. Include a printout, a photocopy, or a scan of each of the two algorithms, mark parts of it that you think are unclear and parts that you think are clear, briefly explain why.

How to submit

Send all of your program files to me by e-mail. You may submit the written part in class or send it together with your problem set. Please send only ONE e-mail with your entire submission, make sure you include everything. Multiple e-mails with your homework may result in lower grade.

Your programs will be graded not just on correctness, but also on the clarity of code. Appearance of the page and creativity may be taken into account.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.