Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to compute amount of coins |
| Date | 2015-09-04 14:11 +0200 |
| Organization | ATMAN - ATM S.A. |
| Message-ID | <msc1on$7hp$1@node2.news.atman.pl> (permalink) |
| References | (1 earlier) <Xe_Fx.427800$121.345061@fx19.am4> <e3fbe659-268a-4a19-8aa5-a9c4bdf76438@googlegroups.com> <Ns%Fx.510855$JH3.131464@fx37.am4> <msa1e1$9oq$1@node2.news.atman.pl> <7163771e-9b76-4553-b81b-a5d280f3322c@googlegroups.com> |
On 04.09.2015 13:09, Alla _ wrote:
> On Thursday, September 3, 2015 at 9:53:16 PM UTC+4, bartekltg wrote:
>> On 03.09.2015 19:30, Richard Heathfield wrote:
>>> On 03/09/2015 18:18, Alla _ wrote:
>>>
>>> <snip>
>>>
>>>> According to assignment, I have to use
>>>> loops (any loop of my choice).
>>>> However, I will now, of course, try without
>>>> the loop, as well.
>>>
>>> If there are constraints on your problem, you should mention them at the
>>> outset.
>>>
>>> Okay, so if you're not allowed to do it the right way, you'll have to
>>> work out how to do it the wrong way:
>>>
>>> if(scanf("%f", amount) == 1)
>>> {
>>> int pennies = amount * 100;
>>> int quarters = 0;
>>> int dimes = 0;
>>> int nickels = 0;
>>> while(pennies >= 25)
>>> {
>>> ++quarters;
>>> pennies -= 25;
>>> }
>>> while(pennies > 10)
>>> {
>>> ++dimes; and so on.
>>
>> I'm sure that these loops teacher mentioned was a loop over "face
>> value".
>>
>>
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main(void)
>> {
>> double amount;
>> const unsigned int coin_value[]= {25,10, 5,1 } ;
>>
>> printf ("Please, enter your amount in $: \n");
>>
>> if (scanf("%lf", &amount) == 1)
>> {
>> if (amount > pow(2.0,32.0)*0.9999){
>> printf ( "Not enough quarters in the universe\n" );
>> }
>> else
>> {
>> unsigned int cents = amount*100.0;
>> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++)
>
> I would be grateful if you explain what does
> sizeof(coin_value)/sizeof(coin_value[0]
> do; how does it work here?
>
>> {
>> int coins = cents/coin_value[i];
>> cents = cents%coin_value[i];
>> printf("%d x %dc\n",coins, coin_value[i]);
>> }
>> }
>>
>> }
>> }
>
sizeof(coin_value) is the size (in bytes) of whole array.
sizeof(coin_value[0]) is the size of the first element,
also in bytes. In c the size of all elements of a array is the same.
So sizeof(coin_value)/sizeof(coin_value[0]) if the number of elements
in the array.
One can write it clearer:
int number_of_coin_types = sizeof(coin_value)/sizeof(coin_value[0])
...
for (int i=0; i<number_of_coin_types; i++)
bartekltg
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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