CS21 Lab 1: First programs

Due by 11:59pm Tuesday, 8 September 2009

This lab assignment will require you to write three programs in python. Run update21, if you haven't already, to create the cs21/labs/01 directory and copy over any skeleton programs. The program handin21 will only submit files in this directory.

Then move into your cs21/labs/01 directory and create the python programs for lab 1 in this directory (handin21 looks for your lab 1 assignments in your cs21/labs/01 directory):

$ cd cs21/labs/01
$ pwd
  /home/your_user_name/cs21/labs/01

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. Acres to Square Yards Conversion

One acre is equivalent to 4840 square yards. Write a program to convert a number of acres entered by the user to its equivalent number of square yards. Here are some sample runs:

$ python acres_to_yards.py

 This program converts a number of acres
 to its equivalent number of square yards

 Enter the number of acres: 4
 The number of square yards in 4 acres is 19360

$ python acres_to_yards.py

 This program converts a number of acres
 to its equivalent number of square yards
 
 Enter the number of acres: 6.4
 The number of square yards in 6.4  acres is  30976.0


2. Print cubes
In the same labs directory, create a program that asks the user for an integer (n) and then prints out all cubes of 1 to n.

Here are some example runs of a working program:

$ python cubes.py 

 This program prints the cubes of 1 to n
 Enter a value for n: 10
 1 8 27 64 125 216 343 512 729 1000

$ python cubes.py 

 This program prints the cubes of 1 to n
 Enter a value for n: 4
 1 8 27 64
It is okay if your program prints one cube value per line rather than all values on the same line (either way is fine):
$ python cubes.py 

 This program prints the cubes of 1 to n
 Enter a value for n: 3
 1
 8
 27


3. Conversion Table
In the same labs directory, write a program that prints a conversion table of dollars to Euros. Use a conversion rate of 1 dollar is 0.7008 Euros. The user will enter a starting and ending dollar value for the range of conversions to print.

To get the values to line up in columns you can print out one or more tab characters ("\t") between printing the dollar and euros amount on each line. For example, try these print statements in python and see what happens:

print 3, "\t\t", 4
print 3, "\t", 4
Here are some example runs of a working program:
$ python dollars_to_euros.py 
 
This program prints a conversion table of US dollars
to Euros of a range given by the usr.

Enter the starting dollar amount for the range: 4
Enter the ending dollar amount: 10

Dollars          Euros
----------------------------
  4             2.8032
  5             3.504
  6             4.2048
  7             4.9056
  8             5.6064
  9             6.3072
  10            7.008


$ python dollars_to_euros.py 

This program prints a conversion table of US dollars
to Euros of a range given by the usr.

Enter the starting dollar amount for the range: 31
Enter the ending dollar amount: 47

Dollars          Euros
----------------------------
  31            21.7248
  32            22.4256
  33            23.1264
  34            23.8272
  35            24.528
  36            25.2288
  37            25.9296
  38            26.6304
  39            27.3312
  40            28.032
  41            28.7328
  42            29.4336
  43            30.1344
  44            30.8352
  45            31.536
  46            32.2368
  47            32.9376
Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.