Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #69172

Re: Program to compute amount of coins

From bartekltg <bartekltg@gmail.com>
Newsgroups comp.lang.c
Subject Re: Program to compute amount of coins
Date 2015-09-03 19:32 +0200
Organization ATMAN - ATM S.A.
Message-ID <msa07n$8it$1@node2.news.atman.pl> (permalink)
References <1436c460-1201-4574-b672-d121a9f306b6@googlegroups.com> <ms9rbq$3m5$1@node2.news.atman.pl> <07ead014-6486-43d1-80c5-a1b6e35e629e@googlegroups.com>

Show all headers | View raw


On 03.09.2015 19:08, Alla _ wrote:
> On Thursday, September 3, 2015 at 8:09:44 PM UTC+4, bartekltg wrote:
>> On 03.09.2015 17:54, Alla _ wrote:
>>> Hello!
>>>
>>> I am working on a program that should
>>> compute the amount of coins: the user
>>> enters amount, and, to make it simple
>>> any amount entered is in dollars, not
>>> cents.
>>>
>>> Therefore, I have to:
>>> - prompt the user to enter the number
>>> - convert that into cents, round the result
>>> - compute how much of 25¢ coins are
>>> contained is this number
>>>
>>> Initially, I split the program, wrote
>>> the 'prompt the user part', and then
>>> combined with a computational part.
>>>
>>> "Prompting" part, including "rounding",
>>>    works.
>>>
>>> Computational part doesn't produce the
>>> correct outcome. I've tried different
>>> approaches.
>>>
>>> Please, help with hints and tips, but
>>> do not write a program for me - if I fail
>>> to get your tips, I will ask again :)
>>>
>>> "Algorithm" that I thought can help
>>> to solve the computational part is:
>>>
>>> use while loop, take amount; until amount
>>> is bigger than 24, reduce amount by 25, and
>>> count the number of 25¢ coins with each iteration
>>> of the loop.
>>>
>>> #include <stdio.h>
>>> #include <math.h>
>>>
>>> int main(void)
>>> {
>>>       float amount; /** I should ask for a float value in $,
>>>                     and then convert it to amount in cents**/
>>
>> Amount in uninitialized. Propabli equal to 0.0
>>
>>>       int coin25 = 0;
>>>       int change = round(amount * 100);
>>
>> 'Change' is computed based on uninitiated variable 'amount'.
>> It propably is equal to 0.
>>
>>>
>>>       printf ("Please, enter your amount in $: \n");
>>>
>>>       if (scanf("%f", &amount) == 1)
>>
>> You put value from input to 'amount', but 'change'
>> is still zero. Do you earlier write only in mathematica?
>> This is the same problem as in #triangle,
>>
>> "int change = round(amount * 100);"
>>
>> do not mean a relation between this two variables.
>> It mean 'take current value of amount, compute a function
>> and the result put in variable "change"'.
>> And the are independent.
>>
> I am not sure I understand what the difference is.

How?
Try to contemplate the difference between these two

main(){
int a=0;
int b=0;

b=a*a;
a=2;

printf("%d\n",b);
}

and

main(){
int a=0;
int b=0;

a=2;
b=a*a;

printf("%d\n",b);
}

The first returns 0, the second return 4.

> I intended exactly as you say: 'take current value
> of amount,compute a function and the result put in
> variable "change"'.

But the value of 'amount' was 0.
You computed the 'change' and _later_ ask user
about amount. This is wrong order.
It is like you first click 'send' button and then
write email.

Just change the order:

....
     printf ("Please, enter your amount in $: \n");

     if (scanf("%f", &amount) == 1)
     {
	int change = round(amount * 100); //***IMPORTANT PART***///

         while (change >= 24)
         {
             change = change - 25;
             coin25++;
         }

     }
....


> Then, within the loop, decrement 'change' with
> each iteration of the loop.

But change is 0. Always. Because you first computed
'change' based on 'amount', and then read input ant put
in ich 'amount'.

> Do you say that this is incorrect?

The main mistake is earlier.
This loop is only a _very_ ineffective algorithm
to compute integer division.

Put 21474830 as input. It computes it quite long time.
But not this is your problem.


bartekltg

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 08:54 -0700
  Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 17:07 +0100
    Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:02 -0700
    Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:12 -0700
    Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:18 -0700
      Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:30 +0100
        Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:53 +0200
          Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 21:03 +0100
            Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 22:29 +0200
              Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 21:51 +0100
                Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 23:37 +0200
                Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 23:07 +0100
                Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-03 18:24 -0400
                Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 00:30 +0200
                Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 16:36 -0700
                Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:00 -0700
              Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:58 -0700
                Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-07 05:07 +0200
                Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 13:14 +0100
                Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-08 02:39 -0700
                Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-08 18:29 +0200
                Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-08 13:15 -0400
                Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-08 10:16 -0700
          Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-04 04:09 -0700
            Re: Program to compute amount of coins Bartc <bc@freeuk.com> - 2015-09-04 12:16 +0100
            Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 14:11 +0200
          Re: Program to compute amount of coins Ike Naar <ike@iceland.freeshell.org> - 2015-09-04 19:06 +0000
            Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-04 15:12 -0400
            Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-05 01:54 +0200
              Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-05 01:01 +0100
                Re: Program to compute amount of coins bartekltg <bartek@gmail.com> - 2015-09-05 02:50 +0200
  Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 18:09 +0200
    Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 17:11 +0100
      Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 18:38 +0200
    Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:08 -0700
      Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:20 +0100
      Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:32 +0200
    Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 12:18 -0700
  Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:16 -0700
    Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:24 +0100
      Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:37 -0700
        Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 19:02 +0100
  Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:42 -0700
    Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:57 +0200
    Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 19:05 +0100
  Re: Program to compute amount of coins Bartc <bc@freeuk.com> - 2015-09-03 21:23 +0100
    Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 21:55 +0100
    Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 16:39 -0700
      Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:02 -0700
        Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-06 17:04 -0700
  Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-04 03:48 -0700
    Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 14:04 +0200
    Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-04 14:23 +0100

csiph-web