Lab 3 -- Selection Structures
CPE101
Winter 2008

Updates

    None yet.

Objectives

Overview

    Control structures modify the flow of execution in a program. You can use control structures to run a particular part of your program while skipping some other portions.

    In this lab, you will be looking at the if-else and switch control statements. In order for your control structures to properly decide which code to execute, the structures must have properly coded condition tests. These conditions are simple expressions which will evaluate to either true or false. You use these conditions to decide whether or not to run some particular code. For example, to run some code if an integer a equals and integer b, use the statement:

    if (a == b) {
        ...run these commands if the condition is true...
   }

   You can include an 'else' also:

    if  (a == b) {
       ...run these commands if the condition is true...
   } else {
       ...otherwise run these commands...
   }

    Logical operators are used to combine multiple condition tests in an 'if' statement. For example, if you would like to run some code only if a equals b and c equals d, then you would use the following:

    if (a == b && c == d) {
        ...run these commands if the condition is true...
   }
operand 1 operand 2 result
false false false
false true false
true false false
true true true
operand 1 operand 2 result
false false false
false true true
true false true
true true true

Lab Requirements

    For this lab, you will write a program that determines the day of the year (1 to 366) for a given date.

    You can determine if a year is a leap year with the following rules:

    Part 1

    Write a program which accepts 3 integers. The integers will represent the month, day, and year for a date. The program should prompt for each number individually.  Here are some sample runs:

	Enter the month: 3
Enter the day: 1
Enter the year: 2000

Day number: 61



Enter the month: 3
Enter the day: 1
Enter the year: 2001

Day number: 60



Enter the month: 3
Enter the day: 1
Enter the year: 1900

Day number: 60

    Your program should have a function leap that returns a 1 if the argument is a leap year. It should return 0 otherwise.

    If the user enters an invalid day, month, and/or year, the program should output a day number even if the answer is incorrect.  

    Part 2

    Modify Part 1 so that your program does error checking.  Your program should check that the month is between 1 and 12.  It should also check that the day is between 1 and 31.  It does not have to check that the day is valid for a given month.

	Enter the month: 13
Enter the day: 1
Enter the year: 2008

Invalid date

Handin

    1. If necessary, move your source (part1.c and part2.c) to vogon.
    2. Log on to vogon via some terminal somewhere.
    3. Change directory (cd), as necessary, to the directory where your source file(s) are.
    4. Compile and test your source on vogon using the required compiler flags (-Wall -ansi -pedantic)
    5. Use the handin command being sure to replace xx with your lecture section number.

    11:59 vogon ~$ handin mhaungs Lab3-xx part1.c part2.c

Demonstration

You will be required to demonstrate you program in lab on the due date.