Previous | Home | Lecture Notes | PDF slides | Next


Perl 2007: Exercises 2

These exercises are part of a programming course taught at University of Tilburg, The Netherlands.

If you are a course participant, please send the solutions to these exercises to erikt(at) before or on Tuesday September 18, 2007. Do not forget to test your programs. Include the test results in your weekly reports.

Exercise 2.1

Write a program that reads a number and prints the multiplication table (1-12) for that number three times. Each of the three tables should be coded with a different iterative structure. The tables should look like this example for input 23:

   23 *  1 = 23
   23 *  2 = 46
   23 *  3 = 69
   23 *  4 = 92
   23 *  5 = 115
   23 *  6 = 138
   23 *  7 = 161
   23 *  8 = 184
   23 *  9 = 207
   23 * 10 = 230
   23 * 11 = 253
   23 * 12 = 276

So all the multiplication operators and equal-to signs should be printed exactly under each other.

[Solution] Only look at the solution when you have finished the exercise or when you are completely stuck.

Exercise 2.2

Write a program that reads an arbitrary number of positive numbers and stops when a zero (0) is entered. The program should print how many numbers were entered and what the average of these numbers is. Additionally, the program should test if a negative number is entered and immediately stop when that happens. In that case, it should skip the normal output and print an error message.

Note: the zero is used as a stop sign. It does not have to be included in the count and the average.

Exercise 2.3

Implement a mini expert system for guessing animal species based on their features. It should ask the following questions:

  1. Is the animal big?
  2. Does it live in water?
  3. Can the animal fly?
  4. Does the animal have legs?
  5. Does the animal have fur?

The questions should be answered with 1 (meaning yes) or 0 (meaning no). The program should respond with lion to the answer set (1,0,0,1,1), whale to (1,1,0,0,0), mouse to (0,0,0,1,1) and parrot to (0,0,1,1,0). For other answer combinations, it should answer unknown animal.

Exercise 2.4*

This is a starred exercise which means that you may skip the exercise. Make this exercise only if you think it is interesting and if you have some time left.

In the game of Yahtzee, players attempt to obtain specific combinations of throws with five dice. They are allowed to throw the dice three times while each time putting aside the dice which achieved favorable results and throwing the others one more time.

One player's goal is to achieve a combination of five sixes. Write a program that estimates his success rate by performing the dice throws of the player 1,000,000 times or more and dividing the number of successful attempts by the total number of attempts (hint: use the rand() function). What success rate do you obtain?


Exercise 2.5*

In the game of darts, one single dart can obtain different scores: the numbers 1-20 (singles), doubles (twice each number in the range 1-20), triples (three times each number in the range 1-20) and also 25 and 50. The maximum score with one dart is 60 (triple 20) and the maximum score with three darts is 180 (three times 60). Not all scores below 180 are attainable with three darts. For example, the second highest possible three dart score is not 179 but 177 (two times triple 20 plus a triple 19).

Write a program which prints all scores between 1 and 180 which are not attainable with three or fewer darts. The program should also report how many of these scores exist. What results do you get?


Previous | Home | Lecture Notes | PDF slides | Next
Last update: October 23, 2007. erikt(at)