CS 33: Lab #06

CS 33: Computer Organization


LAB 06: Introduction to C Programming

Due 11:59pm Wednesday, November 5.

The program handin33 will only submit files in the cs33/lab/06 directory. (You should run update33 first to set up the directory and create any necessary files.)

Remember: You are encouraged to work with a partner.

In this lab, we will write C programs, write a Makefile, read from "standard in" and write to "standard out".

Each program must follow these following guidelines:

  • the file should be named using the name provided,
  • your code should be compiled using a Makefile,
  • your code should be adequately commented, and
  • your program should follow all input and output guidelines exactly.

  1. Before writing your first program in question 2, write a Makefile to compile it. Your Makefile should have three labels: all, timediff, and clean. The label all will compile everything (which is just timediff for now), timediff will compile timediff.c into the executable timediff, and clean will remove all the executables that this Makefile generates (which is just timediff for now). Before beginning each subsequent question, update the Makefile.
  2. Write a program (called timediff.c) which prompts the user for two times in 24-hour format and displays the difference in hours and minutes between the two times. You should assume that both times are on the same day. Your output must be the same as the output that I have provided. You do not need to do any error checking on the input values for this question: you can assume that the user enters valid times.

    Some examples of times in 24-hour format are:

    • 00:00 is midnight,
    • 09:30 is 9:30 AM,
    • 12:00 is noon,
    • 15:15 is 3:15 PM, and
    • 23:59 is 11:59 PM

    Some hints:
    • To read in values such as 09:30 or 23:59, you will want to write:
        scanf ("%d:%d", &hours, &minutes);
      where hours and minutes are of type int.
    • If you want to use the absolute value function (named abs), you will need to:
        #include <stdlib.h>

    The following are some sample runs of this program. Underlined words are entries that the user typed in:

    Run 1:
    Enter the first time: 10:46
    Enter the second time: 13:51
    The difference between the times is 3 hours and 5 minutes.

    Run 2 - Make sure you say "1 hour" (or "1 minute") not "1 hours" (or "1 minutes"):
    Enter the first time: 13:15
    Enter the second time: 14:30
    The difference between the times is 1 hour and 15 minutes.

    Run 3 - If the hours are 0, don't print them:
    Enter the first time: 17:59
    Enter the second time: 18:00
    The difference between the times is 1 minute.

    Run 4 - If the minutes are 0, don't print them. Note that the second time isn't always later in the day than the first time:
    Enter the first time: 03:22
    Enter the second time: 07:22
    The difference between the times is 4 hours.

    Run 5 - If there is no difference, say so:
    Enter the first time: 09:29
    Enter the second time: 09:29
    There is no difference between the times.

  3. Write a program (called fulldate.c) which converts dates in the form mm/dd/yy into the form Name_of_month day, year. For example, 03/15/02 would become March 15, 2002. If the year is 00-19, assume the user is referring to 2000-2019. However, if the year is 20-99, assume 1920-1999.

    Your program must prompt for and read input in the form mm/dd/yy exactly (including slashes) from the user. Then convert the month into the corresponding month name, and convert the 2-digit year into the correct 4-digit year.

    You must use a switch statement (p. 368) to convert the month into the name of the month. You must use an if statement (p. 344) to convert the 2-digit year into a 4-digit year.

    You should not use a variable to store the name of the month: just print each month name directly (e.g. printf("January");)

    You should print out an error message if the user types in an invalid month (not between 1-12), an invalid year (not between 0-99) or types in an invalid date (depending on the month). If the year is a multiple of 4, February should have 29 days; otherwise it should have 28. Any error should result in main returning a value of 1.

    The following are some sample runs of this program. Underlined words are entries that the user typed in:

    Run 1:
    Enter the date: 11/04/08
    The date is November 4, 2008.

    Run 2:
    Enter the date: 12/31/08
    The date is December 31, 2008.

    Run 3:
    Enter the date: 02/29/08
    The date is February 29, 2008.

    Run 4 - The function main returns 1:
    Enter the date: 02/29/09
    Error: Bad date entered.

    Run 5: - The function main returns 1:
    Enter the date: 13/29/08
    Error: Bad date entered.

    After running each test case, you can check that the return value is correct by typing: echo $? on the command line. $? is a special variable which stores the return value of a program after it finished executing.

  4. A perfect number is a positive integer equal to the sum of all its divisors. For example, the divisors of 28 are 1, 2, 4, 7 and 14, and 1 + 2 + 4 + 7 + 14 = 28. Note that you should not consider 1 to be a divisor of itself. You should first calculate the sum of the divisors, and then compare this to the number itself. If they are equal, the number is perfect.

    Your program (called perfect.c) should calculate and print out the first 4 perfect numbers (one per line); the fifth perfect number is quite large (33550336), so you should be sure you stop after four! Your solution should use one for loop (p. 353) and one while loop (p. 350).