CSC 201 Grinnell College Spring, 2005
 
Data Representation, Memory Management, and Formal Methods
 

Laboratory Exercise on Elementary C Programming

Goals

This laboratory exercise provides practice with basic elements of the C programming language. Specifically, the lab emphasizes conditional (if) and looping (for, while) constructs.

Problems

In this lab, you are to write solutions to the following two problems. In each case, turn your solutions utilizing the course's specified format for submitting assignments.


  1. 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 StartingEnding
    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:

    1. As illustrated in this table, an hour of 0 or 12 is interpreted the same, as midnight.

    2. You may not use a 24 hour clock for this problem.

    3. Times must be given in hours and minutes (as two nonnegative integer values).


  1. 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

    1. D divides N and M, and
    2. D is the largest integer dividing both N and M; i.e., every integer E that divides both N and M also divides D.

    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:

    1. Divide 66 by 24: remainder is 18.
    2. Divide 24 by 18: remainder is 6
    3. Divide 18 by 6: remainder is 0, so we can stop.

      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.


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/201.sp05/lab-intro-c.html

created August 31, 1998
last revised February 11, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.