0

I have a program where a user inputs an amount due, and an amount paid. A function makeChange(double amount) is passed the difference (paid - due) and returns a double array with the change due broken down by denomination.

double* makeChange(double amount) {
    double* change = new double[10];
    double values[10] = { 100, 50, 20, 10, 5, 1, .25, .10, .05, .01 };

    double remaining = amount;

    for (int i = 0; i < 10; i++) {
        change[i] = floor(remaining / values[i]);
        remaining -= (change[i] * values[i]);
    }

    return change;
}

I do not always get the output expected. For example, when I pass (0.05 - 0.00) as the amount, it returns saying I need 1 nickel. But if I pass (4.05 - 4.00) through as the amount, it returns saying I need 4 pennies.

What in the code is causing this to happen? Thanks!

  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Sep 29 '18 at 03:51
  • 2
    Rather than working with dollars, work with cents (pennies) and integers. This avoids the precision problems with floating point math. – 1201ProgramAlarm Sep 29 '18 at 03:55
  • @1201ProgramAlarm Thanks! I read the other post and I understand the problem now. I will switch over to cents and integers instead, thank you for your response. –  Sep 29 '18 at 03:59

2 Answers2

0

I don't think there is any problem with the code that you have posted. The problem could with the caller of this function, where you are interpreting the values in the change array. It would be more helpful if you can post that too here.

Cheers.

Rohitashwa Nigam
  • 399
  • 5
  • 16
0

First of all, in c++ you don't need to use pointers to create an array, you can use std::array. You can replace your function, and make it safer to your memory:

array<double, 10> makeChange(double amount) {
    cout << "Amount: " << amount << endl;
    array<double, 10> change = {};
    double values[10] = { 100, 50, 20, 10, 5, 1, .25, .10, .05, .01 };

    double remaining = amount;

    for (int i = 0; i < change.size(); i++) {
        change[i] = floor(remaining / values[i]);
        remaining -= (change[i] * values[i]);
    }

    return change;
}


int main() {
    array<double, 10> change;

    change = makeChange(4.05f - 4.0f);
    for (size_t i = 0; i < change.size(); i++) {
        cout << change[i] << endl;
    }

    return 0;
}

For the problem with the change- when you pass the floating values to the function, pass them with f, like this:

change = makeChange(4.05f - 4.0f);

This will make your calculate less unexpected, because you are taking numbers with less numbers after the dot (using float instead of double). Or even easier solution, replace all of the doubles with floats (Which have no effect in the concept, but will solve this problem):

array<float, 10> makeChange(float amount) {
    cout << "Amount: " << amount << endl;
    array<float, 10> change = {};
    float values[10] = { 100, 50, 20, 10, 5, 1, .25, .10, .05, .01 };

    float remaining = amount;

    for (int i = 0; i < change.size(); i++) {
        change[i] = floor(remaining / values[i]);
        remaining -= (change[i] * values[i]);
    }

    return change;
}


int main() {
    array<float, 10> change = makeChange(4.05 - 4.0);
    for (size_t i = 0; i < change.size(); i++) {
        cout << change[i] << endl;
    }
    return 0;
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22