Skip to content

Instantly share code, notes, and snippets.

@mark-d-holmberg
Created May 26, 2014 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-d-holmberg/189b173ea148b2c47177 to your computer and use it in GitHub Desktop.
Save mark-d-holmberg/189b173ea148b2c47177 to your computer and use it in GitHub Desktop.
This demonstrates how a binary monetary system is clearly superior to our USD.
/* {{{
**************************
* Mark Holmberg : tiggers.no.tail@gmail.com
* Purpose: calculate the average number of coins needed to make change
* Tue Oct 13 15:46:34 MDT 2009
* CS 3310 : Discrete Math
* }}} */
#include <iostream>
#include <vector>
/*US dollars*/
#define HALF 50
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1
/*Binary System*/
#define SIXTY_FOUR 64
#define THRTY_TWO 32
#define SIXTEEN 16
#define EIGHT 8
#define FOUR 4
#define TWO 2
#define ONE 1
namespace mdh {
int computeCoins( std::vector<int> coinage_system, int iAmount ) {
std::vector<int>::iterator it; //iterator
double div_result; //result of division
int mod_remainder; //holds the remainder of division
int num_coins; //determines how many coins it takes this modulo/division time
//int original_amount = iAmount; //iAmount will change, so we want to know what we were passed originally
int total_coins = 0; //the total coins
//iterate through all possible denomination values in the vector
for( it = coinage_system.begin(); it != coinage_system.end(); it++ ) {
if( iAmount % (*it) == 0 ) {
//i.e. 100 is evenly divided into 2 half dollars so count the number of coins
num_coins = iAmount / (*it);
if( num_coins > 0 ) { total_coins = total_coins + num_coins; }
break; //if we don't have this we get ALL possible combos. we want highest amount coins first
} else {
//divide
div_result = iAmount / (*it);
if( div_result > 0 ) { total_coins += div_result; }
//something divided in
if( div_result != 0 ) {
num_coins += div_result;
}
mod_remainder = iAmount % (*it); /*find out the remainder*/
iAmount = mod_remainder; //only divide by whats left
}//end else clause
}//end for loop
return total_coins;
}//end computeCoins
double calculateAverage( double iMyInput ) {
return iMyInput / 100.0;
}
}//end mdh namespace
using namespace std;
int main() {
std::cout.setf(std::ios::fixed); //sets the level of precision
//--------------------------------------------------
// American US Monetary System
//--------------------------------------------------
std::vector<int> US_SYSTEM;
double us_average = 0;
US_SYSTEM.push_back( HALF );
US_SYSTEM.push_back( QUARTER );
US_SYSTEM.push_back( DIME );
US_SYSTEM.push_back( NICKEL );
US_SYSTEM.push_back( PENNY );
for( int i = 100; i > 1; i-- ) {
int temp = mdh::computeCoins( US_SYSTEM, i );
us_average += temp;
}
us_average = mdh::calculateAverage( us_average );
std::cout << "American English System average is: " << us_average << std::endl;
//--------------------------------------------------
//--------------------------------------------------
//Binary Monetary System
//--------------------------------------------------
std::vector<int> BINARY_SYSTEM;
double binary_average = 0;
BINARY_SYSTEM.push_back( SIXTY_FOUR );
BINARY_SYSTEM.push_back( THRTY_TWO );
BINARY_SYSTEM.push_back( SIXTEEN );
BINARY_SYSTEM.push_back( EIGHT );
BINARY_SYSTEM.push_back( FOUR );
BINARY_SYSTEM.push_back( TWO );
BINARY_SYSTEM.push_back( ONE );
for( int k = 100; k > 1; k-- ) {
int not_temp = mdh::computeCoins( BINARY_SYSTEM, k );
binary_average += not_temp;
}
binary_average = mdh::calculateAverage( binary_average );
std::cout << "Binary System average is: " << binary_average << std::endl;
//--------------------------------------------------
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment