0

I am most likely the greenest greenhorn on this site. Currently in school for software development and finishing my first quarter. I'm working on a simple problem where I need to create a function that returns exact change (as the title says). For example, if my input is $1.18 the output should read 1 dollar, 1 dime, 1 nickel, 3 pennies. I don't want the answer as I do want to learn this on my own. I'm posting more for a nudge in the right direction. My code is posted below:

fun main(args: Array<String>) {
    var changeDue = 1.18;
    Money(changeDue);
}
fun Money(changeDue: Double){
    var dollars = Math.floor(changeDue / 1.0);
    var changeDue0 = changeDue - (dollars * 1.0);

    var quarters = Math.floor(changeDue / 0.25);
    var changeDue1 = changeDue - (quarters * 0.25);

    var dimes = Math.floor(changeDue / 0.10);
    var changeDue2 = changeDue - (dimes * 0.10);

    var nickel = Math.floor(changeDue / 0.05);
    var changeDue3 = changeDue - (nickel * 0.05);

    var penny = changeDue;

    return Unit;
}

If someone has already answered this in the past, I apologize for posting a second thread and wasting your time.

Montaser Sobaih
  • 305
  • 2
  • 7
bgZAG
  • 1
  • 2
  • What is your question?  Is there a problem with the code you've written so far, or are you asking about what should come next? – gidds Mar 15 '21 at 19:59
  • My apologies for not being more clear. My question is what comes next? In my main() the var changeDue should pass to the function Money, correct? When I return Unit i'm getting "no output" so I'm confused as to what I'm doing incorrectly. – bgZAG Mar 15 '21 at 20:07
  • OK, you've made a good start: your function takes in the amount of money paid, and you calculate from it.  (I'm not sure `changeDue` is a good name for it, though — isn't that what your function should return?)  Now, you might want to work backwards a little: what _type_ of value do you want your function to return, and what do you want your `main()` function to do with it?  Then look at how your function might construct that value. – gidds Mar 15 '21 at 20:15
  • (Also, according to Kotlin coding conventions, function names should start with a lower-case letter; that helps to distinguish them from classes/types.  That won't help your code work, of course!  But it'll make it slightly easier for people to read.) – gidds Mar 15 '21 at 20:15
  • Ok so what if I use "change" instead of "changeDue" for the name of the function. And isn't the function money declaring the type to be Double already? Also thank you for taking the time to assist me. I really want to understand this. – bgZAG Mar 15 '21 at 20:34
  • A proper function name should start with a verb that describes what it does. Currently the name of your function is `Money` and the name of your parameter is `changeDue` (which is a fine name for the parameter). I would call the function something like `optimizeChange`. To make output, you need to print something. Either your function can return a String that describes all the bills and coins you've calculated which the `main` function can use with `println()`, or the function can directly use `println()` and not return anything. There's no point in returning Unit because that's implicit. – Tenfour04 Mar 15 '21 at 20:57
  • Your use of the `floor` function returns a Double, but it since you are counting an integer number of dollar bills or coins, it would make logical sense to calculate an Int. For example, `val dollars = (changeDue / 1.0).toInt()`. The `toInt()` function rounds towards zero. There are some mistakes I see in your calculations, but maybe you want to find those for yourself. – Tenfour04 Mar 15 '21 at 21:06
  • I appreciate all the help. Been working on this for some time now and still can't get it to work. Thank you for taking the time to comment! – bgZAG Mar 15 '21 at 23:01

0 Answers0