CPSC 115L: Introduction to Computing Spring 2010

Laboratory 2: Your own first program

September 15, 16

For this lab you are expected to work with an assigned partner as a pair. You should hand in one solution and you will receive the same grade. However, both partners should always save their laboratory work on their own accounts.

Objectives

The main objectives of this laboratory are

Warm-up exercise

Alyssa was asked to design and implement a simple Python script to convert a temperature given in Celsius to the equivalent temperature in Fahrenheit. Here is her I/O specification.
Input: a temperature in Celsius
Output: the equivalent temperature in Fahrenheit
How should Alyssa do this conversion? She knows that the freezing temperature 0 °C is equal to 32 °F, and the boiling temperature 100 °C is equal to 212 °F. From this, she computes the ratio of Fahrenheit to Celsius as follows: (212-32)/100 = 180/100 = 9/5. Then, using f for Fahrenheit and c for Celsius, the conversion formula must have the form f = 9/5 × c + k for some constant k. Since she knows that f = 32 when c = 0, this means that k must be equal to 32. In summary, she comes up with the following algorithm in pseudo code, a concise notation that combines elements of English and Python:
  1. Input a temperature in Celsius and call it c.
  2. Compute f = 9/5 × c + 32.
  3. Output the equivalent temperature f.
Alyssa then translates her algorithm into a Python script as follows:
c = input('Enter a temperature in Celsius: ')
f = (9.0 / 5.0) * c + 32
print 'The temperature in Fahrenheit is', f, 'degrees.'

Lab Exercise 1. Modify Alyssa's solution to design and implement a Python script to do the exact opposite: convert a temperature given in Fahrenheit to the equivalent temperature in Celsius. For this, first write down on a piece of paper your I/O specification and algorithm in pseudo code. Then implement your algorithm in a Python script named f_to_c.py. Run your script with five test cases, including the special cases for the freezing point (32 °F) and boiling point (212 °F), and save the snapshots of your test runs in a text file named f_to_c.out (for this, use Text Editor under Accessories). Does your script gives the correct answers? When completed, show your work to the instructor or TA.

Course-grade calculator

Exercise 2. Design and implement a Python script that will prompt the user for three exam scores and compute the course grade based on these scores. Assume that each of Exams 1 and 2 is worth 30% of the course grade, and Final Exam is worth 40% of the course grade. Assume further that the exam grades are integers between 0 and 100 and the course grade is a real number in the same range. Your script should behave as follows:

Enter the score of Exam 1: 87
Enter the score of Exam 2: 78
Enter the score of Final Exam: 77
Your course grade is: 80.3%

As before, first write down on a piece of paper your I/O specification and algorithm in pseudo code. Then implement your algorithm in a Python script named grade1.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named grade1.out.

Exercise 3. In Exercise 2, we assumed that each of Exams 1 and 2 is worth 30% and Final Exam is worth 40%. Now, modify your design and implementation so that the user can specify how much each exam is worth. Your script should behave as follows:

Enter the score of Exam 1: 87
How much is Exam 1 worth? 0.4
Enter the score of Exam 2: 78
How much is Exam 2 worth? 0.4
Enter the score of Final Exam: 77
How much is Final Exam worth? 0.2
Your course grade is: 81.4%
Implement your modified algorithm in a Python script named grade2.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named grade2.out. When completed, show your work to the instructor or TA.

My Calculator (Optional challenge)

Exercise 4. If time permits and you would enjoy an optional challenge, then design and implement a simple calculator or converter for a problem or topic that interests you. For example, if you are a runner you might implement a program that converts kilometers to miles or calculates one's race pace in minutes per mile. If you are interested in health, you could implement a calculator for body mass index or a program that calculates calories consumed to different forms of exercise. It's up to you to search on line for the facts you need and for ideas on the I/O specifications.

As in the previous exercises, write down your I/O specifications and an outline of your algorithm and discuss these with the instructor or TA before you start to code. When completed, show your work to the instructor or TA.

Documentation For All Exercises

At the beginning of each script, put a header in the following format:
#
# File: f_to_c.py
# Author: Takunari Miyazaki
# Lab section: Tuesday
#
# Created:  02/02/10
# Modified: 02/02/10
#

Learning GNU/Linux's command-line interface

At the core of GNU/Linux is a command-line-based operating system that has its root in the well-known UNIX operating system. Historically, UNIX was mostly used in high-end workstations and servers, but it has become very popular even in low-end personal computers lately (in fact, variants of UNIX are now used in smart phones as well). While you will be using Ubuntu's graphical-user interface (GUI) for the most part, it will also be quite useful to know some basic UNIX commands. For the remainder of this laboratory session, to familiarize yourself with some basic UNIX commands, walk through the first two tutorials of the UNIX Tutorial for Beginners (for this, you must be on this Laboratory 2 webpage and click the link provided). To begin, open Terminal under Accessories. Note that, on our system, the command-line prompt is indicated by $, as opposed to the tutorial's prompt %. When completed, show your work to the instructor or TA.

What to hand in

Upon completion of your laboratory, submit the following in paper:

* CPSC 115L home page
Valid HTML 4.01!