Re: The old "making change" program



The following message is a courtesy copy of an article
that has been posted to comp.lang.c.moderated as well.

"Chris" <cmlally@xxxxxxxxx> writes:

> My new C programming class is absolutely kicking my butt. I have not
> been able to devote the time necessary to work on it at times, and am
> now falling behind.

IMO this is the time for you to be doing more self-study. If you
can't motivate yourself sufficiently to spend the time on your course,
there's only one person to blame. If you are having trouble with your
course, it's now the time to seek help from your course instructor, or
classmates.

> This is the task I have been given:
>
> Write a C function named change() that accepts a floating point number
> of total coins and the addresses of the integer variables named
> quarters, dimes, nickels, and pennies. The function should determine
> the number of quarters, dimes, nickels, and pennies in the total coins
> number passed to it and write these values directly into the respective
> variables declared in its calling function using pointers.
>
> Call the function change() from main() three times and print out the
> contents of the variables quarters, dimes, nickels, and pennies after
> each function return.

> Output should look like:
> TOTAL VALUE ENTERED: 1.88
> 7 quarters
> 1 dime
> 0 nickels
> 3 pennies
>
> I am not sure where to begin since they want multiple calls to the same
> program.

The question tells you where to begin. Reread it.

> Please help.

Well, the purpose of this group is not to do your homework; I'm
surprised the moderator approved it. This question is so basic, any
introductory C text would give you the answer in the first chapter
about pointers. If you are finding this hard, you should seek help
now, because passing by value and passing by reference are fundamental
concepts you need to understand to get anywhere with C.

What you need to do is very straightforward, and set out in the
question: write a function called change which takes a floating point
number and addresses of integers, and split the value into the various
types of coin.

So, you'd start with exactly what's stated in the question:

void
change(float total,
int *quarters,
int *dimes,
int *nickels,
int *pennies)
{
}

and then write the logic to do the splitting, which is trivial
division you would have been able to do at primary school. Being
British rather than American, I have no clue about the values of those
coins, so won't help there; that's your job.

When it comes to output, just write another function, such as

void
print_change(int quarters,
int dimes,
int nickels,
int pennies)
{
}

It should be pretty obvious, but if you ever do something more than
once, you should split it out into a function or loop to remove
duplicated code.


Regards,
Roger

--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.



Relevant Pages

  • Re: The old "making change" program
    ... > quarters, dimes, nickels, and pennies. ... > the number of quarters, dimes, nickels, and pennies in the total coins ... int *quarters, ...
    (comp.lang.c.moderated)
  • Re: The old "making change" program
    ... {int cents; ... of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. ... The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers. ...
    (comp.lang.c.moderated)
  • Re: The old "making change" program
    ... named quarters, dimes, nickels, and pennies. ... int *nickels, int *pennies) ... - Keith Thompson More details at: -- comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. ...
    (comp.lang.c.moderated)
  • Re: The old "making change" program
    ... the number of quarters, dimes, nickels, and pennies in the total coins ... int *quarters, ... amount %= 25; ...
    (comp.lang.c.moderated)
  • Re: Working with Cash and uses of Typedef
    ... Which data type should I use instead? ... If you're tracking tenths of pennies, that same 16 bit int ... Abstraction is a powerful tool, and when done properly can make life ...
    (comp.lang.c)