CS303E Homework 3

Instructor: Dr. Bill Young
Due Date: Monday, September 16, 2024 at 11:59pm

Copyright © William D. Young. All rights reserved.

Length Conversions

Frequently in life you'll need to convert from one unit to another: feet to meters, hours to seconds, degrees to radians, etc. Usually, the conversion simply involves a simple conversion factor, meaning that you multiply or divide by a real number.

In this assignment, you'll input a length as some number of feet and inches, and convert that to a number of other length values. Finally, you'll print out the results in a nice format. See the examples below.

How to ask for user input: In your program you'll need to prompt the user to enter the number of feet and the number of inches to convert. The input function is not covered until slideset 3, but it's pretty easy. To get the values of the feet and inches into two variables inputFeet and inputInches, you'll include the following two statements:

inputFeet = int( input("Enter number of feet: ") )
inputInches = int( input("Enter number of inches: ") )
When each statement is executed, it prints the prompt (e.g., "Enter number of feet: ") on the screen, and waits for the user to enter a number. After the user types in a number, say 10000, this number is read by the program as a string, say "10000". Then the function int() will convert that string into the corresponding integer value which will be stored in the variable inputFeet. The second statement will do an analogous thing for variable inputInches. From there you're good to go. Notice we didn't name those variables feet and inches because we'll use those for some other values.

You can assume that the values entered are positive integers. (It would be easy to convert this program to accept floats; that would only require changing one word in each input statements above. Can you guess what you'd change?) Your program doesn't have to check that and doesn't have to work if the user enters a bad value. We won't test your program on illegal values.

Doing the conversions: After you've input those two values, you'll then do the conversions indicated in the following table. We suggest you do them in this order.

inches inputFeet times 12, plus inputInches
feet inches divided by 12
yards feet divided by 3
miles feet divided by 5280
meters feet times 0.3048
centimeters meters times 100
millimeters centimeters times 10
kilometers meters divided by 1000

You will then print out a nice report of the conversion following the sample output below. There is a single space after each colon and two spaces before the items that are indented. Leave blank lines where shown. You don't need to round any values. You should follow this model exactly. (Long decimal fractions could vary slightly; that's OK.)

Note that your program must work for arbitrary legal input values, not merely the values illustrated below. So be sure to test it on a variety of input values. But remember, it's only required to work for positive integers.

> python ConvertUnits.py
Enter number of feet: 5
Enter number of inches: 6

5 feet and 6 inches equals:

English Units
  feet: 5.5
  inches: 66
  yards: 1.8333333333333333
  miles: 0.0010416666666666667 

Metric Units
  meters: 1.6764000000000001
  centimeters: 167.64000000000001
  millimeters: 1676.4
  kilometers: 0.0016764000000000002

> python ConvertUnits.py
Enter number of feet: 20000
Enter number of inches: 0

20000 feet and 0 inches equals:

English Units
  feet: 20000.0
  inches: 240000
  yards: 6666.666666666667
  miles: 3.787878787878788

Metric Units
  meters: 6096.0
  centimeters: 609600.0
  millimeters: 6096000.0
  kilometers: 6.096

> python ConvertUnits.py 
Enter number of feet: 0
Enter number of inches: 19

0 feet and 19 inches equals:

English Units
  feet: 1.5833333333333333
  inches: 19
  yards: 0.5277777777777778
  miles: 0.00029987373737373735 

Metric Units
  meters: 0.48260000000000003
  centimeters: 48.260000000000005
  millimeters: 482.6
  kilometers: 0.0004826

>  python ConvertUnits.py 
Enter number of feet: 0
Enter number of inches: 0

0 feet and 0 inches equals:

English Units
  feet: 0.0
  inches: 0
  yards: 0.0
  miles: 0.0 

Metric Units
  meters: 0.0
  centimeters: 0.0
  millimeters: 0.0
  kilometers: 0.0

>

Turning in the Assignment:

The program should be in a file named ConvertUnits.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment hw3 under the assignments sections by uploading your Python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: ConvertUnits.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

If you submit multiple times to Canvas, it will rename your file name to something like ConvertUnits-1.py, ConvertUnits-2.py, etc. Don't worry about that; we'll grade the latest version.

Programming Tips:

Many assignments this semester will include this section called Programming Tips. In general, these are not hard requirements about this specific assignment; they are suggestions to help you become better programmers. So read and apply them! Some may repeat. That's either because I found that topic especially relevant to several different assignments (or I forgot that I'd already included it).

Exact vs. Approxiate Computation: Python implements integers via a technique often called "bignums." Many programming languages like C, use fixed precision integer arithmetic, representing an integer in 32 or 64 binary digits (bits). If your arithmetic operation in C yields a result that is too large to store in that number of bits the result is well-defined but inaccurate in the mathematical sense.

Bignum arithmetic allows the use of an arbitrary number of bits to store an integer. If more 8-bit bytes of memory are needed to store the result, more are allocated. (Of course, there is a limit to what can be represented in any finite memory; e.g., you could store all of the digits in a googol, but not of a googolplex. But the limit on modern computers is big enough that it's usually not going to be a problem!) This means that Python can easily perform arithmetic computations that would require some special programming in C: e.g. computing 21000. Such computations are exact and mathematically correct.

Computations with floats, on the other hand, are approximate for two reasons. First, floats are stored in a fixed number of bits, following the IEEE floating point standard. Each Python float typically is stored in 64 bits (8 bytes). You can think of this representation as storing a real number in scientific notation with a sign, significand, and exponent. There are clearly limits to the range of numbers that can be stored in 64 bits.

Second, real numbers can be of arbitrary (including infinite) precision. For example, the fraction 1/3 is represented in decimal (base 10) as 0.3333... and in binary (base 2) as 0.010101... In neither case can it be represented in a finite number of digits/bits. So any representation is approximate and any computation involving this number is necessarily approximate.

>>> 1.1 * 3
3.3000000000000003

This is not to say that the results are unpredictable. The results are well-defined and can be computed precisely using the IEEE FP standard. That's well beyond the scope of this class.