116k views
3 votes
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 (possibly among others):

Void computeCoin( int coinValue, int& number, int& amountLeft );

// Precondition: 0
// Postcondition: number has been set equal to the maximum

// number of coings of denomination coinValue cents that

// can be obtained from amountLeft cents. amountLeft has

// been decreased by the value of the coins, that is,

// decreased by number*coinValue.

For example, suppose the value of the variable amountLeftis 86. Then, after the

following call, the value of numberwill be 3 and the value of amountLeftwill be 11

(because if you take three quarters from 86 cents, that leaves 11 cents):

computeCoins(25, number, amountLeft);

Testing your program:

Be sure to test your program with various values to ensure it provides the correct

output before submitting to get full credit. For extra credit, have your program only

accept proper input values (1 to 99). If the user attempts to enter an invalid

number, your program should output an error message and repeat the input

process until it receives an appropriate value. (Hint: use a while or do-while loop.)





could you edit and improvise on this code to work :



#include "stdafx.h"
#include
#include
using namespace std;

void computeCoin(int coinValue, int& number, int& amountLeft)

{
number = amountLeft/ coinValue;
amountLeft = amountLeft - (coinValue*number);
}


int amount = 0;
int number = 0;
int quarters = 0;
int dimes = 0;
int pennies = 0;
int change = 0;


int main()
{
cout << " Enter the amount desired: " ;
cin>>amount;

do

while( amount > 1 || amount < 99 );
{
amount = change;
amount++;

if (amount < 100 && amount >= 25)
{
change = amount/25;
quarters++;
}
else if (amount < 25 && amount >= 10)
{
change = amount/10;
dimes++;
}
else if (amount < 10 && amount >=0)
{
change = amount/1;
pennies++;
}


computeCoin (25, quarters, amount);
cout << amount << " " << number < computeCoin (10, dimes, amount);
cout << amount << " " << number <
computeCoin (01, pennies, amount);
cout << amount << " " << number < cout<< " The change desired is: " << change;

}

return (0);

}

User Anudeep GI
by
6.9k points

2 Answers

3 votes

Final answer:

The improved C++ program efficiently calculates and outputs the number of quarters, dimes, and pennies as change for an input amount between 1 and 99 cents. It also validates user input within the specified range.

Step-by-step explanation:

The student is seeking assistance with a C++ program that calculates the change in coins for any given amount from 1 to 99 cents. The program uses a provided function, computeCoin, to determine the number and type of coins needed. The starting code has several logical issues and syntax errors, which need to be corrected by ensuring proper looping for user input validation and the correct usage of the computeCoin function. Here's an improved version of the code:

#include
using namespace std;

void computeCoin(int coinValue, int& number, int& amountLeft) {
number = amountLeft / coinValue;
amountLeft -= number * coinValue;
}

int main() {
int amount, quarters, dimes, pennies;

do {
cout << "Enter the amount desired (1 to 99 cents): ";
cin >> amount;
if(amount < 1 || amount > 99) {
cout << "Error, please enter a value between 1 and 99 cents.\\";
}
} while(amount < 1 || amount > 99);

computeCoin(25, quarters, amount);
computeCoin(10, dimes, amount);
computeCoin(1, pennies, amount);

cout << "Your change is: " << quarters << " quarter(s), "
<< dimes << " dime(s), and " << pennies << " penny (pennies)";
return 0;
}

This revised program first validates the input, then uses the computeCoin function to calculate the number of quarters, dimes, and pennies that make up the entered amount. The output clearly states how many of each coin type is provided as change.

User DaBlick
by
6.6k points
6 votes

Answer:

Following is attached the image of code as required.

I hope it will help you a lt!

Step-by-step explanation:

Write a program that tells what coins to give out for any amount of change from 1 cent-example-1
User Fetti
by
7.5k points