0

My Programm greedy(less comfortable)

Hello I am relatively new to programming and C in general.

I have left the comments next to the points I have questions to, please don't give me any solutions, as I really want to learn and understand myself, I just want to understand the points.

Moreover, I feel I am missing something here in general,

My approach generally solves the problem, however, it doesn't strictly follow the given steps. i.e. as described in *3 in my comments. Moreover, I feel I am missing something all together here, I feel I should use an Array, list, or some sort of Databank or a pointer (which all have not be introduced yet), so I could keep a count of the times an object was used and to then subtract that from the Number to give the result and return the number of coins.

Or maybe I was on the wrong track and my mind has run me in the wrong direction.

Furthermore, I have compiled and fully ran through my Programm, iterates through the value and outputs as suspected. However, is there no better or clearer way of doing this, I have seen IAR enverionment for C for example, which is embedded, there I can actually run through the whole Programm and see the values change and fully understand everything.

I will be looking forward to your input to further understand and deepen my knowledge.

0

The easiest way to deal with money is converting everything to integer first (by dividing by the smallest unit, here a cent), as integers don't suffer from same rounding errors as they are common with floating point (no way to accurately represent 0.01 in binary, so it's rounded to a finite length binary number).

In the screenshot, you included the last three loops within the first, which is probably a mistake.

For the solution, you would have to count the coins.

You could even do without the loops, by using division, like

int owed_in_cents = Math.round(owed * 100); // without rounding, you'll see odd effects as 419.999999997 becomes 419
int coins = 0;
// quarters
coins += owed_in_cents / 25; // integer division truncates, rounds towards 0
owed_in_cents %= 25; // assign the remainder of division by 25

and so on.

And no, for this easy task you won't need an array, a linked list, a tree, a hashmap, a database or anything like that.

3
  • After going through my Programm and changing my approach I have now solved this task according to the solution. Thank you for bringing me back on track. – Der Traumwanderer Jul 24 '17 at 11:28
  • I have a question regarding the order in which I have declared my variables, does this have effect on compile and runtime?, i.e. I have declared penny 0.01 first and then up to the highest number. – Der Traumwanderer Jul 24 '17 at 11:34
  • Order of declaration affects memory layout (where the variables are placed on the stack), but if they are in the same scope and do not depend on each other, it should not matter for the programme logic. – Blauelf Jul 24 '17 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .