home Sandy, UT
Wednesday, 2017.11.22
04:10 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 4.3°C (39.7°F)
Barometer: 1029 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for C++ class.
This one was really easy.
Given an amount of money in coins, determine the composition.
To go quick I asked if I could skip to input the amount, and play a fixed amount, that I chose balanced in pieces and was 0.69 cents.
Download the executable: coins.exe
June 29, 2004

Composition in Coins of a Given Amount

coin.h
struct Coin {
  long count;
  int denom;
  char *single, *multiple;
  };
coins.cpp
/**
  *  @title: coins.cpp
  *  @date: 2004.06.29
  *  @author: vinnie
  */

#include <iostream>
#include <stdlib.h>
#include "coin.h"

using namespace std;

void initArr( Coin cAr[] )
{ cAr[0].count = 0;
  cAr[0].denom = 25;
  cAr[0].single = "quarter";
  cAr[0].multiple = "quarters";

  cAr[1].count = 0;
  cAr[1].denom = 10;
  cAr[1].single = "dime";
  cAr[1].multiple = "dimes";

  cAr[2].count = 0;
  cAr[2].denom = 5;
  cAr[2].single = "nickel";
  cAr[2].multiple = "nickels";

  cAr[3].count = 0;
  cAr[3].denom = 1;
  cAr[3].single = "penny";
  cAr[3].multiple = "pennies";
  }

void change(int &amnt, Coin &c)
{ c.count = amnt / c.denom;
  amnt    = amnt % c.denom;
  }

void print( const Coin &c )
{
  if ( c.count > 1 ) cout << c.count << " " << c.multiple << '\n';
  if ( c.count == 1 ) cout << c.count << " " << c.single << '\n';
  }

void main( )
{
Coin coinArr[4];
initArr( coinArr);

const int iMin = 0, iMax = 69;
int iAmount = 0;

for ( int ind = iMin; ind <= iMax; ind++)
{ iAmount = ind;
  cout << "Amount = 0." << iAmount << " cents" << '\n';
  for ( short i = 0; i < 4; i++)
    { change( iAmount, coinArr[i] );
      print( coinArr[i]);
      }
  cout << '\n';
  }
cout << "done!" << '\n';
}

2004.06.29

Vincenzo Maggio