> amount; 4 cout << endl; 5 int numBills1 = (int) (amount / 20.0); 6 if (numBills1 20.0< amount) { 7 numBills1++; 8} 9 cout << "How much will Jane be spending? "; 10 cin >> amount; 11 cout << endl; 12 int numBills2 = (int) (amount / 20.0); 13 if (numBills2 * 20.0 < amount) { 14 numBills2++; 15 } 16 cout << "John needs " << numBills1 << " bills" << endl; 17 cout << "Jane needs " << numBills2 << "bills" << endl; Function: Write a C++ function as described, not a complete program. ✔Submit

icon
Related questions
Question
<percentageGrade
You are working on problem set: PS4 (Pause)
Pause) i
→ spending ♡
Language/Type:
Related Links:
C++if/else input redundancy console input
string
Main Page → Exercises → PS4→ C++ → Solve an Exercise
The following code is poorly structured. Rewrite it so that it has a better structure and avoids redundancy. To help
eliminate redundancy, convert the code into a function named spending that accepts as its parameter a string for
a single person's name, and prints the appropriate information about that person's bills. Your function could be
called twice (once for John and once for Jane) to replicate the original code's behavior.
1 cout << "How much will John be spending? ";
2 double amount;
3 cin >> amount;
4 cout << endl;
5 int numBills1 = (int) (amount / 20.0);
6 if (numBills1 20.0< amount) {
7
numBills1++;
8}
9 cout << "How much will Jane be spending? ";
10 cin >> amount;
11 cout << endl;
12 int numBills2 = (int) (amount / 20.0);
13 if (numBills2 * 20.0 < amount) {
14 numBills2++;
15}
16 cout << "John needs " << numBills1 << "bills" << endl;
17 cout << "Jane needs " << numBills2 << " bills" << endl;
Function: Write a C++ function as described, not a complete program.
✔ Submit
Transcribed Image Text:<percentageGrade You are working on problem set: PS4 (Pause) Pause) i → spending ♡ Language/Type: Related Links: C++if/else input redundancy console input string Main Page → Exercises → PS4→ C++ → Solve an Exercise The following code is poorly structured. Rewrite it so that it has a better structure and avoids redundancy. To help eliminate redundancy, convert the code into a function named spending that accepts as its parameter a string for a single person's name, and prints the appropriate information about that person's bills. Your function could be called twice (once for John and once for Jane) to replicate the original code's behavior. 1 cout << "How much will John be spending? "; 2 double amount; 3 cin >> amount; 4 cout << endl; 5 int numBills1 = (int) (amount / 20.0); 6 if (numBills1 20.0< amount) { 7 numBills1++; 8} 9 cout << "How much will Jane be spending? "; 10 cin >> amount; 11 cout << endl; 12 int numBills2 = (int) (amount / 20.0); 13 if (numBills2 * 20.0 < amount) { 14 numBills2++; 15} 16 cout << "John needs " << numBills1 << "bills" << endl; 17 cout << "Jane needs " << numBills2 << " bills" << endl; Function: Write a C++ function as described, not a complete program. ✔ Submit
<percentageGrade
You are working on problem set: PS4 (Pause)
Pause) i
→ spending ♡
Language/Type:
Related Links:
C++if/else input redundancy console input
string
Main Page → Exercises → PS4→ C++ → Solve an Exercise
The following code is poorly structured. Rewrite it so that it has a better structure and avoids redundancy. To help
eliminate redundancy, convert the code into a function named spending that accepts as its parameter a string for
a single person's name, and prints the appropriate information about that person's bills. Your function could be
called twice (once for John and once for Jane) to replicate the original code's behavior.
1 cout << "How much will John be spending? ";
2 double amount;
3 cin >> amount;
4 cout << endl;
5 int numBills1 = (int) (amount / 20.0);
6 if (numBills1 20.0< amount) {
7
numBills1++;
8}
9 cout << "How much will Jane be spending? ";
10 cin >> amount;
11 cout << endl;
12 int numBills2 = (int) (amount / 20.0);
13 if (numBills2 * 20.0 < amount) {
14 numBills2++;
15}
16 cout << "John needs " << numBills1 << "bills" << endl;
17 cout << "Jane needs " << numBills2 << " bills" << endl;
Function: Write a C++ function as described, not a complete program.
✔ Submit
Transcribed Image Text:<percentageGrade You are working on problem set: PS4 (Pause) Pause) i → spending ♡ Language/Type: Related Links: C++if/else input redundancy console input string Main Page → Exercises → PS4→ C++ → Solve an Exercise The following code is poorly structured. Rewrite it so that it has a better structure and avoids redundancy. To help eliminate redundancy, convert the code into a function named spending that accepts as its parameter a string for a single person's name, and prints the appropriate information about that person's bills. Your function could be called twice (once for John and once for Jane) to replicate the original code's behavior. 1 cout << "How much will John be spending? "; 2 double amount; 3 cin >> amount; 4 cout << endl; 5 int numBills1 = (int) (amount / 20.0); 6 if (numBills1 20.0< amount) { 7 numBills1++; 8} 9 cout << "How much will Jane be spending? "; 10 cin >> amount; 11 cout << endl; 12 int numBills2 = (int) (amount / 20.0); 13 if (numBills2 * 20.0 < amount) { 14 numBills2++; 15} 16 cout << "John needs " << numBills1 << "bills" << endl; 17 cout << "Jane needs " << numBills2 << " bills" << endl; Function: Write a C++ function as described, not a complete program. ✔ Submit
Expert Solution
Step 1: Algorithm

1. Define a function calculateBills that takes an amount as input:
1.1. Calculate the integer part of (amount / 20.0) and store it as numBills.
1.2. Check if (numBills * 20.0) is less than amount.
1.3. If the check is true, increment numBills by 1.
1.4. Return numBills.

2. Define a function spending that takes a personName as input:
2.1. Output a prompt to enter the spending amount for the person.
2.2. Read the spending amount from the user and store it in the amount variable.
2.3. Call the calculateBills function with the amount as input and store the result in numBills.
2.4. Output the person's name and the number of bills needed based on the calculated numBills.

3. In the main function:
3.1. Call the spending function for John with the name "John."
3.2. Call the spending function for Jane with the name "Jane."

4. End the program.

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions