COMP 1671 : Introduction to Computer Science

Lab 4 : Decisions, decisions !


In this lab, you will be using control structures (if - else, switch) to complete the following programs so they do what we require of them. Much of the programs are written - you just need to add code for the decision structures (aka selection structures).
You will also need to draw and turn in flowcharts for the decision statements for each program. If you like, you can number the statements of your code so you can refer to them in the flowchart by number.
Print out and turn in the source code of each of your completed programs, along with their corresponding flowcharts.
 
 

Program 1.

This program asks the user for a year and determines if the year is a leap year. A year is a leap year if it is a multiple of 4. However, if it is a multiple of 100, it is not a leap year unless it is a multiple of 400. Examples of leap years are : 2000, 1996, 1904;  examples of non-leap years are 1999, 1900.
If you feel you need additional variables, feel free to define them.

#include <iostream>
using namespace std;
int main()
{
    int year;         // store year entered by the user
    bool isLeapYear;  // set to true if year is a leap year, else set to false

    // Prompt user for a year, and store it
    cout << "Please enter a year: ";
    cin >> year;

    // check if the year is a leap year and set boolean variable
    //  isLeapYear accordingly:
    

    // check value of isLeapYear and output message stating if the year
    // is a leap year or not:

    return 0;
}

Program 2.

This program asks the user to input an uppercase character and determines if the character is a vowel. Set a flag (a boolean variable) to true if the character is a vowel, and set it to false if the character is not a vowel. Use a switch statement to set the flag, and an if statement to output the appropriate message.
#include <iostream>
using namespace std;
int main()
{
    char inputChar;      // store character input by user
    bool isVowel=false;  // set to true if inputChar is a vowel,
                         // else set to false

    // Prompt user to enter an uppercase character and read input character
    cout << "Please enter a character (A-Z): ";
    cin >> inputChar;

    // If inputChar is a vowel, set isVowel to true, else set it to false:

    // Output message stating whether inputChar is a vowel or not:
    cout << "Character is a vowel." << endl;
    cout << "Character is not a vowel." << endl;

    return 0;
}

Program 3.

This program asks the user to input the age and then takes one of the following actions. If the age is less than 13 or greater than 19, it prints "You are not a teenager". Once it has been determined that the user is not a teenager, the program does one of the following. If the age is less than 13 it prints "You are too young", otherwise it prints "You are older". If the age lies between 13 through 19 print "You are a teenager".
#include <iostream>
using namespace std;
int main()
{
    int userAge;   // used to store user's age

    // Prompt user for age and read the value
    cout << "Please enter your age : ";
    cin >> userAge;

    // Check if user is or is not a teenager and print appropriate message.
    // If they are not a teenager, print message indicating whether user
    // is too young or old to be a teenager

    return 0;
}