Assignment 17
Due Tuesday, November 29 at 3:20pm on Catalyst


Assignment 17.1: Into the Void (10 pts)

  • Void functions are useful for printing out information in a particular format.
  • Let's consider dates and times.
  • In America, we use the 12 hour clock, but in Europe, the 24 hour clock is used. For example, in America, 8:30 at night is represented as 8:30pm, while in Europe, it is represented as 20:30.
  • In America, we write dates in this format MM-DD-YYYY. In Europe, dates are often written as DD.MM.YYYY
  • Let's write a program that uses void functions to format dates and times.
  • We will print each date and time in both the American and European formats for our user.
  • Open up CodeBlocks and create a new C++ file named dateTime.cpp.
  • Copy and paste the starter code below into your file:
/*
* Name(s)
* Section info
*/
#include<iostream>
using namespace std;

//write function prototypes and comments here

int main() {
    int year;
    int day;
    int month;
    int hour;
    int minutes;
    string dayEve;

    cout << "Welcome! This program will print dates and times in both the American and European styles!\n\n";

    cout <<"First, let's print a formatted date.\n\n";
   
    cout << "Please enter the current year: ";
    cin >> year;
    cout << "Please enter the current month: ";
    cin >> month;
    cout << "Please enter the current day: ";
    cin >> day;
    cout << endl;
  
    //call to the formatDateAmerican function here
    //call to the formatDateEuropean function here
   
    cout << "\nNow, let's print a formatted time.\n\n";
   
    cout << "Please enter the current hour: ";
    cin >> hour;
    cout << "Please enter the current minutes: ";
    cin >> minutes;
    cout << "Please enter whether it is \"morning\" or \"evening\": ";
    cin >> dayEve;
    cout << endl;

   
    //call to the formatTimeAmerican function here
    //call to the formatTimeEuropean function here

    cout << "\nBye! See you another day!" << endl;

    return 0;
}

//write functions here

  • Now, you need to write four functions as follows:
formatDateAmerican
takes as input three integer parameter, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format m/d/yyyy
returns nothing
formatDateEuropean
takes as input three integer parameters, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format d.m.yyyy
returns nothing
formatTimeAmerican
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the format H:MMam or H:MMpm
returns nothing
formatTimeEuropean
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the 24 hour clock. Note that there is no am or pm in this format.
returns nothing

  • These functions should be written below main, and their prototypes and above main.
  • Please provide a comment below each function prototype as described in class.
  • When you are finished writing your functions call them in main in the place indicated by the comments.
  • Upload your dateTime.cpp file to Catalyst.

Your output should look identical the output below when you are finished:



Assignment 17.2: Squares, Triangles, Rectangles (10pts)
  • Open up your squares.cpp file from class and let's alter it!
  • We want our program to be able to print out triangles and rectangles as well as squares.
  • Add two additional methods to the top of your program
    • printTriangles()
      • Takes in an integer argument for the length of the base of the triangle.
      • Returns nothing.
      • Prints a triangle using a nested for loop.
    • printRectangles()
      • Takes in an integer argument for the length.
      • Takes in an integer argument for the width.
      • Returns nothing.
      • Prints a rectangle using a nested for loop.
  • Use your triangle code from our previous triangles assignment or use the starter code below for drawing squares and edit it to print a triangle.
  • For the rectangle code, try altering the code to print a square by using the length and width parameters in place of the size.
//Alter the code below to print rectangles by removing size and replacing it with length and width.
for (int row = 1; row <= size; row++)
    {
        for (int col = 1; col <= size; col++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
  • Now, we need to alter the program so that it prints out various shapes for the user.
  • Declare 5 variables at the top of your program:
int length;
int width;
int base;
String shape;
string repeat;
  • Welcome your user to the program with the message below.
I will print squares for you!
Rectangles and triangles, too!
  • Then ask the user what type of shape he/she would like printed.
Enter the shape that you would like to print (rectangle, triangle or square): _
  • The user must be able to enter upper and lower case versions of the shape names, i.e. both "Rectangle" and "rectangle"
  • Depending on the shape selected, the program will prompt the user to enter the length of a side (square), length of the base (triangle) or length and width of the sides (rectangle). Hint: Use if - else if -else statements here.
Enter the length of the rectangle: _
Enter the width of the rectangle: _
  • Store the information in the appropriate variable that you declared at the beginning of main (length and width for the rectangle, base for the triangle, length for the square).
  • Then call the specific method to print out that shape, with size determined by the user input.
  • There should also be an error message in your else clause if the user did not enter one of the recognized shapes.
You entered an invalid shape!
  • The program should prompt the user about whether he or she wishes to print another shape. Hint: Use a while loop around most of your code.
Would you like me to print more shapes for you (y/n)? _
  • As a final message, make sure the following prints to the screen before the program finishes. Hint: the print statement should go below and outside the while loop.
Thanks for "square" dancing with me!
  • When you are finished, your should have output like below.
  • Upload your squares.cpp source code to Catalyst.


The output of your program should look identical to the output below: