Programming Assignment: P4 (Conditional Statements)

  1. Read the following sample program to find out the output with the following input values, and then run the program on computer and compare the output you found manually with the output from computer.
    1. Test case #1: If the letter C and the value 100 are input, what is the output?
    2. Test case #2: If the letter F and the value 32 are input, what is the output?
    3. Test case #3: If the letter c and the value 0 are input, what is the output?

    // Program Convert converts a temperature from Fahrenheit to 
    // Celsius or a temperature from Celsius to Fahrenheit,
    // depending on whether the user enters an F or a C.
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char letter;          // Place to store input letter
      double tempIn;        // Temperature to be converted
      double tempOut;       // Converted temperature
    
      cout << "Input Menu" << endl << endl;
      cout << "F or f:  Convert from Fahrenheit to Celsius" << endl;
      cout << "C or c:  Convert from Celsius to Fahrenheit" << endl;
      cout << "Type a C (or c) or an F (or f), then press return." << endl;
      cin  >> letter;
    
      cout << "Type a temperature value, then press return." << endl;
      cin  >> tempIn;
    
      if (letter == 'C' || letter == 'c')
        tempOut = (9.0 * tempIn / 5.0) + 32.0;
      else if (letter == 'F' || letter == 'f')
        tempOut = 5.0 * (tempIn - 32.0) / 9.0;
      else
      {
        cout << "Invalid input letter!" << endl;
        return 0;
      }
      
      cout << "Temperature to convert: " << tempIn << endl;
      cout << "Converted temperature:  " << tempOut << endl;
      return 0;
    }
    
  2. Examine the following pairs of expressions and determine if they are equivalent (≡). Just say Yes or No. Hint: Use DeMorgan's Law.
     
    Assume that variables x, y and z are of type int.
    
    Case#: Expression 1               ≡       Expression 2 
    #1: !(x == y)                     ≡       x != y          
    #2: !((x == y) || (x == z))       ≡      (x != y) && (x != z)              
    #3: !((x >=0) && (x <=100))       ≡      (x < 0) || (x > 100)   
    #4: !(x != 10)                    ≡      x == 10 
    
  3. Examine the following pairs of expressions and determine if they are equivalent. Just say Yes or No.
    Assume that variables A, B, and C are of type bool.
    
    Case#: Expression 1    ≡      Expression 2 
    #1: !A && B            ≡      B && !A 
    #2: !A || B            ≡      B || !A 
    #3: !A                 ≡      A == false 
    #4: A || B && C        ≡     (A || B) && C 
    #5: A && B || C        ≡      (A && B) || C 
    
  4. Write a program that first prompts the user to enter a year as an int value and checks if it is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400. Test each of the years: 2020, 2040, 2000, 2021, 2100, 2200 and submit test results.
  5. Write a program that prompts the user to enter his/her weight in lbs and height in feet and inches and then calculate Body Mass Index (BMI) for the user. Print out the corresponding interpretation message. Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms (1 pound = 0.4536 kilograms) and dividing by the square of your height in meters (1 foot = 12 inches = 0.3048 meters). The interpretation of BMI for people 16 years or older is as follows:
            BMI < 18.5        Underweight
    18.5 <= BMI < 25.0        Normal
    25.0 <= BMI < 30.0        Overweight
    30.0 <= BMI               Obese
    
    Example test:
    Enter your weight (lbs):
    190
    Enter your height (two numbers for feet and inches separated by a space):
    6 2
    
    Your BMI is 24.4 kg/m^2   (Normal)
    
  6. (Bonus 15 points) Write a program to calculate the day of the week. The formula is given below:
    \( h=\large(q+\frac{13(m+1)} {5} + k + \frac{k}{4} + \frac{j}{4} + 5j \large)\%7 \\ \) where Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week. All the divisions in the formula should be integer divisions. Here are some sample runs:
    Enter year: (yyyy): 2015
    Enter month: 1-12: 1
    Enter the day of the month: 1-31: 25 
    Day of the week is Sunday
    
    Enter year: (yyyy): 2023 
    Enter month: 1-12: 2
    Enter the day of the month: 1-31: 14 
    Day of the week is Tuesday
    
    Note that January and February are counted as 13 and 14 in the formula, so you need to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year. For example, if the user enters 1 for m and 2023 for year, m would be 13 and year would be 2022 to be used in the formula.
  7. At the top section of your program you need to use C++ comments to include your name, class name, programming assignment name, and date the program was written.
  8. Your programs should be properly formatted using indentation and empty lines (spaces). Use "camel" notation for variable naming.
  9. Upon completion, save all programs and the required output into a single text file using notepad (or notepad++). The file should be named in this format: P4_YourName.txt, for example, P4_JohnDoe.txt. Then submit to Canvas as attachment by the deadline. The submission link on Canvas will be closed automatically after the deadline. Assignments that fail to follow the instructions will NOT be graded.