Answer Key


Assignment 15
Due Monday, March 12 at 9:20am on Canvas


Don't forget to do Activity 15.4: My Salary!


Assignment 15.1: Vowels (10 pts)
  • Write a program that takes in a sentence from the user and then counts the number of vowels in the sentence.
  • You will need to use string.length(), string indexing using the square bracket syntax, and a for loop to correctly solve this problem.
  • Open up a new C++ program named vowels.cpp
  • First, welcome the user to the program and explain its purpose:
Welcome! Give me a sentence and I will count its vowels.
  • Then prompt the user to enter a sentence.
Please enter a sentence: _
  • Store the user input as a string variable.
  • Now, using a for loop and the string.length() function, count how many vowels (a, e, i, o, u) there are in the sentence.
  • Note that the program should also count capital vowels (A, E, I, O, U).
  • Finally, output the number of vowels like so:
There are 5 vowels in your sentence.
  • Make sure that your sample output matches mine.
  • Then, submit your program to Canvas.

The output of your program should look identical to the sample output below.


It should also count capital letter vowels like this:

Assignment 15.2: Vowels Again

  • Write a function with the following signature:
    int countVowels(string str) 
  • The function returns a count of the letters in the string parameter that are vowels (A, E, I, O, U, a, e, i, o, u)
  • Hint: You've done this same thing on your previous assignment, just not with a function.
  • For this assignment we are going to "outsource" the work of counting vowels to a function named countVowels.

Part 1:

  • Copy and paste the starter code into a file named vowels.cpp:

/**
* Name
* Section
*/

int countVowels(string s)
{
    //add the code for your function here

}

int main()
{
    string sentence;
    int numVowels;
    //Prompt user to enter a sentence
   
    //call the countVowels function, passing it the sentence as a parameter
    //& storing result as numVowels
   
    //print out the number of vowels in the sentence

    cout << "There are " << numVowels << " in your sentence." << endl;

}

  • Your program should first prompt the user to enter a sentence.
  • Hint: Use getline().
  • Then, you should pass the sentence variable into your function and store the return value in your numVowels variable.
  • Test out your code with various sentences.
  • The program should now work like this:

Enter a sentence: I love cookies a lot!

There are 9 vowels in your sentence.

Part 2:

  • Add a while loop to your code to repeatedly prompt the user to enter a sentence or q to quit.
  • The program should accept q, Q, quit, or Quit as signals to exit the loop.
  • When you are finished, your program should work like this:

Enter a sentence or q to quit: I love cookies a lot!

There are 9 vowels in your sentence.

Enter a sentence or q to quit: I wouldn't mind a piece of cake, either.

There are 14 vowels in your sentence.

Enter a sentence or q to quit: Q

Goodbye!

Part 3:

  • Next, add another function to your program to count letters in the sentence.
  • Your function signature should look like this:

int countLetters(string s)

  • Thus, add a new variable to the top of main that will store the result of calling the above function.
int numLetters;
  • To write the above function, you may need to consult the ASCII table. Hint: what is the range of ASCII values in which you would find letters?
  • After you write the above function, call it inside of main and then add to your print statement to display the result for the number of letters in the sentence.
  • Your program should now work like this:

Enter a sentence or q to quit: I love cookies a lot!

There are 9 vowels in your sentence, and 16 letters.

Enter a sentence or q to quit: I wouldn't mind a piece of cake, either.

There are 14 vowels in your sentence, and 30 letters.

Enter a sentence or q to quit: Q

Goodbye!

  • When you are certain it is working properly submit to Canvas.


Assignment 15.3: Another 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 two 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 have an @ character contained somewhere in the string
    • The address must not contain a space.
  • For this version of the assignment, we are going to take the for loops you wrote for Assignment 14.2 and place them inside of their own functions.
  • Copy and paste the starter code into a new file called email.cpp
#include <iostream>
using namespace std;

//returns true if the email contains a space, false otherwise
bool containsSpace(string email) {
     //fill in missing lines of code here  
}

bool containsAt(string email) {
    bool at = false;
    for (int i = 0; i < email.length(); i++) {
          if (email[i] == '@') {
               at = true; //set at to true only if you find an @ in the string
          }
    }

    return at;
}

//returns true if the email has a .com, .org or .edu extension, false otherwise
bool correctExtension(string email) {
    //fill in the missing line(s) of code here

}

int main() {

    string email;
    bool validEmail = true;

    cout << "Please enter an email address: ";
    //read in the email address to the email variable
  

    if (                ) { //fill in the missing function call here
        cout << "Your email address cannot contain a space." << endl;
        validEmail = false;
    }

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

    if(                ) { //fill in the missing function call here
        cout << "Your email address must end with .com, .edu or .org" << endl;
        validEmail = false;
    }

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

    return 0;
}


  • Fill in the missing parts of the program indicated by the comments.
  • Your program must work identically to the sample output below.
  • When your program is giving the correct output, upload your source code to Canvas.


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

Alternately,

Please enter an email address: bobajobs.net
Your email address must contain an @ symbol.
Your email address must end with .com, .edu or .org
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, .edu or .org
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 15.4: 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 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;

}