I need a little help with this problem related to a previous assignment that I d
ID: 3587487 • Letter: I
Question
I need a little help with this problem related to a previous assignment that I did, I have previous the program below the problem, any help would be greatly appreciated.
Rewrite the Project 1 (I have project 1 below) with a new approach. Must decompose the function main into smaller functions and integrate them together. Below are the requirements:
· The program must have an option for user to repeat the program.
· Apply all of the value condition from Project 1.
· Use the following function requirements to write function definitions:
1. Function getGameDuration()
· Function prompts user to input the duration of the game.
· Function validates user's input for the duration of the game.
· Function returns the value of the duration of the game.
2. Function getPricePerMinute()
· Function prompts user to input the price per minute for the payment of the game.
· Function validates user's input for the price.
· Function returns the value of the price per minute.
3. Function getGameInformation()
· Function gets inputs for the duration of the game and the price per minute for the payment of the game.
· Function outputs the value of the duration of the game and the price per minute.
4. Function calculateTotalPayment()
· Function gets the duration of the game and the price per minute for the payment of the game passed in from the caller.
· Function calculates the total payment.
· Function returns the value of the total payment.
5. Function calculateRefPayments()
· Function gets the total payment passed in from the caller.
· Function calculates the payments for center referee and assistant referee.
· Function outputs payments calculated.
6. Function printResults()
· Function gets the duration of the game and the price per minute for the payment of the game passed in from the caller.
· Function gets the total payment passed in from the caller.
· Function gets the payments for center referee and assistant referee passed in from the caller.
· Function prints out the game information, the total payment and all referee payments.
7. Function refPaymentCalculation()
· This is the overall function for the calculation.
· This function integrates all of the above function.
· Can add any other functions if needed.
· For the design document, draw flowcharts for each of the functions including function main().
Project 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
int duration;
double pricePerMin;
cout << "Enter the game information as followed: ";
cout << "Duration of the game in minutes (between 40 and 90 and in intervals of 10): ";
cin >> duration;
while(duration%10 != 0 || duration < 40 || duration > 90) {
cout << "Invalid duration. Try again: ";
cin >> duration;
}
cout << "Price per minute(between $1.50 and $5.00): $";
cin >> pricePerMin;
while(pricePerMin < 1.5 || pricePerMin > 5) {
cout << "Invalid price. Try again: ";
cin >> pricePerMin;
}
double total_payment = duration * pricePerMin;
double centerRef, assistantRef;
// get integer value
total_payment = ((int)total_payment);
int payment_to_divide = total_payment;
// reach to nearest 10 multiple
int remaining_payment = (payment_to_divide % 10);
payment_to_divide = (payment_to_divide - remaining_payment);
centerRef = payment_to_divide * 0.4;
assistantRef = payment_to_divide * 0.3;
int shares_of_remaining = remaining_payment/3;
remaining_payment = remaining_payment%3;
centerRef += shares_of_remaining;
assistantRef += shares_of_remaining;
// payment_to_divide will be 0, 1 or 2$ left only.
if(remaining_payment == 1) {
centerRef += 1;
} else if(remaining_payment == 2){
assistantRef += 1;
}
cout << " Referee Payment Breakdown ";
cout << "Game duration: " << duration << endl;
cout << "Price per minute: $" << fixed << setprecision(2) << pricePerMin << endl;
cout << "Total Payment: $" << fixed << setprecision(2) << total_payment << endl;
cout << "Center Ref's payment: $" << fixed << setprecision(2) << centerRef << endl;
cout << "Assistant Refs' Pay: $" << fixed << setprecision(2) << assistantRef << endl;
cout << "Do another calculation(y/n): ";
cin >> choice;
cout << endl;
}
return 0;
}
Explanation / Answer
Below is your program. There is alot of redundancy in requirement. I have tried to make it as less redundant as possible.
#include <iostream>
#include <iomanip>
using namespace std;
int getGameDuration() {
int duration;
cout << "Enter the game information as followed: ";
cout << "Duration of the game in minutes (between 40 and 90 and in intervals of 10): ";
cin >> duration;
while(duration%10 != 0 || duration < 40 || duration > 90) {
cout << "Invalid duration. Try again: ";
cin >> duration;
}
return duration;
}
double getPricePerMinute() {
double pricePerMin;
cout << "Price per minute(between $1.50 and $5.00): $";
cin >> pricePerMin;
while(pricePerMin < 1.5 || pricePerMin > 5) {
cout << "Invalid price. Try again: ";
cin >> pricePerMin;
}
return pricePerMin;
}
void getGameInformation(int duration,double pricePerMin) {
cout << "Game duration: " << duration << endl;
cout << "Price per minute: $" << fixed << setprecision(2) << pricePerMin << endl;
}
double calculateTotalPayment(int duration,double pricePerMin) {
double total_payment = duration * pricePerMin;
return total_payment;
}
void calculateRefPayments(int duration,double pricePerMin) {
double total_payment = calculateTotalPayment(duration,pricePerMin);
double centerRef, assistantRef;
// get integer value
total_payment = ((int)total_payment);
int payment_to_divide = total_payment;
// reach to nearest 10 multiple
int remaining_payment = (payment_to_divide % 10);
payment_to_divide = (payment_to_divide - remaining_payment);
centerRef = payment_to_divide * 0.4;
assistantRef = payment_to_divide * 0.3;
int shares_of_remaining = remaining_payment/3;
remaining_payment = remaining_payment%3;
centerRef += shares_of_remaining;
assistantRef += shares_of_remaining;
// payment_to_divide will be 0, 1 or 2$ left only.
if(remaining_payment == 1) {
centerRef += 1;
} else if(remaining_payment == 2){
assistantRef += 1;
}
cout << "Total Payment: $" << fixed << setprecision(2) << total_payment << endl;
cout << "Center Ref's payment: $" << fixed << setprecision(2) << centerRef << endl;
cout << "Assistant Refs' Pay: $" << fixed << setprecision(2) << assistantRef << endl;
}
void printResults() {
int duration = getGameDuration();
double pricePerMin = getPricePerMinute();
getGameInformation(duration,pricePerMin);
calculateRefPayments(duration,pricePerMin);
}
void refPaymentCalculation() {
printResults();
}
int main()
{
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
cout << " Referee Payment Breakdown ";
refPaymentCalculation();
cout << "Do another calculation(y/n): ";
cin >> choice;
cout << endl;
}
return 0;
}
Output
Referee Payment Breakdown
Enter the game information as followed:
Duration of the game in minutes (between 40 and 90 and in intervals of 10): 95
Invalid duration. Try again: 105
Invalid duration. Try again: 90
Price per minute(between $1.50 and $5.00): $1
Invalid price. Try again: 6
Invalid price. Try again: 5.2
Invalid price. Try again: 4.5
Game duration: 90
Price per minute: $4.50
Total Payment: $405.00
Center Ref's payment: $161.00
Assistant Refs' Pay: $122.00
Do another calculation(y/n): y
Referee Payment Breakdown
Enter the game information as followed:
Duration of the game in minutes (between 40 and 90 and in intervals of 10): 50
Price per minute(between $1.50 and $5.00): $5
Game duration: 50
Price per minute: $5.00
Total Payment: $250.00
Center Ref's payment: $100.00
Assistant Refs' Pay: $75.00
Do another calculation(y/n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.