coin counting program
where have i been? nearly losing my mind writing my first algorithm.
after about a week of tortured evenings, i solved it thanks to a clear process i now have.
=^_^=
/***********************************************************
***********************************************************
CHANGE COUNTER
Displays how many quarters, dimes, nickels, and pennies are
needed for a money amount less than $1.00.
Bob Wirtz
Programming exercise. Displays how many quarters, dimes,
nickels, and pennies are needed for a money amount less
than $1.00. the program counts the number of coins needed
to equal the inputted amount from the largest denomination
to smallest (quarters, dimes, nickels, pennies)
Simple usage. Just run the program…
Version 1.0
Copyright 2012 Synkrex
***********************************************************
***********************************************************/
#include <iostream>
// coins
float quarter;
float dime;
float nickel;
float penny;
// coin count
int quarter_count;
int dime_count;
int nickel_count;
int penny_count;
float money_amount; // amount of money to be counted
int main (int argc, const char * argv[])
{
// 01 how much money is there to be counted?
std::cout « “Enter an amount of money less than $1.00: “ « “\n”;
std::cin » money_amount; // input money amount less than $1.00
// 02 what are the different values of the coins?
quarter = 0.25;
dime = 0.10;
nickel = 0.05;
penny = 0.01;
// 03 Q: which of the coins do i start counting first?
// A: count in descending order from the largest to the smallest compared
// to the value to be counted.
// 04 while the amount of money is greater than or equal to 0.25, then count quarters
while (money_amount >= 0.25) {
quarter_count++;
// 05 subtract the combined value of the counted quarters from the amount to
// be counted. the amount counted is now less.
money_amount = money_amount-quarter;
}
// 06 while the amount of money is greater than or equal to 0.09, then count dimes
while (money_amount >= 0.09) {
dime_count++;
// 07 subtract the combined value of the counted dimes from the amount to
// be counted. the amount counted is now less.
money_amount = money_amount-dime;
}
// 08 while the amount of money is greater than or equal to 0.04, then count nickels
while (money_amount >= 0.04) {
nickel_count++;
// 09 subtract the combined value of the counted dimes from the amount to
// be counted. the amount counted is now less.
money_amount = money_amount-nickel;
}
// 10 while the amount of money is greater than 0 and equal to or less than
// four, then count pennies
while (money_amount > 0 && money_amount <=0.04) {
penny_count++;
// 11 subtract the combined value of the counted pennies from the amount to
// be counted. the amount counted is now less.
money_amount = money_amount-penny;
}
// 12 show the amount of coins counted
std::cout « “quarters: “ « quarter_count « “\n”;
std::cout « “dimes: “ « dime_count « “\n”;
std::cout « “nickels: “ « nickel_count « “\n”;
std::cout « “pennies: “ « penny_count « “\n”;
return 0;
}





