Trying to do something in SPIM /MIPS, and cant seem to get it to work…
Basically, find all combinations of $1, $5, $10, $20, $50, and $100 for the inputted amount…
For example:
Enter positive dollar amount: ?1
Enter positive dollar amount: 11
11 bills: 1 1 1 1 1 1 1 1 1 1 1
7 bills: 5 1 1 1 1 1 1
7 bills: 1 5 1 1 1 1 1
7 bills: 1 1 5 1 1 1 1
7 bills: 1 1 1 5 1 1 1
7 bills: 1 1 1 1 5 1 1
7 bills: 1 1 1 1 1 5 1
3 bills: 5 5 1
2 bills: 10 1
7 bills: 1 1 1 1 1 1 5
3 bills: 5 1 5
3 bills: 1 5 5
2 bills: 1 10
Solution count: 13
Call count: 205
For the code (want to test it in AS2 first to make sure it works
), I have:
input = 11;
billsArray = new Array(1, 5, 10, 20, 50, 100);
printBill(input,0,0);
function printBill(a, bill, depth) {
remainder = a – bill;
if (remainder < 0) {
return;
} else if (remainder == 0) {
printSolution();
} else {
for (i = 0; i < billsArray.length; i++) {
printBill(remainder,billsArray[i],depth);
}
depth++;
}
}
function printSolution() {
trace(“end”);
}
But it only runs through it once… (1 1 1 1 1 1 1 1 1 1 1) bills, not 5 1 1 1 1 1 1, etc…
Any ideas?
Thanks 
Are you sure that the solutions:
7 bills: 5 1 1 1 1 1 1
7 bills: 1 5 1 1 1 1 1, etc
are distinct for the purposes of your exercise? It would be a shame to solve it that way only to find that the “position” of the bills doesn’t count (i.e that there is only one 7-bill solution for an input of $11 dollars.
Why not just write out the question exactly as it was presented to you. (I am assuming this is an academic exercise
)
- Has been a member for 4-5 years
- Author was Featured
- Contributed a Tutorial to a Tuts+ Site
- Netherlands
- Community Moderator
- Microlancer Beta Tester
- Sold between 10 000 and 50 000 dollars
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Exclusive Author
Reminds me of this Project Euler problem
It’s quite similar, so perhaps it’ll help you to see my code for solving that problem..
#include <iostream>
using namespace std;
unsigned int precomputed_array[201];
int coins[8] = {200,100,50,20,10,5,2,1};
int formwithpence(int v, int index = 0) {
if (precomputed_array[v] > -1)
return precomputed_array[v];
if (v == 0)
return 1;
if (v < 0)
return 0;
if (index == 7)
return 1;
int count = 0;
int coin = coins[index];
//anything that includes this coin
if (v-coin >= 0)
count += formwithpence(v-coin,index);
//anything that doesn't include it
count += formwithpence(v,index+1);
precomputed_array[v] = count;
return count;
}
void clearprecomputed() {
for (int i = 0; i < 201; i++) {
precomputed_array[i] = -1;
}
}
int main() {
clearprecomputed();
cout << formwithpence(200);
}
//</iostream> <-- ignore this line, the pre-tag on the forum wanted it included..
I guess the precomputation array isn’t that nessesary, but it saves a lot of time 
//anything that includes this coin
if (v-coin >= 0)
count += formwithpence(v-coin,index);
//anything that doesn't include it
count += formwithpence(v,index+1);
feyteng@gmail.com