Assignment 17
Due Tuesday, June 12 at 9:20am 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 for credit on this assignment
    • Please do not remove the prototypes from above 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.

/**
 * Your Name
 * 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 bad: " << 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)
  • Write a program that takes in an email address as a string and determines whether the email address is valid.
  • 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, .net or .edu
    • The address must have an @ character contained somewhere in the string
    • The address must not contain a space.
  • Copy and paste the starter code into a new file called email.cpp
#include <iostream>
using namespace std;


bool containsSpace(string email);
//returns true if the email contains a space, false otherwise

/****add prototype and comment for containsAt here****/

bool correctExtension(string email);
//returns true if the email has a .com, .net or .edu extension, false otherwise

int main() {

    string email;
    bool validEmail = true;

    cout << "Please enter an email address: ";
    //read in the email address to the email variable
   
    bool atSymbol = containsAt(email); //call containsAt and save result in variable 
    bool extensionCorret = //fill in function call here
    bool space = //fill in function call here

    if (                ) { //fill in test condition for if
        cout << "Your email address cannot contain a space." << endl;
        validEmail = false;
    }

    if (!atSymbol) {
        cout << "Your email address must contain an @ symbol." << endl;
        validEmail = false;
    }

    if(                ) { //fill in test condition for if
        cout << "Your email address must end with .com, .net or .edu" << endl;
        validEmail = false;
    }

    if (validEmail) {
        cout << "Your email address is valid." << endl;
    } else {
        cout << "Your email address is not valid." << endl;
    }

    return 0;
}

       //Write additional two functions here

bool containsAt(string email) {
    bool at = false;
    for (int i = 0; i < email.length(); i++) { //search one character at a time
          if (email[i] == '@') { //if you find an @ symbol
               return true;
          }
    } //end of loop
            
    return false; //did not return true above so never found @ symbol
}
  • Fill in the missing parts of the program indicated by the comments.
  • Important! Do not add, remove or alter anything about the program.
    • Only fill in the missing parts as indicated by the comments
  • When your program is giving the correct output, upload your source code to Canvas.

Your program must work identically to the sample output below:

Please enter an email address: bob@jobs.org
Your email address must end with .com, .net or .edu
Your email address is not valid.

Alternately,

Please enter an email address: bobajobs.org
Your email address must contain an @ symbol.
Your email address must end with .com, .net or .edu
Your email address is not valid.

Alternately,

Please enter an email address: bob a jobs.co
Your email address cannot contain a space.
Your email address must contain an @ symbol.
Your email address must end with .com, .net or .edu
Your email address is not valid.

Alternately,

Please enter an email address: boba jobs.com
Your email address cannot contain a space.
Your email address must contain an @ symbol.
Your email address is not valid.

Alternately,

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


Assignment 17.3: 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 for credit on this assignment
    • Please do not remove the prototypes from above 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.


/**
 * Your Name
 * 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;
}