In this prelab you will familiarize yourself with some of the mathematical and design questions underlying Lab 02 by working on the first three steps of the programming process (describing and understanding the problem and designing an algorithm). There are 10 exercises which appear in boxes below. Please type your solutions and hand in a printed paper copy before the beginning of class on Wednesday. Remember, no late prelabs allowed!
Write down the output generated by the following two chunks of code. Remember that the print method generates a new line after printing the string it is passed, unless the "end" parameter sets it otherwise.
for i in range(1, 5) : for j in range(1, 2*i+1) : print("*", end="") print("!")
for i in range(4,0,-1) : for j in range(1, i+1) : print(i, end="") for j in range(1, i+1) : print(j, end="") print("?")
In this portion of the prelab you'll start thinking about how computers can handle repetition.
The Fibonacci numbers are a sequence of integers defined by the following recurrence relation
Describe the Problem:
The problem you will solve on your lab is as follows.
input: get a positive integer n from the user.
goal: compute and output the nth Fibonacci number.
Understand the Problem:
For example, the first six Fibonacci numbers are 1, 1, 2, 3, 5, 8.
Hint: you should end with 377.
In light of the recent economic meltdown, you've decided to take a more conservative approach with your financial portolio; as of today, you're getting rid of all your sub-prime morgage securities, all your toxic assets (what were you thinking?), and your considerable stockpile of pork bellies.
To play it safe, you're going to put your money into a savings account. This account earns a fixed percentage of interest every month. Assuming you make reguar, monthly deposits as well, you'd like to know how much you'll have after a given number of month, assuming your monthly contribution occurs after interest is accrued.
Describe the Problem:
The problem you will solve on your lab is as follows.
input: get an initial deposit, a monthly interest rate, a monthly deposit, and a number of months n from the user.
goal: compute and output the total money in your possession at the end of n months.
Understand the Problem:
For example, suppose your initial savings is $100, the monthly interest rate is 1%, and you plan on contributing $20 a month. Then during the first month, you'd get $1 of interest (100 * 0.01) followed by another $20 from your regular contribution. So you'd end the first month with $121. On the second month, your interest would be $1.21 (i.e., 121 * 0.01), which, together with you fixed $20, leaves you with $142.21.
After the third month, you'd have $163.63. Technically, the amount you'd have on month 3 would be $163.6321, but most accounts don't keep track of fractions of pennies (as fans of the movie Office Space may know), so you'll want to cut off anything below 2 decimal places.
In the next batch of problems, we're going to be looking for patterns in sequences of figures. For each problem, a few figures are given, each of which made out of ASCII characters and is associated with an index (a number).
Describe the Problem:
The problem you will solve on your lab is as follows.
input: get a positive integer n that represents the index of your pattern.
output: print the indexth pattern to the console, for whichever pattern you are currently on.
Understand the Problem:
For example, below are some indices (the numbers on the left) and their associated figures for Pattern A.
1 2 3 1 2 3 1 2 3
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
You can probably see what's happening; figure 3 consists of 3 rows, each of which includes all numbers from 1 to 3. You can infer that figure 2 would look like
1 2 1 2
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Line 1 of figure 6 is all numbers from 1 to 6. Line 2 of figure 6 is also all numbers from 1 to 6. In general, line i of figure 6 is all numbers from 1 to 6. Even more generally, we can give the following characterization of Pattern A:
Design an Algorithm:
In order to design an algorithm for this problem, we need to formulate a precise description of what the current pattern is. In particular, an algorithm to output pattern A is as follows:
Below we will give you some different patterns. Your task is to (1) show that you understand each pattern, by giving the next pattern in the sequence, and (2) design an algorithm for the problem, by defining what an arbitrary line i of figure n would contain. Note that in the example above, the description of line i in figure n did not depend on i. In the following patterns, the description of a line may depend on i, n, or both.
1 1 1 2 2 2 3 3 3
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
1 2 3 2 3 3
1 2 3 4 2 3 4 3 4 4
1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
8. Describe line i of figure n in Pattern C.
* * ** * * ** * *
* * ** * * * * * ** * *
* * ** * * * * * * * * ** * *
10. Write pseudocode for the pattern. You may want to break it up into 3 sections, one to print the first row, one to print the middle rows, and one to print the last row.
If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.
I affirm that I have adhered to the Honor Code in this assignment.