Difference between revisions of "CSC111 Homework 2 2015"
(→Problem 4) |
(→Problem 5) |
||
Line 123: | Line 123: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | =Problem 5= | + | =Problem 5: Triangles= |
<br /> | <br /> | ||
+ | |||
=Problem #1: Triangles= | =Problem #1: Triangles= | ||
Revision as of 21:06, 3 February 2015
--D. Thiebaut (talk) 20:38, 3 February 2015 (EST)
The due date for this homework is Tuesday 2/10 at 11:55 p.m.
Contents
Problem 1
Write a program called hw2_1.py that displays a table of temperatures, the left column in Fahrenheit, and the right column in Celsius. The program asks the user for two numbers, a temperature T, and a number of temperatures n, and displays a conversion table showing n lines of temperature conversions.
Here is an example of how your program should work (the user input is in boldface):
Please enter the starting temperature: 30 How many lines do you want for the table? 10 30 -1.11111111111 31 -0.555555555556 32 0.0 33 0.555555555556 34 1.11111111111 35 1.66666666667 36 2.22222222222 37 2.77777777778 38 3.33333333333 39 3.88888888889
And here is another example:
Please enter the starting temperature: 32 How many lines do you want for the table? 4 32 0.0 33 0.555555555556 34 1.11111111111 35 1.66666666667
- Submit your program on Moodle, in the HW2 PB1 section.
Problem 2
Write a program called hw2_2.py that displays a table of temperatures, the left column in Fahrenheit, and the right column in Celsius. The program asks the user for three numbers:
- a temperature T,
- a number of temperatures n,
- and the step used to skip temperatures.
If the user enters 1 for the step, the output of this program will be the same as the output of the solution program for Problem 1.
Note: we assume that the step will always be positive.
Here is an example of how your program should work (the user input is in boldface):
Please enter the starting temperature: 30 How many lines do you want for the table? 5 What step should be used? 2 30 -1.11111111111 32 0.0 34 1.11111111111 36 2.22222222222 38 3.33333333333
And here is another example:
Please enter the starting temperature: 38 How many lines do you want for the table? 4 What step should be used? 2 38 3.33333333333 37 2.77777777778 36 2.22222222222 35 1.66666666667
- Submit your program on Moodle, in the HW2 PB2 section.
Problem 3
This problem is the same as Problem 1, except that the output now shows degrees Kelvin, as well. A degree Kelvin is computed as degrees celsius + 273.15.
For example, if you have a variable called celsius that contains the temperature expressed in Celsius, and you want to compute the equivalent Kelvin temperature, and store that value in a variable called kelvin, you would write:
kelvin = celsius + 273.15
Here is an example of how your program should behave:
Enter low temperature: 30 How many lines? 5 Fahrenheit Celsius Kelvin 30 -1.11111111111 272.038888889 31 -0.555555555556 272.594444444 32 0.0 273.15 33 0.555555555556 273.705555556 34 1.11111111111 274.261111111
Do not worry about the unpleasantly formatted output. Right now your job is to concentrate on the loop construct. We'll see how to format real numbers later!
Submit your program to the HW2 PB3 section on Moodle.
Problem 4
This problem is the same as Problem 3, except that the output now shows degrees Kelvin listed first on a line, then Celsius, then Fahrenheit.
Here is an example of how your program should behave:
Enter low temperature: 30 How many lines? 6 Fahrenheit Celsius Kelvin 272.038888889 -1.11111111111 30 272.594444444 -0.555555555556 31 273.15 0.0 32 273.705555556 0.555555555556 33 274.261111111 1.11111111111 34 274.816666667 1.66666666667 35
Submit your program to the HW2 PB4 section on Moodle.
Problem 5: Triangles
Problem #1: Triangles
Using python in interactive mode, type the following python statements one after the other, and try to figure out what is going on:
>>> line = "!"
>>> line
>>> line = 5 * "!"
>>> line
>>> line = 20 * "a"
>>> line
>>> n = 7
>>> line = n * "b"
>>> line
Using this new discovery, write a program (calle it hw2a.py) that uses a for-loop to display the following triangle:
*
**
***
****
*****
Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars. When this test program works well, modify it so that it will first ask the user how many lines the triangle should have, and then the program displays triangle with that number of lines.
Here's an example of how your program should work (the user input is underlined):
python hw2b.py How many lines? 7 * ** *** **** ***** ****** *******
When your program works well, you are ready to write the final program, which is the one you will submit for this assignment.
Modify your program (or write a new one) that will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.
Here is an example of how the program will work when the user enters 5:
<u>python hw2a.py</u>
How many lines? <u>5</u>
triangle 1
*
**
***
****
*****
triangle 2
*****
****
***
**
*
triangle 3
*
**
***
****
*****
triangle 4
*****
****
***
**
*
Note that all the triangles are flush against the left margin, but it is okay if your solution has an extra space there, in particular for Triangles 3 and 4.