Assignment 21
Due Thursday, June 21 at 9:20am on Canvas

Quiz 7 Answer Key

Assignment 21.1: More Barnyard Animals (10 pts)

  • In the previous version of this assignment (20.3 from last night's homework) we wrote a program to read in numbers from a file and save them in 5 separate integer variables.
  • Now, we will alter the above assignment so that it uses arrays.
  • From the previous version of your program, remove the 5 integer variables that you declared at the top of main, and instead, declare an integer array of length 5.
const int SIZE1 = 5;
int numAnimals[SIZE1];
  • Now, alter your 5 fin statements to store values in the array instead -- each number at one index of the array
  • Next, at the top of main, declare a string array named animals, with a corresponding constant variable named SIZE2, assigned the value of 5.
  • Store the following values in the string array -- one at each index:

  • We are now going to write the contents of our two arrays to the output file.
  • Below the statement fout << "Animals at our farm" << endl; replace the subsequent 5 fout statements with a for loop.
  • Use this for loop to print out the contents of two arrays to the output file.
  • Hint, use one of the array's SIZE variables in the test condition of your for loop.
  • See the notes from Tuesday's class for an example of how to print an array using a for loop, or see your in-class activity 21.3.
  • Finally, compile and run your code and verify that the following the same output appears in the file as in the previous version of the program:

    Animals at our farm:
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your animals.cpp program to Canvas when you are finished

Assignment 21.2: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new C++ file called nums.cpp
  • Prompt a user to enter in the size of the array (the number of numbers).
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0

  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload nums.cpp to Canvas when you are finished.

Assignment 21.3: Customer Data (10 pts)
  • Assume you work for a company that tracks customer information, including name, gender and phone numbers
  • Your company has a file called customers.txt which contains the following information:

James Brown
Leanna Perez
Xing Li
Stacey Cahill
Mohammed Abbas
Kumari Chakrabarti
Shakil Smith
Jung Ahrin
Pedro Martinez
Ally Gu
  • Your task is to read in the data from the above file using a loop, format the data, and write it into a new file called contacts.txt
  • When you are finished, contacts.txt should contain the following information:

Mr. James Brown
Ms. Leanna Perez
Mr. Xing Li
Mx. Stacey Cahill
Mr. Mohammed Abbas
Ms. Kumari Chakrabarti
Mr. Shakil Smith
Ms. Jung Ahrin
Mr. Pedro Martinez
Mx. Ally Gu
  • In other words, whenever the customers.txt file contains an M below the customer name, the contacts.txt file should have the word Mr. before the name.
  • And, whenever the customers.txt file contains an F below the customer name, the contacts.txt file should have the word Ms. before the name.
  • Finally, whenever the file contains an O below the customer name, the contacts.txt file should have the word Mx. before the name.
  • In addition, the unformatted phone numbers in customers.txt must be formatted as XXX-XXX-XXXX inside of contacts.txt.
  • To format the phone numbers, you must write a function with the following signature:

void formatPhone(string& phone);
//Takes an unformatted 10-digit phone number as a string
//Alters the string to be formatted like so XXX-XXX-XXXX
  • Additionally, you must use a while loop to read in the customer information.
  • Copy and paste the starter code into a new C++ file called contacts.cpp

#include <iostream>
//add two more libraries here for file I/O
using namespace std;

void formatPhone(string& phone);
//Takes an unformatted 10-digit phone number as a string
//Formats the string in the format XXX-XXX-XXXX

int main() {
    string name, gender, phone;
    //declare your input file stream variable here
    //open the customer.txt file
    //check for failure
    //decare your output file stream variable here
    //open contacts.txt file
    //check for failure
    while () { //Will you need fin >> or getline() inside the while loop test to read in the name?
        fin >> gender;
        //one more line here to read in the phone number
        //if, else if and else statements here
        //function call to formatPhone here to alter the phone number

        //call fin.ignore() or fin >> ws to avoid problem when fin before a getline

    //close your input and output file streams
    return 0;

  • When your code is providing the correct output in the contacts.txt file, upload your contacts.cpp file to Canvas