| CSC 153 | Grinnell College | Spring, 2009 |
| Computer Science Fundamentals | ||
This laboratory exercise provides practice with basic elements of the C programming language. Specifically, the lab emphasizes conditional (if) and looping (for, while) constructs.
Classifying Triangles:
[This problem is based on an exercise by Darrah Chavey, Beloit
College.]
Write a C program that reads the lengths of three sides of a triangle and prints out the classification of the triangle as follows:
In this problem, you should assume that the input values are all numbers, although error checking may indicate some numbers are negative or zero.
Computing the Greatest Common Divisor (Euclidean Algorithm):
[The following is an edited and abridged version of Section 4.1 from
Problems for Computer Solutions Using FORTRAN by Henry M. Walker,
Winthrop Publishers, 1980 and is used with permission of the copyright holder.]
Let N and M be two positive integers. The greatest common divisor of N and M, denoted gcd (M, N), is defined to be the positive integer D such that
For example, 2 = gcd (6, 8); 4 = gcd (4, 12); 1 = gcd (8, 9); 6 = gcd (66, 24).
Algorithm: The algorithm proceeds by long division -- keeping track of subsequent remainders:
This process continues until we find a remainder Ri+1 which is 0. Then Ri = gcd (M, N).
Example:
Thus, 6 = gcd (66, 24).
Write a C program that reads two positive integers from the keyboard and computers and prints their greatest common divisor -- using the Euclidean Algorithm.
(Challenge Problem — Optional)
A Baby Sitting Problem:
A baby sitter charges $1.50 per hour until 9:00 pm (while the kids
are still up), $1.00 per hour between 9:00 pm and midnight, and $1.25 per
hour after midnight (since late night baby sitting interferes with morning
classes).
Write a program that reads the sitter's starting time in hours and minutes and the ending time in hours and minutes and then computes the sitter's fee. Assume all times are between 6:00 pm and 6:00 am, and hours should be entered as being between 0 and 12 (inclusive). Hours outside the range of 0 to 12 should be considered invalid.
The following table may clarify allowed time values for this problem.
| Starting | Starting | Ending | Ending | Starting | Ending |
|---|---|---|---|---|---|
| Hour | Minutes | Hour | Minutes | Time | time |
| 8 | 0 | 3 | 30 | 8:00pm | 3:30am |
| 6 | 0 | 0 | 45 | 6:00pm | 12:45am |
| 12 | 0 | 6 | 0 | 12:00am (midnight) | 6:00am |
Additional Notes:
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/201.sp05/lab-intro-c.html
|
created 31 August 1998 last revised 8 April 2008 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |