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


Groups > comp.lang.c > #69147 > unrolled thread

Program to compute amount of coins

Started byAlla _ <modelling.data@gmail.com>
First post2015-09-03 08:54 -0700
Last post2015-09-04 14:23 +0100
Articles 20 on this page of 53 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 1 of 3  [1] 2 3  Next page →


#69147 — Program to compute amount of coins

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 08:54 -0700
SubjectProgram to compute amount of coins
Message-ID<1436c460-1201-4574-b672-d121a9f306b6@googlegroups.com>
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**/
    int coin25 = 0;
    int change = round(amount * 100); 
    
    printf ("Please, enter your amount in $: \n");
    
    if (scanf("%f", &amount) == 1)
    {
                
        while (change >= 24)
        {
            change = change - 25;
            coin25++;
        }
    
    }
    printf ("%i\n", coin25);
    
}

Thank you!

[toc] | [next] | [standalone]


#69151

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-03 17:07 +0100
Message-ID<Xe_Fx.427800$121.345061@fx19.am4>
In reply to#69147
On 03/09/2015 16: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.

In your code, you have >= 24 when you meant to write > 24

But is there some special reason you are using a loop? You don't need it.

coin25 = change / 25;

will give you the right result, because of the way integer division works.

change is an int, and 25 is an int, so change / 25 is an int, and it's 
exactly the int value you want. No loop required.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#69163

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 10:02 -0700
Message-ID<a43f84b5-201e-4ebc-8678-56cde5f425a2@googlegroups.com>
In reply to#69151
On Thursday, September 3, 2015 at 8:07:27 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 16: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.
> 
> In your code, you have >= 24 when you meant to write > 24
> 
> But is there some special reason you are using a loop? You don't need it.
> 
> coin25 = change / 25;
> 
> will give you the right result, because of the way integer division works.
> 
> change is an int, and 25 is an int, so change / 25 is an int, and it's 
> exactly the int value you want. No loop required.
> 
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within

I am using a loop because the user might 
enter a big number, therefore I thought 
that the loop will help to go through the 
value until it is bigger than 24. 

It's just the beginning of the program.
After this part, I have to check the division 
further. 

As one of the options, I've tried 
to use modulo to no avail.
I will try the division now.

[toc] | [prev] | [next] | [standalone]


#69166

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 10:12 -0700
Message-ID<742f786a-2e38-4645-96e9-1393a5543ad0@googlegroups.com>
In reply to#69151
On Thursday, September 3, 2015 at 8:07:27 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 16: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.
> 
> In your code, you have >= 24 when you meant to write > 24
> 
> But is there some special reason you are using a loop? You don't need it.
> 
> coin25 = change / 25;

of course; I've just realized why this 
would work. Simple math, produces exact amount
of coins, because both variables are ints,
as you pointed, and hence all decimal values
will be omitted.
> 
> will give you the right result, because of the way integer division works.
> 
> change is an int, and 25 is an int, so change / 25 is an int, and it's 
> exactly the int value you want. No loop required.
> 
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#69168

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 10:18 -0700
Message-ID<e3fbe659-268a-4a19-8aa5-a9c4bdf76438@googlegroups.com>
In reply to#69151
On Thursday, September 3, 2015 at 8:07:27 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 16: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.
> 
> In your code, you have >= 24 when you meant to write > 24
> 
> But is there some special reason you are using a loop? You don't need it.
> 
> coin25 = change / 25;
> 
> will give you the right result, because of the way integer division works.
> 
> change is an int, and 25 is an int, so change / 25 is an int, and it's 
> exactly the int value you want. No loop required.
> 
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within

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.

[toc] | [prev] | [next] | [standalone]


#69171

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-03 18:30 +0100
Message-ID<Ns%Fx.510855$JH3.131464@fx37.am4>
In reply to#69168
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.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#69176

Frombartekltg <bartekltg@gmail.com>
Date2015-09-03 19:53 +0200
Message-ID<msa1e1$9oq$1@node2.news.atman.pl>
In reply to#69171
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++)
             {
                 int coins = cents/coin_value[i];
                 cents = cents%coin_value[i];
                 printf("%d x %dc\n",coins, coin_value[i]);
             }
         }

     }
}


[toc] | [prev] | [next] | [standalone]


#69197

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-03 21:03 +0100
Message-ID<878u8ngti7.fsf@bsb.me.uk>
In reply to#69176
bartekltg <bartekltg@gmail.com> writes:
<snip>
> I'm sure that these loops teacher mentioned was a loop over "face
> value".

Yes, that must surely be it.

> #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){

That's an odd number to test against.  For 32-bit unsigned ints things
go wrong for values < 0 and >= pow(2, 32)/100.

>             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++)

Most teachers would rather not have to explain that test too early on!
Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
test of coin_value[i] > 0.  As a consequence, since I am wary about
teaching for loop "mission creep", this would become a while loop:

  int i = 0;
  while (coin_value[i] > 0) {
      ....
      i += 1;
  }

>             {
>                 int coins = cents/coin_value[i];
>                 cents = cents%coin_value[i];
>                 printf("%d x %dc\n",coins, coin_value[i]);
>             }
>         }
>     }
> }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#69204

Frombartekltg <bartekltg@gmail.com>
Date2015-09-03 22:29 +0200
Message-ID<msaajf$iof$1@node2.news.atman.pl>
In reply to#69197
On 03.09.2015 22:03, Ben Bacarisse wrote:
> bartekltg <bartekltg@gmail.com> writes:
> <snip>
>> I'm sure that these loops teacher mentioned was a loop over "face
>> value".
>
> Yes, that must surely be it.
>
>> #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){
>
> That's an odd number to test against.  For 32-bit unsigned ints things
> go wrong for values < 0 and >= pow(2, 32)/100.

Yes.
I was thinking about 0.00999. Probably:)



>
>>              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++)
>
> Most teachers would rather not have to explain that test too early on!
> Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
> test of coin_value[i] > 0.
> As a consequence, since I am wary about
> teaching for loop "mission creep", this would become a while loop:
>
>    int i = 0;
>    while (coin_value[i] > 0) {
>        ....
>        i += 1;
>    }


Or hardcode array length.
But your version is safer.
     const int N = 5;
     int coin_value[N]= {25, 10, 5,1};
do not generates a error or warning,
and the last entry is uninitialized.

bartekltg


[toc] | [prev] | [next] | [standalone]


#69214

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-03 21:51 +0100
Message-ID<87wpw7fcpv.fsf@bsb.me.uk>
In reply to#69204
bartekltg <bartekltg@gmail.com> writes:

> On 03.09.2015 22:03, Ben Bacarisse wrote:
>> bartekltg <bartekltg@gmail.com> writes:
>> <snip>
>>> I'm sure that these loops teacher mentioned was a loop over "face
>>> value".
>>
>> Yes, that must surely be it.
>>
>>> #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){
>>
>> That's an odd number to test against.  For 32-bit unsigned ints things
>> go wrong for values < 0 and >= pow(2, 32)/100.
>
> Yes.
> I was thinking about 0.00999. Probably:)

Ah, right.  Mind you, that's still odder than just dividing by the
multiplier you are going to use later (100), and the difference (2**32 x
0.00001) still over 42,949.  Not that that really matters here since
it's an exercise, but there is serious point to be made: it's good to
avoid things that make the reader think more than they need to.  I'd
have to ask myself why 0.00999?  Is is safe?  It is too conservative?
Does that matter?  And so on.

<snip>
> Or hardcode array length.
> But your version is safer.
>     const int N = 5;
>     int coin_value[N]= {25, 10, 5,1};
> do not generates a error or warning,
> and the last entry is uninitialized.

And that's a "variable length array" in C (it isn't in C++) and these
(much to my dismay) has been made optional in C11.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#69224

Frombartekltg <bartekltg@gmail.com>
Date2015-09-03 23:37 +0200
Message-ID<msaej7$msp$1@node2.news.atman.pl>
In reply to#69214
On 03.09.2015 22:51, Ben Bacarisse wrote:
> bartekltg <bartekltg@gmail.com> writes:
>
>> On 03.09.2015 22:03, Ben Bacarisse wrote:
>>> bartekltg <bartekltg@gmail.com> writes:
>>> <snip>
>>>> I'm sure that these loops teacher mentioned was a loop over "face
>>>> value".
>>>
>>> Yes, that must surely be it.
>>>
>>>> #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){
>>>
>>> That's an odd number to test against.  For 32-bit unsigned ints things
>>> go wrong for values < 0 and >= pow(2, 32)/100.
>>
>> Yes.
>> I was thinking about 0.00999. Probably:)
>
> Ah, right.  Mind you, that's still odder than just dividing by the
> multiplier you are going to use later (100), and the difference (2**32 x
> 0.00001) still over 42,949.  Not that that really matters here since

I just tried too hard to be safe ;-)


> it's an exercise, but there is serious point to be made: it's good to
> avoid things that make the reader think more than they need to.  I'd
> have to ask myself why 0.00999?  Is is safe?  It is too conservative?
> Does that matter?  And so on.

It looks like you think about this much more than I did.
I should comment this line.
// 2^32/100 * 0.99999  is the maximal number of quater
coins that can be given as change in retail stores.


> <snip>
>> Or hardcode array length.
>> But your version is safer.
>>      const int N = 5;
>>      int coin_value[N]= {25, 10, 5,1};
>> do not generates a error or warning,
>> and the last entry is uninitialized.
>
> And that's a "variable length array" in C (it isn't in C++) and these
> (much to my dismay) has been made optional in C11.


Are you sure this is the problem?
I compiled it in c++ with
-Wall -Wpedantic -Wextra -std=c++98
(so no variable length arrays) and it still work and do not generate
any warnings.
N is const.


bartekltg



[toc] | [prev] | [next] | [standalone]


#69227

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-03 23:07 +0100
Message-ID<87lhcnf97k.fsf@bsb.me.uk>
In reply to#69224
bartekltg <bartekltg@gmail.com> writes:

> On 03.09.2015 22:51, Ben Bacarisse wrote:
>> bartekltg <bartekltg@gmail.com> writes:
<snip>
> It looks like you think about this much more than I did.

!!

Not necessarily more, just differently.  As an ex-teacher, I see
exercises as micro-problems used to illustrate things that become
important in bigger systems.  It's fine to work up a solution and move
on, but I am used to trying to prepare model solutions to exercises that
will be scrutinised by dozens of enquiring minds, year after year.  It
gave me a mind set of studying every line for best practise.

It probably comes over as hyper-critical -- I'm sorry about that.  I
don't mean it to be negative, just as another perspective.

<snip>
>>> Or hardcode array length.
>>> But your version is safer.
>>>      const int N = 5;
>>>      int coin_value[N]= {25, 10, 5,1};
>>> do not generates a error or warning,
>>> and the last entry is uninitialized.
>>
>> And that's a "variable length array" in C (it isn't in C++) and these
>> (much to my dismay) has been made optional in C11.
>
> Are you sure this is the problem?

That VLAs are now optional?  I don't really know how much of a problem
it will be in practice, but it's an odd precedent.  I'm no longer sure
what C11 features I can reply on arriving in due course.  Maybe some of
them will be optional in C19 (or whatever).

> I compiled it in c++ with
> -Wall -Wpedantic -Wextra -std=c++98
> (so no variable length arrays) and it still work and do not generate
> any warnings.
> N is const.

Yes, C++ does not have VLAs.  This is an area where the same source code
has different meanings between C and C++.  C90 (or a C11) conforming
compiler could reject the code, but a C++ compiler must accept it.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#69230

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-09-03 18:24 -0400
Message-ID<msah7r$j11$1@dont-email.me>
In reply to#69224
On 09/03/2015 05:37 PM, bartekltg wrote:
> On 03.09.2015 22:51, Ben Bacarisse wrote:
>> bartekltg <bartekltg@gmail.com> writes:
...
>>> Or hardcode array length.
>>> But your version is safer.
>>>      const int N = 5;
>>>      int coin_value[N]= {25, 10, 5,1};
>>> do not generates a error or warning,
>>> and the last entry is uninitialized.
>>
>> And that's a "variable length array" in C (it isn't in C++) and these
>> (much to my dismay) has been made optional in C11.
> 
> 
> Are you sure this is the problem?
> I compiled it in c++ with
> -Wall -Wpedantic -Wextra -std=c++98
> (so no variable length arrays) and it still work and do not generate
> any warnings.
> N is const.


Until C99, the dimension of an array had to be an integer constant
expression (ICE), and that's still the rule in C++. In C99, if the
dimension is not an ICE, then the array is considered to be of variable
length.
However, in C++, the value of a const object initialized by constant
expression also qualifies as a constant expression. In C, it does not.
Therefore, coin_value[N] is a VLA according to C, and an ordinary array
in C++.

-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#69232

Frombartekltg <bartekltg@gmail.com>
Date2015-09-04 00:30 +0200
Message-ID<msahli$ja7$1@node1.news.atman.pl>
In reply to#69230
On 04.09.2015 00:24, James Kuyper wrote:
> On 09/03/2015 05:37 PM, bartekltg wrote:
>> On 03.09.2015 22:51, Ben Bacarisse wrote:
>>> bartekltg <bartekltg@gmail.com> writes:
> ...
>>>> Or hardcode array length.
>>>> But your version is safer.
>>>>       const int N = 5;
>>>>       int coin_value[N]= {25, 10, 5,1};
>>>> do not generates a error or warning,
>>>> and the last entry is uninitialized.
>>>
>>> And that's a "variable length array" in C (it isn't in C++) and these
>>> (much to my dismay) has been made optional in C11.
>>
>>
>> Are you sure this is the problem?
>> I compiled it in c++ with
>> -Wall -Wpedantic -Wextra -std=c++98
>> (so no variable length arrays) and it still work and do not generate
>> any warnings.
>> N is const.
>
>
> Until C99, the dimension of an array had to be an integer constant
> expression (ICE), and that's still the rule in C++. In C99, if the
> dimension is not an ICE, then the array is considered to be of variable
> length.
> However, in C++, the value of a const object initialized by constant
> expression also qualifies as a constant expression. In C, it does not.
> Therefore, coin_value[N] is a VLA according to C, and an ordinary array
> in C++.


Now I understand. Thanks!

bartekltg


[toc] | [prev] | [next] | [standalone]


#69240

FromKeith Thompson <kst-u@mib.org>
Date2015-09-03 16:36 -0700
Message-ID<lnk2s7qdm1.fsf@kst-u.example.com>
In reply to#69224
bartekltg <bartekltg@gmail.com> writes:
> On 03.09.2015 22:51, Ben Bacarisse wrote:
>> bartekltg <bartekltg@gmail.com> writes:
[...]
>>> Or hardcode array length.
>>> But your version is safer.
>>>      const int N = 5;
>>>      int coin_value[N]= {25, 10, 5,1};
>>> do not generates a error or warning,
>>> and the last entry is uninitialized.
>>
>> And that's a "variable length array" in C (it isn't in C++) and these
>> (much to my dismay) has been made optional in C11.
>
>
> Are you sure this is the problem?
> I compiled it in c++ with
> -Wall -Wpedantic -Wextra -std=c++98
> (so no variable length arrays) and it still work and do not generate
> any warnings.
> N is const.

In C, coin_value is a VLA, because the expression N is not a constant
expression.  That means that a C90 compiler will complain, a C99
compiler will not, and a C11 compiler will complain *if* it doesn't
support VLAs (which were made optional).

In C++, coin_value is *not* a VLA, because the expression N *is* a
constant expression.

You can avoid the problem either by writing

    int coin_value[] = {25, 10, 5, 1};

to let the compiler figure out the length, or by making N a constant
expression.  There are two good ways to do that, either

    #define N 5

or

    enum { N = 5 };

(the latter works only for constants of type int).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#69671

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-09-06 14:00 -0700
Message-ID<kfnsi6rqn4u.fsf@x-alumni2.alumni.caltech.edu>
In reply to#69240
Keith Thompson <kst-u@mib.org> writes:

> bartekltg <bartekltg@gmail.com> writes:
>> On 03.09.2015 22:51, Ben Bacarisse wrote:
>>> bartekltg <bartekltg@gmail.com> writes:
>
> [...]
>
>>>> Or hardcode array length.
>>>> But your version is safer.
>>>>      const int N = 5;
>>>>      int coin_value[N]= {25, 10, 5,1};
>>>> do not generates a error or warning,
>>>> and the last entry is uninitialized.
>>>
>>> And that's a "variable length array" in C (it isn't in C++) and these
>>> (much to my dismay) has been made optional in C11.
>>
>> Are you sure this is the problem?
>> I compiled it in c++ with
>> -Wall -Wpedantic -Wextra -std=c++98
>> (so no variable length arrays) and it still work and do not generate
>> any warnings.
>> N is const.
>
> In C, coin_value is a VLA, because the expression N is not a constant
> expression.  That means that a C90 compiler will complain, a C99
> compiler will not, and a C11 compiler will complain *if* it doesn't
> support VLAs (which were made optional).  [snip]

A C99 compiler will also complain, because VLA's may not
have initializers.

[toc] | [prev] | [next] | [standalone]


#69670

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-09-06 13:58 -0700
Message-ID<kfnwpw3qn7y.fsf@x-alumni2.alumni.caltech.edu>
In reply to#69204
bartekltg <bartekltg@gmail.com> writes:

> On 03.09.2015 22:03, Ben Bacarisse wrote:
>> bartekltg <bartekltg@gmail.com> writes:
>> <snip>
>>
>> Most teachers would rather not have to explain that test too early on!
>> Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
>> test of coin_value[i] > 0.
>> As a consequence, since I am wary about
>> teaching for loop "mission creep", this would become a while loop:
>>
>>    int i = 0;
>>    while (coin_value[i] > 0) {
>>        ....
>>        i += 1;
>>    }
>
> Or hardcode array length.
> But your version is safer.
>     const int N = 5;
>     int coin_value[N]= {25, 10, 5,1};
> do not generates a error or warning,
> and the last entry is uninitialized.

In C these two lines represent a constraint violation.  The
violations are different in C90 and C99 (C11 may be either of
the two), but it's always a violation.

[toc] | [prev] | [next] | [standalone]


#69730

Frombartekltg <bartekltg@gmail.com>
Date2015-09-07 05:07 +0200
Message-ID<msiv11$64k$1@node1.news.atman.pl>
In reply to#69670
On 06.09.2015 22:58, Tim Rentsch wrote:
> bartekltg <bartekltg@gmail.com> writes:
>
>> On 03.09.2015 22:03, Ben Bacarisse wrote:
>>> bartekltg <bartekltg@gmail.com> writes:
>>> <snip>
>>>
>>> Most teachers would rather not have to explain that test too early on!
>>> Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
>>> test of coin_value[i] > 0.
>>> As a consequence, since I am wary about
>>> teaching for loop "mission creep", this would become a while loop:
>>>
>>>     int i = 0;
>>>     while (coin_value[i] > 0) {
>>>         ....
>>>         i += 1;
>>>     }
>>
>> Or hardcode array length.
>> But your version is safer.
>>      const int N = 5;
>>      int coin_value[N]= {25, 10, 5,1};
>> do not generates a error or warning,
>> and the last entry is uninitialized.
>
> In C these two lines represent a constraint violation.  The
> violations are different in C90 and C99 (C11 may be either of
> the two), but it's always a violation.

Yes, I admit, I haven't the slightest idea that
const int N;
int T[N]...
is problematic in C.

:]
bartekltg

[toc] | [prev] | [next] | [standalone]


#69774

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-07 13:14 +0100
Message-ID<87a8sys9xu.fsf@bsb.me.uk>
In reply to#69730
bartekltg <bartekltg@gmail.com> writes:

> On 06.09.2015 22:58, Tim Rentsch wrote:
>> bartekltg <bartekltg@gmail.com> writes:
<snip>
>>>      const int N = 5;
>>>      int coin_value[N]= {25, 10, 5,1};
>>> do not generates a error or warning,
>>> and the last entry is uninitialized.
>>
>> In C these two lines represent a constraint violation.  The
>> violations are different in C90 and C99 (C11 may be either of
>> the two), but it's always a violation.
>
> Yes, I admit, I haven't the slightest idea that
> const int N;
> int T[N]...
> is problematic in C.

Exactly what the problem is depends on which version of C you are
considering.  Let me try to clarify it:

In C90 const int N = 5; int T[N]; is a constraint violation because N is
not a constant integer expression.

In C99 const int N = 5; int T[N]; is fine and T is a VLA.  However, VLAs
can't have an initialiser so the = {...}; part is a constraint
violation.

In C11 VLAs are optional, but an implementation with them must report
the initialiser, and one without them must report the fact the N is not
a constant integer expression.

(In C++, const int N = 5; int T[N]; N is a constant expression and the
array is a normal automatic array and an have an initialiser like any
other.)

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#69881

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-09-08 02:39 -0700
Message-ID<kfny4ghl06j.fsf@x-alumni2.alumni.caltech.edu>
In reply to#69730
bartekltg <bartekltg@gmail.com> writes:

> On 06.09.2015 22:58, Tim Rentsch wrote:
>> bartekltg <bartekltg@gmail.com> writes:
>>
>>> On 03.09.2015 22:03, Ben Bacarisse wrote:
>>>> bartekltg <bartekltg@gmail.com> writes:
>>>> <snip>
>>>>
>>>> Most teachers would rather not have to explain that test too early on!
>>>> Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
>>>> test of coin_value[i] > 0.
>>>> As a consequence, since I am wary about
>>>> teaching for loop "mission creep", this would become a while loop:
>>>>
>>>>     int i = 0;
>>>>     while (coin_value[i] > 0) {
>>>>         ....
>>>>         i += 1;
>>>>     }
>>>
>>> Or hardcode array length.
>>> But your version is safer.
>>>      const int N = 5;
>>>      int coin_value[N]= {25, 10, 5,1};
>>> do not generates a error or warning,
>>> and the last entry is uninitialized.
>>
>> In C these two lines represent a constraint violation.  The
>> violations are different in C90 and C99 (C11 may be either of
>> the two), but it's always a violation.
>
> Yes, I admit, I haven't the slightest idea that
> const int N;
> int T[N]...
> is problematic in C.

Sorry, based on your comments in other postings I got
the impression you were more of an expert than you are
apparently.  I tend to give somewhat terse replies to
people I think are experts, saving longer explanations
for people who look like they need more help.  Needless
to say sometimes I guess wrong and this seems to be
one of those times.  Hopefully it all got straightened
out.

If you don't have a copy of the C standard, let me
suggest that you get one, especially since you seem
more than capable of digesting it -

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

This is an unofficial (and later) version of C99, which
IMO is the best starting point.  There is also an
unofficial draft of the C11 standard available, which
obviously is more recent, but it is significantly
longer and has a fair amount of stuff (eg, about
threading) which is not important on a first reading.

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web