mercredi 28 octobre 2015

Trying to make a binary like system in an array -Wont Work

I am confuded on how to go about the calulation but when i tried it on a piece of paper i found that i want to keep diving the remainder with the next value in array from left to right thats my function:

    void getChange(int paid, int cost){
    int r, k, s, cntr, c=1, value[8], money[8]={200,100,50,20,10,5,2,1};

    r=paid-cost;
    value[0]=r/money[0];
    k=r%money[0];                       
    for(money[c], cntr=1;cntr<8;c++, cntr++){

        if (k !=0){
            value[c]= k/money[c];
            k=value[c]%money[c];
        }
        else{
            value[c]=0;
            k=r/money[c+1];
        }
    }

    int i, j;
    for (i=0; i<8; i++) {
        printf("%i \t", money[i]);
    }
    printf("\n \n");

    for (j=0; j<8; j++) {
            printf("%i \t ", value[j]);
        }

}

Aucun commentaire:

Enregistrer un commentaire