Sie sind auf Seite 1von 8

ENGN 1310 – Computer Programming

Practice Problems

Question 1
While braving the CN Tower’s “Edge Walk” (a mere 356 meters above ground), you attempt to
capture that perfect “selfie” … but oops … you drop your phone. Let’s write a simple simulation
in which, using a loop and a standard physics formula, we can compute and display the altitude of
the phone at each second until it hits the ground.

Your job is to write a program that generates output that looks like this:

1
• The standard physics formula to compute the distance fallen at time t is: (0.5.g.t2) where g is
the gravitational acceleration in meters per second per second (9.80665 m/s2).
• The loop should cycle once for each second until the phone hits the ground.
• In this case, a while loop or a do loop would be best, since we don’t know in advance how
many loop cycles are needed.
• Notice that, most likely, the last loop cycle will compute a negative altitude, and that shouldn’t
be reported.

2
Question 2
One of the simplest (and most insecure) algorithms for encryption is called the Caesar Cypher.
The algorithm replaces each character in the original message with another character. We will limit
ourselves to single words composed of alphabetic characters. Thus, our algorithm will be to replace
each letter in the original word by a letter further up the alphabet. The “encryption key” is an
integer indicating how much further up the alphabet the replacement letter will be (wrapping
around if necessary). For example, if the key is 2, the word “bye” becomes encrypted as “dag”.
With an encryption key of 5, the program execution would look like this:

Write a program to encrypt the input word by replacing each letter in turn with another letter further
up the alphabet. Your program should start by replacing each letter to lowercase, before encrypting
any character.

3
Question 3
When buying real estate (land, house, etc.), most people take out a mortgage and pay it off over
an extended period. Different than a regular loan, a mortgage is secured by the property itself until
the debt is paid off. Early in the term, most of the monthly payment goes toward paying off the
interest. Later on, more goes toward the principle (the amount you borrowed originally). The
calculation of the monthly payment is a bit messy and takes into account three things: the amount
borrowed, the interest rate and the term (how long you take to pay off the mortgage). The formula
for computing the fixed monthly payment (P) to fully amortize a loan of L dollars over a term of
n months with a monthly interest rate of c is:
𝑃 = 𝐿[𝑐(1 + 𝑐 )𝑛 ]/[(1 + 𝑐 )𝑛 − 1]
The formula to calculate the remaining loan balance (B) of a fixed payment loan after p months
is:
𝐵 = 𝐿[(1 + 𝑐 )𝑛 − (1 + 𝑐 )𝑝]/[(1 + 𝑐 )𝑛 − 1]
Use a loop to allow someone to experiment with the three parameters of the mortgage. Your
program will input these three values and then compute and display the monthly payment and
amount owing after a one year. Then it will ask for another set of input, and so on. If the user enters
a zero or negative value for the loan amount, then the program should terminate.
Notice that the variable names used in the formulas are not adequately descriptive for a computer
program. Also notice that there are pieces of the formulas that are used multiple times. In such
cases, we often compute that piece once and store the partial result in a temporary variable.
The screen capture below shows what the program execution might look like. Notice that the
program output is rounded to two decimal places (Google it).

4
You need a while loop to:
• Input the 3 values from the user. Notice that the interest rate is entered in a typical way (e.g.
6.6 means 6.6% annually) but the formula uses monthly fractional values (e.g. 6.6% —>
0.0055).
• Compute the monthly payment and display it (rounded to two decimal places).
• Compute the remaining balance after one year and display it (rounded to two decimal places).

5
Question 4
As you are doubtless aware, every car has a tripmeter that typically reads from 000.0 to 999.9 km.
You are going to simulate this, using nested loops. For four digits, you will require four loops; the
innermost loop is the tenths-of-a-kilometer; the outermost loop would be the hundreds of a
kilometer. (A handy tip: with nested loops, the innermost loop is always the thing that happens
most often, and the outermost loop, that thing that happens least often.)

Your display should print:


*START*
000.0
000.1
000.2
...
999.8
999.9
*COMPLETE*

6
Question 5
One of the standard challenges, in years gone by, was the memorization of the multiplication table,
to 12 x 12. These were famously produced, often with typing errors, by teachers when teaching
gradeschool students.

Your task: write a complete program to generate the 12 x 12 multiplication table, like the one
shown below.

Hints:
• Every box is assumed to be a product of p x q where p and q are values
• The first line is a line of indexes only, and isn't part of the loop structure
• Each subsequent line is then composed of:
• the row index (p)
• the 12 products of p x q (where q is the item that is varying)
q is varied in the inner loop, and p is varied in the outer loop

7
Question 6
You’ve been introduced to the random number generator, and you know how to generate random
numbers between any contiguous integer range [a,b]. We’re going to use that information, together
with a single loop, to do some statistical testing. You will write a program which will simulate one
million rolls of three six-sided dice, the values of which are added together (so, the result will
always be in the range of 3 to 18 - gamers call this "3d6").

You will then tally the roll results and print them as follows:

For 3d6:
values <= 7: count1
values between 8 and 13, inclusive: count2
values >= 14: count3

Your program will then have a second section which simulates the rolling of a single theoretical
16-sided die, having face values from 3 to 18. Again, you will tally the roll results and print them
as follows:

For 16-sided:
values <= 7: count4
values between 8 and 13, inclusive: count5
values >= 14: count6
Observe the difference. With 3d6, how many ways can 3 be rolled? How many ways can 18 be
rolled? What about 12? Have the program run both tests three times. What can you determine
from the values for the 16-sided die in each run?

Das könnte Ihnen auch gefallen