Array Processing

I am working with arrays and I need to figure out how to get the minimum amount of change with 50, 20, 10, 5, 1, with two loops, and display the change in a horizontal row. I have my code below and I know that the calculation I have set up going into the "money" variable does not give the correct change. Is the solution gonna be a matter of finding the right calculation and the rest of the code is fine? Or, do I need more code to handle the values form the array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
using namespace std;

int main()
{
    int bankNotes[5] = {50,20,10,5,1};
    int money = 0, change = 0;

    cout << "Enter amount of money please: ";
    cin >> money;

    for(int count = 0; count < 5; count++)
    {
        while(money > bankNotes[count])
        {
            money = (money - bankNotes[count]);
            cout << money << " ";
        }
    }
    return 0;
}
Try printing bankNotes[count] instead.
That looks better but it's still off. If I type in 125, I will get "50 50 20 1 1 1 1" and the correct answer should be "50 50 20 5."
Perhaps it would be better to study the results of say
125 / 50
125 % 50
Got it to work! If money = 1, then "while(money > bankNotes[element 4])" would never be true and the while loop would not be entered to give evaluation and print. So, I put ">=" inside the while loop and it solved the problem.

I am trying to understand how printing "bankNotes[count]" gives the correctly calculated change though? It seems that the money variable is the one holding calculations and the "bankNotes[count]" holds the incremented counter from 0 to 4 which corresponds to the value at each element. So, I thought printing "bankNotes[count]" would just display the values stored in the array.
Topic archived. No new replies allowed.