Home Forums Re: ATM

#3335
Humayan
Participant

Heres some example code ( this is an algorithm I adapted from “Data structures and Problem Solving Using C++” by Mark Allen West ). The code is in C++ , but you can adapt it ( I like to program most things in C++ , but without the use of OPP ). Anyway , heres the code:


/****************************************************************
* File Name : c:programshelpshell.cpp
* Date : January,3,1980
* Comments : new project
* Compiler/Assembler : visual c++ 6.0
* Program Shell Generated At: 3:29:12 a.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >
using namespace std;

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
void makeChange ( int * bills , int differentBills , int change , int *& billsUsed , int *& addedBill );
void outputResults ( int change , int * billsUsed , int * billsAdded );
//##################################################################################

//main function ******************************
int main ( )
{
int bills [ 5 ] = { 1 , 5 , 10 , 20 , 50 };
int * billsUsed = 0; //bills used for various amounts
int * addedBill = 0; //bill added to make new total


cout << "enter an amount ";
int amount;
cin >> amount;
makeChange ( bills , 5 , amount , billsUsed , addedBill );
outputResults ( amount , billsUsed , addedBill );
delete billsUsed;
delete addedBill;
return 0 ;
}

/******************************* FUNCTION DEFINITION ******************************
Name : makeChange
Parameters :
bills a(n) int * ( int * ) ,
billsOutput a(n) int ( int ) ,
change a(n) int ( int )

Returns: user defined type , int
Comments:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void makeChange ( int * bills , int differentBills , int change , int *& billsUsed , int *& addedBill )
{


billsUsed = new int [ change + 1 ];
addedBill = new int [ change + 1 ];

billsUsed [ 0 ] = 0; //zero bills used to change zero dollars
addedBill [ 0 ] = 1;

int dollars = 1;
while ( dollars <= change )
{
int minimumBills = dollars; //assume all ones at first
int newBill = 1; //try to use one's first
int j = 0;
while ( j < differentBills )
{

//current bill is too large to make change
if ( bills [ j ] > dollars )
{
j ++;
continue;
}
//check if bill makes a new minimum bill total
if ( billsUsed [ dollars - bills [ j ] ] + 1 < minimumBills )
{
//save new minimum along with bill used to make it
minimumBills = billsUsed [ dollars - bills [ j ] ] + 1;
newBill = bills [ j ];
}
j ++; //try next bill
}
billsUsed [ dollars ] = minimumBills;
addedBill [ dollars ] = newBill;
dollars ++;
}
}
/******************************* FUNCTION DEFINITION ******************************
Name : outputResults
Parameters :
change a(n) int ( int ) ,
billsUsed a(n) int * ( int * ) ,
billsAdded a(n) int * ( int * )

Returns: user defined type , void
Comments:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void outputResults ( int change , int * billsUsed , int * billsAdded )
{

cout << endl << endl;
cout << "The number of bills needed to change " << change;
cout << " is " << billsUsed [ change ] << endl << endl;
cout << "The bills used are:" << endl;
int total = 0;
int i = change;
while ( total != change )
{

cout << "tone ";

switch ( billsAdded [ i ] )
{
case 1:
{
cout << "dollar" << endl;
};
break;
case 5:
{
cout << "five" << endl;
};
break;
case 10:
{
cout << "ten" << endl;
};
break;
case 20:
{
cout << "twenty" << endl;
};
break;
case 50:
{
cout << "fifty" << endl;
};
break;
}
total += billsAdded [ i ];
i -= billsAdded [ i ];
}
return;
}

and heres some output examples:

enter an amount 88

The number of bills needed to change 88 is 7
The bills used are:
one dollar
one dollar
one dollar
one five
one ten
one twenty
one fifty
Press any key to continue
enter an amount 77

The number of bills needed to change 77 is 5
The bills used are:
one dollar
one dollar
one five
one twenty
one fifty
Press any key to continue

sorry for the alignment , this forum messes up my tabing….