Download Lab Assignments - Computer Science I - Fall 2007 | COMP 128 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!
COMP128 Labs Fall 2007 Professor Werner
Labs are worth 4 points maximum. Labs 1 week late are worth 3 points
maximum. Later labs not accepted.
Submit the source code for your program, as well as 2 or 3 test runs
showing sample outputs.
Lab 0. due Sep 11 (no credit for this lab)
Savitch p. 36 Programming Projects 1 – 5. Project 1 is to enter and run the following program in Visual Studio. #include using namespace std; int main( ) { int number_of_pods, peas_per_pod, total_peas; cout << "Press return after entering a number.\n"; cout << "Enter the number of pods:\n"; cin >> number_of_pods; cout << "Enter the number of peas in a pod:\n"; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout << "If you have "; cout << number_of_pods; cout << " pea pods\n"; cout << "and "; cout << peas_per_pod; cout << " peas in each pod, then\n"; cout << "you have "; cout << total_peas; cout << " peas in all the pods.\n"; return 0; } Project 5 is to write a program, which reads 2 integers and outputs their sum and product.
Lab 1 due Sep 18
- Write a program to help carpenters. Sometimes measurements are given in terms of yards, feet and inches. Your program will convert to all inches. When running, it will prompt the user first for the yards, then the feet, then the inches. Assume all numbers are whole. Your program will print out the length in inches. (1 yard = 3 feet, 1 foot = 12 inches)
- Write a program to do the reverse of program 1. It asks the user for a length in inches, and breaks it up into yards, feet and inches.
- (Pro-challenge instead of problems 1 and 2) The user types in a length in feet and inches such as 8’5” and the program types back 101”. Or if the user types 80” the program types back 6’8”.
Lab 2 due Sep 25
- Chapter 2 Programming Project 7 on p.106 (Deals with payroll)
- Write a program to solve a quadratic equation in the form: ax^2 + bx + c = 0 using the quadratic formula. First find the discriminant: d = b^2 – 4ac If the discriminant is negative, print out a message saying the roots are imaginary and stop. If the discriminant is zero, print a message saying there is one real root, and then give the answer, namely –b/2a. If the discriminant is positive, print a message saying there are two real roots and then give their values, namely: r1 = (-b + √d)/(2a) r2 = (-b - √d)/(2a)
Lab 3 due Oct 2
- Chapter 2 Programming Project 8 on p.106 (Deals with inflation)
- Write a program to evaluate the area of a triangle, given the lengths of its sides. Use the formula: a = √(s(s-a)(s-b)(s-c)) where s = (a+b+c)/
February 1 2 3 4 5
Lab 7 due Oct 30
- Write a function to compute the greatest common divisor of two integers using Euclid’s algorithm. Write a driver to test the function.
Euclidean algorithm
From Wikipedia, the free encyclopedia.
The Euclidean algorithm (also called Euclid's algorithm ) is an extremely efficient algorithm to determine the greatest common divisor (GCD) of two integers. It is the oldest algorithm known. It is described in Euclid's Elements. The algorithm does not require factoring. Given two natural numbers a and b , first check if b is zero. If yes, then the GCD is a. If no, calculate c , the remainder after the division of a by b. Replace a with b , b with c , and start the process again.
For example, the GCD of 1071 and 1029 is
computed by this algorithm to be 21 with the
following steps:
a b c 1071 1029 42 1029 42 21 42 21 0 21 0
The algorithm can be formulated in the Python
programming language as follows:
def gcd(a,b): while b != 0: c = a%b a = b b = c return abs(a)
- Write a program to manipulate fractions. The program allows for the addition, subtraction, multiplication and division of fractions. You need to submit a structure chart in addition to the program code and sample runs.
Lab 8 Due Nov 6
- Write a program, which reads a stream of numbers from a file, and writes only the positive ones to a second file. The user enters the names of the input and output files. Use a function named process, which is passed the two opened streams, and reads the numbers one at a time, writing only the positive ones to the output.
- Write a program, which reads a stream of numbers from a file, and prints to the screen, the highest and lowest numbers found.
Lab 9 due Nov 13
Write a student billing program. Student information is kept in an input file. The file has 3 fields, last name, first name, hours. Here is an example: Smith Alice 15 Jones Fred 10 Wang Phil 20 The billing system is as follows: A regular load is from 12.0 to 18.0 hours. The bill is $7,000. An overload is more than 18.0 hours. The student is billed $7,000 for a regular load plus $400 for each excess hour over 18.0. An underload is less than 12 hours. The student is billed $450 per hour. Output is to be written to an output file. It will be in the form of a report with fields for: last name, first name Example: Smith, Alice hours regular hours regular bill overload hours overload bill underload hours underload bill total bill The report should be properly formatted. Plan it on a sheet of paper first to get a good layout. The paper has a width of 80 columns. Hours should always be printed with one decimal place, and dollar amounts with two. Columns should line up. Note: To make this work when printing, you need to use a fixed width font such as Courier.
Lab 10 due Nov 20
- Write a program that analyzes a text file by counting the number of times each of the 26 letters in the alphabet is used. How many a’s, how many b’s, etc. Count capital and lower case versions of the same letter as one.
Lab 14 Extra Credit
Chapter 7 Programming Project 7 on p. 440. It involves storing and adding large integers using arrays of int.
Lab 15 Extra Credit - Strings
- p. 496 #6 (replace digits by x’s)
- p. 497 #11 (converts words to pig latin)
Lab 16 Extra Credit - Classes
p. 590 #3 (Create a class called CDAccount)