Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q2: Introduction to Pass by Value in C++ The code is as follows: // This program

ID: 3724897 • Letter: Q

Question

Q2: Introduction to Pass by Value in C++

The code is as follows:

// This program will allow the user to input from the keyboard

// whether the last word to the following proverb should be party or country:

// "Now is the time for all good men to come to the aid of their "

// Inputting a 1 will use the word party. Any other number will use the word country.

// PLACE YOUR NAME HERE

#include <iostream>

#include <string>

using namespace std;

// Fill in the prototype of the function writeProverb.

int main ()

{

int wordCode;

cout << "Given the phrase:" << endl;

cout << "Now is the time for all good men to come to the aid of their "

     << endl;

cout << "Input a 1 if you want the sentence to be finished with party"

     << endl;

cout << "Input any other number for the word country" << endl;

cout << "Please input your choice now" << endl;

cin >> wordCode;

cout << endl;

writeProverb(wordCode);

return 0;

}

// ******************************************************************************

// writeProverb

//

// task: This function prints a proverb. The function takes a number

// from the call. If that number is a 1 it prints "Now is the time

// for all good men to come to the aid of their party."

// Otherwise, it prints "Now is the time for all good men

// to come to the aid of their country."

// data in: code for ending word of proverb (integer)

// data out: no actual parameter altered

//

// *****************************************************************************

void writeProverb (int number)

{

// Fill in the body of the function to accomplish what is described above

}

Exercise 1: Some people know this proverb as “Now is the time for all good men to come to the aid of their country” while others heard it as “Now is the time for all good men to come to the aid of their party.” This program will allow the user to choose which way they want it printed. Fill in the blanks of the program to accomplish what is described in the program comments. What happens if you inadvertently enter a float such as -3.97?

Exercise 2: Change the program so that an input of 1 from the user will print “party” at the end, a 2 will print “country” and any other number will be invalid so that the user will need to enter a new choice.

Sample Run:

Given the phrase:

Now is the time for all good men to come to the aid of their Input a 1 if you want the sentence to be finished with party Input a 2 if you want the sentence to be finished with country Please input your choice now

4

I'm sorry but that is an incorrect choice; Please input a 1 or 2

2

Now is the time for all good men to come to the aid of their country

Exercise 3: Change the previous program so the user may input the word to end the phrase. The string holding the user’s input word will be passed to the proverb function instead of passing a number to it. Notice that this change requires you to change the proverb function heading and the prototype as well as the call to the function.

Sample Run:

Given the phrase:

Now is the time for all good men to come to the aid of their _______

Please input the word you would like to have finish the proverb

family

Now is the time for all good men to come to the aid of their family

Explanation / Answer

Question 2:

// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their "
// Inputting a 1 will use the word party. Any other number will use the word country.
// PLACE YOUR NAME HERE

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

// Fill in the prototype of the function writeProverb.
void writeProverb (int);

int main ()
{
int wordCode;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their "
     << endl;
cout << "Input a 1 if you want the sentence to be finished with party"
     << endl;
cout << "Input any other number for the word country" << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);

return 0;
}

// ******************************************************************************
// writeProverb
//
// task: This function prints a proverb. The function takes a number
// from the call. If that number is a 1 it prints "Now is the time
// for all good men to come to the aid of their party."
// Otherwise, it prints "Now is the time for all good men
// to come to the aid of their country."
// data in: code for ending word of proverb (integer)
// data out: no actual parameter altered
//
// *****************************************************************************

void writeProverb (int number)
{
// Fill in the body of the function to accomplish what is described above
if(number == 1)
cout<< "Now is the time for all good men to come to the aid of their party.";
else
cout<<"Now is the time for all good men to come to the aid of their country.";

}

Output:

Given the phrase:
Now is the time for all good men to come to the aid of their
Input a 1 if you want the sentence to be finished with party
Input any other number for the word country
Please input your choice now 1

Now is the time for all good men to come to the aid of their party.

Exercise 1: else part will execute

Given the phrase:
Now is the time for all good men to come to the aid of their
Input a 1 if you want the sentence to be finished with party
Input any other number for the word country
Please input your choice now -3.97

Now is the time for all good men to come to the aid of their country.

Exercise 2:

// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their "
// Inputting a 1 will use the word party. Any other number will use the word country.
// PLACE YOUR NAME HERE

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

// Fill in the prototype of the function writeProverb.
void writeProverb (int);

int main ()
{
int wordCode;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their "
     << endl;
cout << "Input a 1 if you want the sentence to be finished with party"
     << endl;
cout << "Input any other number for the word country" << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);

return 0;
}

// ******************************************************************************
// writeProverb
//
// task: This function prints a proverb. The function takes a number
// from the call. If that number is a 1 it prints "Now is the time
// for all good men to come to the aid of their party."
// Otherwise, it prints "Now is the time for all good men
// to come to the aid of their country."
// data in: code for ending word of proverb (integer)
// data out: no actual parameter altered
//
// *****************************************************************************

void writeProverb (int number)
{
// Fill in the body of the function to accomplish what is described above
if(number == 1)
cout<< "Now is the time for all good men to come to the aid of their party.";
else if(number == 2)
cout<<"Now is the time for all good men to come to the aid of their country.";
else
cout<<" I'm sorry but that is an incorrect choice; Please input a 1 or 2";

}

Output:

Given the phrase:
Now is the time for all good men to come to the aid of their
Input a 1 if you want the sentence to be finished with party
Input any other number for the word country
Please input your choice now 4


I'm sorry but that is an incorrect choice; Please input a 1 or 2

Exercise 3:

// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their "
// Inputting a 1 will use the word party. Any other number will use the word country.
// PLACE YOUR NAME HERE

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

// Fill in the prototype of the function writeProverb.
void writeProverb (string);

int main ()
{
string proverb;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their "
     << endl;

cout << "Please input your choice now" << endl;
cin >> proverb;
cout << endl;
writeProverb(proverb);

return 0;
}

// ******************************************************************************
// writeProverb
//
// task: This function prints a proverb. The function takes a number
// from the call. If that number is a 1 it prints "Now is the time
// for all good men to come to the aid of their party."
// Otherwise, it prints "Now is the time for all good men
// to come to the aid of their country."
// data in: code for ending word of proverb (integer)
// data out: no actual parameter altered
//
// *****************************************************************************

void writeProverb (string proverb)
{
// Fill in the body of the function to accomplish what is described above
cout<<"Now is the time for all good men to come to the aid of their "<<proverb<<".";

}

Output:

Given the phrase:
Now is the time for all good men to come to the aid of their
Please input your choice now family

Now is the time for all good men to come to the aid of their family.