Professional Documents
Culture Documents
Pseudo Code Coin Changing
Pseudo Code Coin Changing
Pseudo Code Coin Changing
Mata Kuliah :
[ CF-1333 Alpro 2 ]
Disusun oleh :
Example :
I have money 500. I want to exchange with available coin. There are 50 as much as
5, 25 as much as 10, and 10 as much as 15. How much coin is needed to this
exchange? The answer of this problem have two combination. There are maximal
combination and minimum combination.
Solution :
To understand the problem, make a table which contain some available coin with
their amount.
For the minimum combination the kind of available coin must be sort
(descending). Here is the table :
0 50 5 Looked for
1 25 10 Looked for
2 10 15 Looked for
For the maximum combination the kind of available coin must be sort
(ascending). Here is the table :
0 10 15 Looked for
1 25 10 Looked for
2 50 5 Looked for
This time I will explain the solution to find minimum combination. The way to find
maximal combination is same. The different is in the procces of sorting coin.
To find minimum combination, the first step , we take the first kind of coin (50).
If after this taking the value still less than or not equals with the value of money
then we must take again the coin. If the first kind of coin (50) was taken all and the
sum still less than or not equals with the value of money then we take again the coin
in the next kind (25). This process will looping until the sum of coin combination is
equals with the money that we want to exchange.
In this process we must pay attention for some case. When we taking the first
kind of coin and weren’t taking all, so we have to take again the kind of this coin. If
after this taking the sum is more than the value of our money, then this taking must
be canceled and we take the next kind of coin. The next process is same with the
previous explanation. The process is looping until the sum of coin combination equals
with the money that we want to exchange.
Pseudocode Coin Changing Problem
From the previous explanation of the solution coin changing problem, I can make
the pseudocode.
The input is the value of money that we want to exchange (n), amount kind of
available coin (counter) and amount of each kind of available coin. Data about the
amount of each kind of available coin must be placed into array. And must be sorting
by ascending or descending, according to our needed (want to find maximal
combianation or minimum combination).
In my pseudocode, I prepare 3 arrays, there are coin array is used for saving
the kind of available coin, denom array is used for save amount of each kind of
coin, and the last array is amount array is used for saving amount of each coin that
used for maximal combination or minimum combination. In this pseudocode I make
3 method . There are 2 variable as global variable. There are n variable , sum
variable (used for saving the sum of coin combination that be arrangged).