Laboratory Exercises For Computer Science 213

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; hours should be entered as numbers on a 12-hour clock, so that hour 3 should be considered as 3:00 am and hour 8 should be considered as 8:00 pm. Hours outside the range 0 to 12 should be considered invalid.

    [Note: You may not use a 24 hour clock for this problem. Also, 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/195.fa01/lab-intro-C.html

created August 31, 1998
last revised September 14,2001
Valid HTML 3.2!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.