Help coming up with an algorithm for this set of code: #include \"stdafx.h\" #in
ID: 3573533 • Letter: H
Question
Help coming up with an algorithm for this set of code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// Declare variables
double due, paid, difference;
int change, dollars20, dollars10, dollars5, dollars1;
int quarter, nickel, dime, penny;
int main()
{
// Input
cout << "Total Amount Due: $";
cin >> due; cout << endl;
cout << "Total Amount Paid: $";
cin >> paid; cout << endl;
if (paid > due)
{
// The amount difference calculate new variables.
difference = paid - due;
cout << fixed << setprecision(2) << "Total Change Due: $" << difference << endl << endl;
difference = difference * 100;
change = (int)(difference);
//calculation of money that is due
dollars20 = change / 2000;
change = change % 2000;
dollars10 = change / 1000;
change = change % 1000;
dollars5 = change / 500;
change = change % 500;
dollars1 = change / 100;
change = change % 100;
quarter = change / 25;
change = change % 25;
dime = change / 10;
change = change % 10;
nickel = change / 5;
change = change % 5;
penny = change;
// Output
cout << "Number of twenty dollar bills due: " << dollars20 << endl;
cout << "Number of ten dollar bills due: " << dollars10 << endl;
cout << "Number of five dollar bills due: " << dollars5 << endl;
cout << "Number of one dollar bills due: " << dollars1 << endl;
cout << "Number of quarters due: " << quarter << endl;
cout << "Number of dimes due: " << dime << endl;
cout << "Number of nickels due: " << nickel << endl;
cout << "Number of pennies due: " << penny << endl;
}
else {
cout << "No change is due." << endl;
}
cin.ignore(100);
return 0;
}
Explanation / Answer
Algorithm :
Step1 : Start
Step2 : Declare varibles due,paid,difference
Step3 : read varibles due ,paid
Step4 : if paid greaterthan due then
4.1 : calculate difference
4.2 : difference shown as multiples of 20$,10$,5$,1$,quarter,dime,nickel,penny
4.2 : stop
else
4.3 : Display No change due
4.5 : stop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.