Lab 06: Introduction to C

Due 11:59pm Wednesday November 2, 2011


The program handin33 will only submit files in the cs33/labs/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, use a Makefile, read from "standard in" and write to "standard out".

Each program must follow these following guidelines:

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

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

  3. 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 the first four perfect numbers and then return the fourth perfect number. Your solution should use one for loop (p. 353) and one while loop (p. 350). There should be no input or output. You can verify your program works by typing echo $? in the terminal after the program ends. For verification, the first four perfect numbers are 6, 28, 496, and 8128, so your program should return 8128. NOTE/WARNING: Only 1 byte is displayed when you echo $? -- so if you return 8128 from your program, the terminal will only display 192. WHY?

    When you are done and you are sure your program works, have gcc generate the assembly language code (gcc -S -m32 perfect.c). Edit the file perfect.s and add comments into the file that explain what is happening in the program. Do not delete any lines of that file.

    You may find the x86 Assembly book on wikibooks to be a helpful resource for instructions that you can't decipher. Also, use google to track down any additional instructions you have trouble figuring out.

    You can write comments in assembly by using the number sign (#). Anything from that symbol to the end of the line is a comment. For example:

    #save the old base pointer and set up the new base pointer
    	pushl	%ebp        #push the old base pointer onto the stack
    	movl	%esp, %ebp  #set the new base pointer to be the stack pointer
    Note that after you edit .s file, you should still be able to compile that into a running program as follows: gcc -m32 -o perfect perfect.s (not perfect.c).