Assignment 17
Due Wednesday, March 14 at 9:20am on Canvas


Assignment 17.1: Function Worksheet 2 (10 pts)

  • Copy and paste the starter code into a new file called funFunctions2.cpp
  • Write the required functions as described by the prototypes and comments.
  • The functions should be written below main.
  • Then, run the code when you are finished to see if you wrote the functions correctly.
  • Check the test results and make any alterations to your functions as necessary.
  • When all of the tests pass, upload your code to Canvas.


/**
 *
 * CIS 22A
 */

#include <iostream>
#include <cmath>
using namespace std;

bool icyHot(int temp1, int temp2);
//Given two temperatures, return true if one is less than 0
//and the other is greater than 100.
//icyHot(120, -1) → true
//icyHot(-1, 120) → true
//icyHot(2, 120) → false

bool startHi(string str);
//Given a string, return true if the string starts
//with "hi" and false otherwise.
//startHi("hi there") → true
//startHi("hi") → true
//startHi("hello hi") → false

string frontTimes(string str, int n);
//Given a string and a non-negative int n,
//the front of the string is the first 3 chars,
//or whatever is there if the string is less than length 3
//Return n copies of the front;
//Hint: use a for loop and string concatenation
//frontTimes("Chocolate", 2) → "ChoCho"
//frontTimes("Abcd", 3) → "AbcAbcAbc"
//frontTimes("Oy", 4)  → "OyOyOyOy"

int close10(int a, int b);
//Given 2 int values, return whichever value is nearest to the value 10,
//or return 0 in the event of a tie.
//Hint: recall that fabs(n) returns the absolute value of a number.
//close10(8, 13) → 8
//close10(13, 8) → 8
//close10(13, 7) → 0

bool doubleX(string str);
//Given a string, return true if the first instance of "x" in
//the string is immediately followed by another "x".
//Hint: use a for loop and string indexing ([] syntax)
//doubleX("axxbb") → true
//doubleX("axaxax") → false
//doubleX("xxxxx") → true

int main() {
    int result;
    bool answer;
    string value;
    cout << boolalpha;
    cout << "***Testing icyHot***" << endl << endl;
    answer = icyHot(120, -1);
    cout << "Should print true: " << answer << endl;
    answer = icyHot(-1, 120);
    cout << "Should print true: " << answer << endl;
    answer = icyHot(2, 120);
    cout << "Should print false: " << answer << endl << endl;

    cout << "***Testing startHi***" << endl << endl;
    answer = startHi("hi there");
    cout << "Should print true: " << answer << endl;
    answer = startHi("hi");
    cout << "Should print true: " << answer << endl;
    answer = startHi("hello hi");
    cout << "Should print false: " << answer << endl;

    cout << "***Testing frontTimes***" << endl << endl;
    value = frontTimes("Chocolate", 2);
    cout << "Should print ChoCho: " << value << endl;
    value = frontTimes("Abcd", 3);
    cout << "Should print AbcAbcAbc: " << value << endl;
    value = frontTimes("Oy", 4);
    cout << "Should print OyOyOyOy: " << value << endl << endl;

    cout << "***Testing close10***" << endl << endl;
    result = close10(8, 13);
    cout << "Should print 8: " << result << endl;
    result = close10(13, 8);
    cout << "Should print 8: " << result << endl;
    result = close10(13, 7);
    cout << "Should print 0: " << result << endl << endl;

    cout << "***Testing doubleX***" << endl << endl;
    answer = doubleX("axxbb");
    cout << "Should print true: " << answer << endl;
    answer = doubleX("axaxax");
    cout << "Should print false: " << answer << endl;
    answer = doubleX("xxxxx");
    cout << "Should print true: " << answer << endl << endl;

    cout << "***End of Tests***" << endl;

    return 0;
}


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;

  • 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 Canvas.


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