1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
using namespace std;
void calcChange(int & changeDue, int index, int [], int [] );
int main()
{
//Variables
int price;
int payment;
int changeDue;
int coins[11] = { 1,2,5,10,20,50,100,200,500,1000,2000 };
int count[11] = { 0 }; // keeps the count and initialized at zero
cout << "Enter the total price: ";
cin >> price;
cout << "Enter payment amount: ";
cin >> payment;
changeDue = payment - price;
cout << "Total change due = " << changeDue << " pence.";
if (changeDue > 0)
{
for(int i = 10; i >= 0; i--)
{
calcChange(changeDue, i, coins, count);
if (changeDue == 0)
break;
}
}
cout << "\n\nCoinage to recieve: \n";
for(int i = 0; i < 11; i++)
cout << count[i] << " " << coins[i] << "p's.\n";
return 0;
}
void calcChange(int & changeDue, int index, int coins[], int count[] )
{
while(changeDue >= coins[index])
{
changeDue -= coins[index];
count[index] += 1;
}
}
|