Question

In: Computer Science

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 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& coinVals) In C++ please

Solutions

Expert Solution

Since there are no constraints given, it is assumed that there are infinite number of all coins available.

C++ code:

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

void ExactChange(int userTotal, vector<int>& coinVals){
    vector<string> coinsName(4, "");
    coinsName[0] = "Quaters"; coinsName[1] = "Dimes"; coinsName[2] = "Nickels"; coinsName[3] = "Pennies";
  
    for(size_t i = 0; i < coinVals.size(); ++i)
    {
        int numberOfCoins = userTotal / coinVals[i]; // calculate the number of quarters
        userTotal = userTotal % coinVals[i]; // calculate remaining change needed
        cout << coinsName[i] << ": " << numberOfCoins << endl; // display # of quarters
    }
}

int main ( )
{
    float userNUmber;
    int change, quarters, dimes, nickels, pennies; // declare variables

    cout <<"Enter the amount of money: ";
    cin >> userNUmber; // input the amount of change
    change = userNUmber;

    vector<int> coins(4, 0);
    coins[0] = 25; coins[1] = 10; coins[2] = 5; coins[3] = 1;
    ExactChange(change, coins);

    return (0);
}

Please refer to the screenshot of the code to understand the indentation of the code


Output:

Hope this helped. Please do upvote and if there are any queries please ask in comments section.


Related Solutions

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...
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...
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...
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
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...
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer...
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second...
In this lab we will write 3 functions: GenData: This function will take an integer parameter...
In this lab we will write 3 functions: GenData: This function will take an integer parameter and return a vector with that many random integers generated in the range of 0 and 100 (rand()%101). Seed the random number generator with 22. Mean(): This function will take a vector and return the mean. Variance(): This function will take a vector and return the population variance, as: [Sum for values[( x_i - Mean )^2]] / (number of items) In Main: Use GenData...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT