PhotoDune

Anyone familiar with Assembly language?

1089 posts
  • Has been a member for 5-6 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Won a Competition
  • Microlancer Beta Tester
  • United States
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
lifwanian says

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 :)

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says

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 ;) )

3371 posts
  • Has been a member for 4-5 years
  • 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
+4 more
Joost moderator says

Reminds me of this Project Euler problem :o It’s quite similar, so perhaps it’ll help you to see my code for solving that problem..

The code I created to solve that, was:
#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 ;)

The main difference I see when comparing our code is that I have a double recursion:
//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);
1 post
  • Has been a member for 0-1 years
  • United States
shiqing says
I am also very interesting about this question~ Do u got the MIPS code for this question, if you do, can you email it to me? Thank you so much! :) feyteng@gmail.com
by
by
by
by
by