Assignment 17
Due Tuesday, November 28 at 3:20pm on Canvas

Assignment 17.1: Function Worksheet (10 pts)

  • Copy and paste the starter code into a new file called funFunctions.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>
using namespace std;

int sumDouble(int a, int b);
//Given two int values, return their sum.
//Unless the two values are the same, then return double their sum.
//sumDouble(1, 2) → 3
//sumDouble(3, 2) → 5
//sumDouble(2, 2) → 8

bool makes10(int a, int b);
//Given 2 ints, a and b, return true if one of them is 10
// or if their sum is 10.
//makes10(9, 10) → true
//makes10(9, 9) → false
//makes10(1, 9) → true


bool monkeyTrouble(bool aSmile, bool bSmile);
//We have two monkeys, a and b, and the parameters
//aSmile and bSmile indicate if each is smiling.
//We are in trouble if they are both smiling
//or if neither of them is smiling.
//Return true if we are in trouble.
//monkeyTrouble(true, true) → true
//monkeyTrouble(false, false) → true
//monkeyTrouble(true, false) → false


bool or35(int a);
//Return true if the given non-negative number is a multiple of 3
//or a multiple of 5. Use the % "modulus" operator
//or35(3) → true
//or35(10) → true
//or35(8) → false


string notString(string str);
//Given a string, return a new string where "not " has been added to the front.
//However, if the string already begins with "not", return the string unchanged.
//notString("candy") → "not candy"
//notString("x") → "not x"
//notString("not bad") → "not bad"


string frontBack(string str);
//Given a string, return a new string where the first and last chars have been exchanged.
//frontBack("code") → "eodc"
//frontBack("a") → "a"
//frontBack("ab") → "ba"


bool hasTeen(int num1, int num2, int num3);
//We'll say that a number is "teen" if it is in the range 13..19 inclusive.
//Given 3 int values, return true if 1 or more of them is/are teen.
//hasTeen(13, 20, 10) → true
//hasTeen(20, 19, 10) → true
//hasTeen(20, 10, 13) → true



int main()
{
    int result;
    bool answer;
    string value;
    cout << "***Testing sumDouble***"<< endl << endl;
    result = sumDouble(1, 2);
    cout << "Should print 3: " << result << endl;
    result = sumDouble(3, 2);
    cout << "Should print 5: " << result <<endl;
    result = sumDouble(2, 2);
    cout << "Should print 8: " << result << endl << endl;
   
    cout << "***Testing makes10***"<< endl << endl;
    answer = makes10(9, 10);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = makes10(9, 9);
    cout << "Should be false: " << answer << endl;
    answer = makes10(1, 9);
    cout << "Should be true: " << answer << endl;
       
    cout << "***Testing monkeyTrouble***"<< endl << endl;
    answer = monkeyTrouble(true, true);
    cout << "Should be true: " << answer << endl;
    answer = monkeyTrouble(false, false);
    cout << "Should be true: " << answer << endl;
    answer = monkeyTrouble(true, false);
    cout << "Should be false: " << answer << endl << endl;
   
    cout << "***Testing or35***"<< endl << endl;
    answer = or35(3);
    cout << "Should be true: " << answer << endl;
    answer = or35(10);
    cout << "Should be true: " << answer << endl;
    answer = or35(8);
    cout << "Should be false: " << answer << endl << endl;
   
    cout << "***notString***"<< endl << endl;
    value = notString("candy");
    cout << "Should be not candy: " << value << endl;
    value = notString("x");
    cout << "Should be not x: " << value << endl;
    value = notString("not bad");
    cout << "Should be not bac: " << value << endl << endl;
   
    cout << "***frontBack***"<< endl << endl;
    value = frontBack("code");
    cout << "Should be eodc: " << value << endl;
    value = frontBack("a");
    cout << "Should be a: " << value << endl;
    value = frontBack("ab");
    cout << "Should be ba: " << value << endl << endl;
   
   
    cout << "***Testing hasTeen***"<< endl << endl;
    answer = hasTeen(13, 20, 10);
    cout << "Should be true: " << answer << endl;
    answer = hasTeen(20, 19, 10);
    cout << "Should be true: " << answer << endl;
    answer = hasTeen(20, 10, 13) ;
    cout << "Should be true: " << answer << endl;
    answer = hasTeen(20, 10, 45) ;
    cout << "Should be false: " << answer << endl << endl;
   
    cout << "***End of Tests***" << endl;

    return 0;

}


Assignment 17.2: Valid Email Address? (10 pts)
  • Let's revise our program from a previous homework to use functions for checking an email address.
  • For the purposes of this assignment, we will use three criteria to determine if an email address is valid:
    • The address must end with one of the following extensions: .com, .org or .edu
    • The address must not contain a space
    • The address must have an @ character contained somewhere in the string
  • You will write 3 functions, one to check each of the above conditions.
  • Your first function signature should look like this:

bool validExtension(string address) {

    //function body will go here

}

  • The second function signature should look like this:

bool containsSpace(string address) {

    //function body will go here

    }

  • The third function signature should look like this:

bool containsAt(string address) {

    //function body will go here

    }
  • Hint 1: You will want to move your for loops for checking for the space and the at into the appropriate function body.
  • Hint 2: Do not alter the starter code in any way. Your job is only to add to it where indicated below:
/*
 * Name
 * Section
 */

#include <iostream>
using namespace std;

//**WRITE PROTOTYPE HERE FOR FUNCTION DESCRIBED IN BELOW COMMENT**
//determines whether an email address has the proper extension
//returns true if the email address ends in .org, .edu or .com
//returns false otherwise

//**WRITE PROTOTYPE HERE FOR FUNCTION DESCRIBED IN BELOW COMMENT**
//determines whether a string contains a space
//returns true if the email address contains a space and false otherwise

//**WRITE PROTOTYPE HERE FOR FUNCTION DESCRIBED IN BELOW COMMENT**
//determines whether an email address contains an @ symbol
//returns true if the email address contains an @ symbol and false otherwise

int main()
{
    string address;
    cout << "Enter your email address: ";
    getline(cin, address);

    if (!containsAt(address)) { //the function will return true or false so we can check in an if statement
        cout << "Invalid email. Your email address must contain an @ symbol" << endl;
    } else if (??????????) { //fill in function call here
        cout << "Invalid email. Your email address cannot contain a space" << endl;
    } else if (???????????) { //fill in function call here
        cout << "Invalid email. Your email must contain a .com, .edu or .org extension" << endl;
    } else {
        cout << "Your email address is valid." << endl;

    }

}

bool validExtension(string address) {
    //write function body here
}

bool containsSpace(string address) {
   //write function body here
}

bool containsAt(string address) {
    //write function body here  
}

When your program is giving the correct output, upload your source code to Canvas.


Enter an email address: bob@jobs.net
Invalid email. Your email must contain a .com, .edu or .org extension.

Alternately,

Enter an email address: bobajobs.com
Invalid email. Your email must contain an @ symbol.

Alternately,

Enter an email address: bob @jobs.com
Invalid email. Your email address cannot contain a space.

Alternately,

Enter an email address: boba jobs.com
Invalid email. Your email must contain an @ symbol.

Alternately,

Enter an email address: bob@jobs.com
Your email address is valid.