Question

In: Computer Science

C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an...

C++

19.32 LAB: Exact change - functions

Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.

Ex: If the input is:

0 

or less, the output is:

no change

Ex: If the input is:

45

the output is:

1 quarter
2 dimes 

Your program must define and call the following function. Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
void ExactChange(int userTotal, vector<int>& coinVals)

Note: This is a lab from a previous chapter that now requires the use of a function.

Solutions

Expert Solution

#include <iostream>
#include <vector>

using namespace std;

void ExactChange(int userTotal, vector<int>& coinVals) {
    coinVals.clear();
    coinVals.push_back(userTotal / 100);
    userTotal %= 100;
    coinVals.push_back(userTotal / 25);
    userTotal %= 25;
    coinVals.push_back(userTotal / 10);
    userTotal %= 10;
    coinVals.push_back(userTotal / 5);
    userTotal %= 5;
    coinVals.push_back(userTotal);
}

int main() {
    vector<int> coins;
    int value;
    cin >> value;
    if (value <= 0) {
        cout << "no change" << endl;
    } else {
        ExactChange(value, coins);
        if (coins[0] != 0)
            cout << coins[0] << " " << (coins[0] == 1 ? "dollar" : "dollars") << endl;
        if (coins[1] != 0)
            cout << coins[1] << " " << (coins[1] == 1 ? "quarter" : "quarters") << endl;
        if (coins[2] != 0)
            cout << coins[2] << " " << (coins[2] == 1 ? "dime" : "dimes") << endl;
        if (coins[3] != 0)
            cout << coins[3] << " " << (coins[3] == 1 ? "nickel" : "nickels") << endl;
        if (coins[4] != 0)
            cout << coins[4] << " " << (coins[4] == 1 ? "penny" : "pennies") << endl;
    }
    return 0;
}


Related Solutions

6.32 LAB: Exact change - functions Write a program with total change amount as an integer...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes...
For Java 3.25 LAB: Exact change Write a program with total change amount in pennies as...
For Java 3.25 LAB: Exact change Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount as an integer...
USE PYTHON 4.15 LAB: Exact change Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter...
Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Write a program in pyton with total change amount as an integer input, and output the...
Write a program in pyton with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes Can you...
In the space provided below write a C ++ program that computes the total amount of...
In the space provided below write a C ++ program that computes the total amount of money you are depositing in your bank account. Your program does this by asking you to first enter the number and amount of each check you are depositing, and then it asks you to enter the type of cash bills being deposited and how many of each type, also the types of coins being deposited, and the number of each coin type.
In the space provided below write a C program that computes the total amount of money...
In the space provided below write a C program that computes the total amount of money you have stored in your piggy bank. Your program does this by asking you for number of pennies, nickels, dimes, and quarters in the piggy bank and then displays how much money in total is in the piggy bank.
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT