SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:5 | ISBN:9780321531346 | Edition: 7

Question

Write a program that tells what coins to give out for any amount of
change from 1 cent to 99 cents. For example, if the amount is 86 cents, the
output would be something like the following:
86 cents can be given as
3 quarter(s) 1 dime(s) and 1 penny(pennies)
Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1
cent (pennies). Do not use nickel and half-dollar coins. Your program
will use the following function (among others):
void compute_coin(int coin_value, int& number, int& amount_left);
//Precondition: 0 < coin_value < 100; 0 <= amount_left < 100.
//Postcondition: number has been set equal to the maximum number of coins
//denomination coin_value cents that can be obtained from amount_left
//amount_left has been decreased by the value of the coins, that is,
//decreased by number*coin_value.
For example, suppose the value of the variable amount_left is 86 . Then,
after the following call, the value of number will be 3 and the value of
amount_left will be 11 (because if you take 3 quarters from 86 cents, that
leaves 11 cents):
compute_coins(25, number, amount_left);
Include a loop that lets the user repeat this computation for new input
values until the user says he or she wants to end the program. Hint: Use
integer division and the % operator to implement this function.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include <iostream>
using namespace std;

void computeCoin(int coin_value, int &number, int &amount);
void inputData(int &amount);

int main()
{
	int amount,number;
	char option;
	do
	{

		inputData(amount);
		cout << amount << " cents can be given as" 
			<< endl;
		computeCoin(25, number, amount); 
		cout << number << " quarter(s) ";
		computeCoin(10, number, amount); 
		cout << number << " dime(s) ";
		cout << amount << " penny(pennies)" << endl;
		cout << "Do you want to continue again?(Y/N): ";
		cin >> option;
		cout << endl;
	}while(option == 'Y' || option == 'y');
	return 0;
} 
void computeCoin(int coin_value, int &number, int &amount)
{
	number = amount / coin_value;
	amount = amount - number * coin_value;
} 
void inputData(int &amount)
{
	cout << "Enter the amount of change in cents: ";
	cin >> amount;
	while(amount < 1 || amount > 99)
	{
		cout << "Amount between 1 and 99 (inclusive): ";
		cin >> amount;
	} 
	cout << endl;
}

Output:

Enter the amount of change in cents: 100
Amount between 1 and 99 (inclusive): 86

86 cents can be given as
3 quarter(s) 1 dime(s) 1 penny(pennies)
Do you want to continue again?(Y/N): n

 

0 0

Discussions

Post the discussion to improve the above solution.