Write a program that generates a random amount of a purchase between 0 - $4.99.
ID: 662422 • Letter: W
Question
Write a program that generates a random amount of a purchase between 0 - $4.99.
Generate a random amount paid that is between $5.00 - $10.00. Since these amounts are randomly generated within an array don't worry that the amount paid might not make sense in relation to the amount of purchase.
The dollars, quarters, dimes, nickels and pennies variables and any other needed variables will be passed from Main to the method where any changes or assignments to the variables will be made.
Calculate the total change the customer will receive and how many dollars, quarters, dimes, nickels and pennies (in this order) make up that amount of change.
In Main use a character data type and a loop to allow the user to quit the program or continue generating purchases.
Declare the dollars, quarters, dimes, nickels and pennies variables in Main.
Create four (4) static void methods to be called from Main: getPurchaseAmt, getAmtPaid, calcChange and outputChange. These methods are void so no values are returned to Main.
The random numbers generated for the amount of purchase will be done in the getPurchaseAmt method and the purchase amount variable will be passed from Main to the method where the assignment of the random value will be made.
The random numbers generated for the amount paid will be done in the getAmtPaid method and the amount paid variable will be passed from Main to the method where the assignment of the random value will be made.
The variables required for calculation will be passed from Main to the calcChange method and any changes/assignments to these variables will be calculated and assigned in that method. It is up to you to come up with the algorithm needed to determine the amounts of each denomination of change needed.
All variables required for output will be passed from Main to the outputChange method for display.
Random numbers will be generated in the methods where they are needed and not in Main.
The display of the monetary amounts and the counts of each coin type will be as shown in the sample output.
I am having trouble with this assignment can anyone give me a shell code so i can finish this assignment.
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
float getRandomNumber(float min,float max){
return min+static_cast <float> (rand())/(static_cast <float> (RAND_MAX/(max-min)));
}
static void getPurchaseAmt(float &purchaseAmount){
purchaseAmount=getRandomNumber(0.00,4.99);
}
static void getAmtPaid(float &paidAmount){
paidAmount=getRandomNumber(5.00,10.00);
}
static void calcChange(float &purchaseAmount,float &paidAmount,int &dollars,int &quarters,int &dimes,int &nickels,int &pennies){
int changeAmount=(paidAmount-purchaseAmount)*100;
if(changeAmount>=100){
dollars=changeAmount/100;
changeAmount=changeAmount%100;
}
if(changeAmount>=25){
quarters=changeAmount/25;
changeAmount=changeAmount%25;
}
if(changeAmount>=10){
dimes=changeAmount/10;
changeAmount=changeAmount%10;
}
if(changeAmount>=5){
nickels=changeAmount/5;
changeAmount=changeAmount%5;
}
if(changeAmount>=1){
pennies=changeAmount/1;
changeAmount=changeAmount%1;
}
}
static void outputChange(float &purchaseAmount,float &paidAmount,int &dollars,int &quarters,int &dimes,int &nickels,int &pennies){
cout<<"Amount Purchased: "<<purchaseAmount;
cout<<" Amount Paid: "<<paidAmount;
cout<<" No. of Dollars: "<<dollars;
cout<<" No. of Quarters: "<<quarters;
cout<<" No. of Dimes: "<<dimes;
cout<<" No. of Nickels: "<<nickels;
cout<<" No. of Pennies: "<<pennies;
}
int main(){
int dollars=0,quarters=0,dimes=0,nickels=0,pennies=0;
float purchaseAmount,paidAmount;
getPurchaseAmt(purchaseAmount);
getAmtPaid(paidAmount);
calcChange(purchaseAmount,paidAmount,dollars,quarters,dimes,nickels,pennies);
outputChange(purchaseAmount,paidAmount,dollars,quarters,dimes,nickels,pennies);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.