# 1990 (Niagara South

FIRST ANNUAL NIAGARA SOUTH

PROGRAMMING CONTEST

MARCH 1990

DO NOT TURN THIS PAGE UNTIL YOU ARE INSTRUCTED TO DO SO AND HAVE READ THE FOLLOWING CAREFULLY

1. YOU WILL HAVE TWO AND ONE‑HALF HOURS FOR THIS CONTEST.

2. ALL SOLUTIONS ARE TO BE STORED ONTO THE DISKETTE GIVEN TO YOU BY THE PRESIDING TEACHER.

3. PRINTING IS NOT ALLOWED DURING THE CONTEST.

4. SOLUTIONS WILL BE JUDGED ON THE BASIS OF LOGICAL CORRECTNESS AND ALSO GOOD STRUCTURED STYLE, INPUT VALIDATION, AND SCREEN PRESENTATION.

5. YOU ARE TO ATTEMPT ONLY 2 QUESTIONS FROM PART A, 1 QUESTION ONLY FROM PART B, AND 1 QUESTION ONLY FROM PART C.

6. IN DETERMINING YOUR OVERALL SCORE, THE 2 QUESTIONS FROM PART A ARE WORTH 25 POINTS EACH, THE 1 QUESTION FROM PART B IS WORTH 50 POINTS AND THE 1 QUESTION FROM PART C, 75 POINTS.

## PART A

DO ONLY 2 QUESTIONS FROM THIS PART

### QUESTION A‑1 [SAVE ON DISK AS "Q.A1"]

Write a program which accepts a number as input from the user and then offers the user a menu of calculation choices:

`1. Square of the number`

`2. Square root of the number`

`3 Reciprocal of the number`

`4. Cube of the number`

`5. Cube root of the number`

`0. Exit program`

The output is the result of the user's selected choice.

### QUESTION A‑2 [SAVE AS "Q.A2"]

Write a program which inputs a positive integer, N, from the user. The output is to be the integer in the range 1 to N which has the greatest number of divisors. For example, for an input of 5, the output should be 4.

### QUESTION A‑3 [SAVE AS "Q.A3"]

Write a program which inputs a positive integer, N, from the user. The factorial of N is defined as the product of all integers from 1 up to and including N; for example, the factorial of 4 is 24, since 1X2X3X4=24. The output of the program is to be a chart showing the values of the factorial of each integer from 1 to N. Determine what is the largest value of N that the user should be allowed to input.

### QUESTION A‑4 [SAVE AS "Q.A4"]

Imagine a "particle" located on the centre square of a two‑dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right. Associated with each possible motion are the following probabilities:

Up 1/12

Down 1/12

Left 1/24

Right 19/24

Write a program that simulates the motion of the particle within the grid, until it reaches an "exit" at the square in row 6, column 75. The particle cannot "penetrate" any of the four boundaries. Have the program repeat the simulation 10 times and calculate the average total distance travelled by the particle until it exits.

**Note: the ratios above may never all your program to complete, so experiment with your own ratios to be more successful! **

## PART B

DO ONLY 1 QUESTION FROM THIS PART

### QUESTION B‑1 [SAVE AS "Q.B1" ]

Write a program that allows the user to enter a seven‑character code. The program should report whether or not the code is valid. The code consists of seven characters, the first six of which may be letters or numbers. The seventh character is a number which is a check digit calculated as follows:

- take the place value in the alphabet of any letters in the code; e.g., an 'E' would have place value 5, 'k' would be 11;

- add the even‑position numbers and/or place values;

- add the odd‑position numbers and/or place values;

- subtract the two sums obtained above and take the absolute value of the result;

- add the digits of the result obtained in the previous step;

- repeat the last step if there is more than one digit in that sum.

Check your program using the following codes, one of which is valid: "R5T83O4" and "RTN6521" (updated from original competition)

### QUESTION B‑2 [SAVE AS "Q.B2" ]

The following game is played by two people using coins. In each play of the game, each person flips a coin (producing 'Heads' or 'Tails'). The payoff on each play is determined by the following rules:

Person 1 Person 2 Result

Heads Heads Person 2 pays Person 1 $20

Heads Tails Person 1 pays Person 2 $30

Tails Heads Person 1 pays Person 2 $10

Tails Tails Person 2 pays Person 1 $20

Assuming that there is an equal chance of each person obtaining Heads or Tails, the amount of money lost by either player should be approximately zero. Develop a program to simulate this game over 1000 plays.

### QUESTION B‑3 [SAVE AS "Q.B3"]

This program deals with Bell Canada long distance billing. The regular rate for a direct‑dial call from St. Catharines to Thunder Bay is 42 cents per minute. (Note that 61 seconds would count as a two‑minute call). Write a program that inputs the day of the week (1=Sunday, 2=Monday, etc.), the time of day at which the call originated (using 24 hour format, e.g., 06:35 or 19:53), and the number of seconds the call lasted and then outputs the total charge to the customer based on the table given below:

TIME DAY DISCOUNT

08:00 ‑ 18:00 Weekdays none

18:00 ‑ 23:00 Weekdays 35%

23:00 ‑ 08:00 Weekdays 60%

All day Saturday/Sunday 60%

## PART C

DO ONLY 1 QUESTION FROM THIS PART

### QUESTION C‑1 [SAVE AS "Q.C1"]

Set up a 12‑element array called DAYS_OF_MONTH whose Kth element contains the number of days in month number K. The user should be requested to input two dates of the form "yy‑mm‑dd" (for example, 90‑03‑22 would be March 22, 1990). The program should then check that the dates are valid and, if so, calculate the number of days from the first date to the last date inclusive. The program should be able to accommodate leap years.

Note: A leap year is any year which is divisible by 4. A year divisible by 100 is not a leap year unless it is also divisible by 400. (For example, 1980 is a leap year (divisible by 4 but not by 100) but 1900 is NOT a leap year (divisible by 100 but not 400) and 2000 IS a leap year (divisible by 400)

### QUESTION C‑2 [SAVE AS "Q.C2"] (Counts as "B"-level question in Beens' class)

Part A:

Write a program which creates a data file named "EMP.dat". This file contains a record of information on each EMPLOYEE of the ABC Company.

Each record is to contain the following fields:

last_name : maximum of 16 characters

first_name : maximum of 16 characters

hourly_rate : range $0.00 to $99.99

tax_code : integer from 0 to 5 inclusive

A typical record is of the form: "Able", "Andrew", 12.00, 4

The file ends with the sentinal: "zzzz", "zzzz", 0.0, 0, 0

Use the program to create a file which you will use as test data for the program you will write in Part B of this question.

Part B:

Write a program which:

1) For each employee:

- inputs last_name, first_name, hourly_rate, and tax_code from the file EMP.dat

- requests hours_worked as keyboard input

- calculates his/her

- regular pay ( 40 hrs. max )

- overtime pay ( time and a half over 40 )

- gross pay

- income tax deducted, where

tax = gross pay x tax_rate

- his/her net pay

The tax rate code has been entered on the employee's record according to his/her number of dependents and projected annual gross income. Use the table shown to find the applicable tax rate

code tax_rate

0 .10

1 .16

2 .22

3 .28

4 .34

5 .40

2) Generates a report showing the following columns with the headings:

Last Name, Initial, Hours Worked, Regular Pay, Overtime Pay, Gross Pay, Income Tax, and Net Pay

3) Gives totals for all appropriate columns.

### QUESTION C‑3 [SAVE AS "Q.C3"]

Store the following maze into a two‑dimensional arrav:

` ┌──── Start`

` │`

` │`

` X X X X X X X X X`

` X X X`

` X X X X X`

` X X X X X`

` X X X X X`

` X X X X X X`

` X X X X`

` X X X X X X X X`

` X X X`

` X X X X X X X X │ X`

` │`

` End ────┘`

Write a program which will seek a path through the maze. The output should include the path and the maze. The program should also include a method of handling the situation where no solution is possible.

Only the correct path should be left on the screen when the program terminates.