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


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

Initializing sum to zero wouldn't compile

Started byAlla _ <modelling.data@gmail.com>
First post2015-08-21 04:39 -0700
Last post2015-08-30 02:14 -0700
Articles 7 on this page of 47 — 12 participants

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


Contents

  Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 04:39 -0700
    Re: Initializing sum to zero wouldn't compile James Kuyper <jameskuyper@verizon.net> - 2015-08-21 07:54 -0400
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:27 -0700
        Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-21 10:28 -0700
          Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:15 -0700
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:30 -0700
    Re: Initializing sum to zero wouldn't compile Ike Naar <ike@iceland.freeshell.org> - 2015-08-21 12:40 +0000
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:16 -0700
    Re: Initializing sum to zero wouldn't compile Siri Cruz <chine.bleu@yahoo.com> - 2015-08-21 07:41 -0700
      Re: Initializing sum to zero wouldn't compile Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-08-21 10:56 -0400
      Re: Initializing sum to zero wouldn't compile Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-21 09:28 -0600
    Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 10:02 -0700
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:21 -0700
        Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 22:27 -0700
          Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 02:25 -0700
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 03:50 -0700
      Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 13:18 +0100
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:21 -0700
          Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 21:55 +0100
            Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:36 -0700
              Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 11:24 +0100
          Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 17:17 -0700
            Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:26 -0700
              Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-26 02:27 -0700
                Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 04:21 -0700
                  Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 12:37 +0100
                  Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 13:06 +0100
                    Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-26 13:43 +0100
                      Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 14:03 +0100
                      Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 14:06 +0100
                        Re: Initializing sum to zero wouldn't compile Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 15:14 -0700
                    Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:38 -0700
                  Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:44 -0700
      Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 10:49 -0700
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:16 -0700
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-29 07:19 -0700
      Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 15:35 +0100
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:11 -0700
          Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 12:28 +0100
            Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 15:02 -0700
              Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 22:36 +0100
                Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 16:10 -0700
                  Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-31 00:14 +0100
                    Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 19:56 -0700
      Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 16:57 +0100
        Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 17:37 +0100
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:14 -0700

Page 3 of 3 — ← Prev page 1 2 [3]


#68726

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-08-30 22:36 +0100
Message-ID<877focwja7.fsf@bsb.me.uk>
In reply to#68725
Keith Thompson <kst-u@mib.org> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> Alla _ <modelling.data@gmail.com> writes:
> [...]
>>> As to arrays, I am using an array because this 
>>> exercise is part of the chapter on arrays.
>>
>> Ah.  Then I suspect you are doing it wrong.  Using an array in a
>> pointless way is unlikely to be what the exercise wants.
>
> I disagree.  I think the exercise (to compute the average of three
> floating-point values using an array) has two parts: to read values
> into an array, and to compute the average of the values in an array.
> Both are sensible things to do.  The fact that the combination
> is more easily done without using an array would be relevant in a
> real-world program, but not in an introductory exercise.

It looks like we agree but you say that you disagree so I must have made
my point badly.  Assigning to an array element which is then ignored is
"doing it wrong" and "unlikely to be what the exercise wants".  What you
describe is what the exercise probably wants.

-- 
Ben.

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


#68728

FromKeith Thompson <kst-u@mib.org>
Date2015-08-30 16:10 -0700
Message-ID<lnlhcstltc.fsf@kst-u.example.com>
In reply to#68726
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>> Alla _ <modelling.data@gmail.com> writes:
>> [...]
>>>> As to arrays, I am using an array because this 
>>>> exercise is part of the chapter on arrays.
>>>
>>> Ah.  Then I suspect you are doing it wrong.  Using an array in a
>>> pointless way is unlikely to be what the exercise wants.
>>
>> I disagree.  I think the exercise (to compute the average of three
>> floating-point values using an array) has two parts: to read values
>> into an array, and to compute the average of the values in an array.
>> Both are sensible things to do.  The fact that the combination
>> is more easily done without using an array would be relevant in a
>> real-world program, but not in an introductory exercise.
>
> It looks like we agree but you say that you disagree so I must have made
> my point badly.  Assigning to an array element which is then ignored is
> "doing it wrong" and "unlikely to be what the exercise wants".  What you
> describe is what the exercise probably wants.

You're right, we agree.

-- 
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]


#68731

FromBartc <bc@freeuk.com>
Date2015-08-31 00:14 +0100
Message-ID<ms02la$93t$1@dont-email.me>
In reply to#68728
On 31/08/2015 00:10, Keith Thompson wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> Keith Thompson <kst-u@mib.org> writes:
>>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>>> Alla _ <modelling.data@gmail.com> writes:
>>> [...]
>>>>> As to arrays, I am using an array because this
>>>>> exercise is part of the chapter on arrays.
>>>>
>>>> Ah.  Then I suspect you are doing it wrong.  Using an array in a
>>>> pointless way is unlikely to be what the exercise wants.
>>>
>>> I disagree.  I think the exercise (to compute the average of three
>>> floating-point values using an array) has two parts: to read values
>>> into an array, and to compute the average of the values in an array.
>>> Both are sensible things to do.  The fact that the combination
>>> is more easily done without using an array would be relevant in a
>>> real-world program, but not in an introductory exercise.
>>
>> It looks like we agree but you say that you disagree so I must have made
>> my point badly.  Assigning to an array element which is then ignored is
>> "doing it wrong" and "unlikely to be what the exercise wants".  What you
>> describe is what the exercise probably wants.
>
> You're right, we agree.

(It's odd but on my Thunderbird newsreader, your posts seem to be an 
hour ahead of everyone else's. So I will first read someone's reply to 
your post, /then/ read your original post as it has a later time-stamp!

This happens when using un-threaded view and sorting chronologically. 
But the time-stamps are still out of order in threaded view.)

-- 
Bartc

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


#68736

FromKeith Thompson <kst-u@mib.org>
Date2015-08-30 19:56 -0700
Message-ID<lnh9ngtbbr.fsf@kst-u.example.com>
In reply to#68731
Bartc <bc@freeuk.com> writes:
[...]
> (It's odd but on my Thunderbird newsreader, your posts seem to be an 
> hour ahead of everyone else's. So I will first read someone's reply to 
> your post, /then/ read your original post as it has a later time-stamp!
>
> This happens when using un-threaded view and sorting chronologically. 
> But the time-stamps are still out of order in threaded view.)

The clock on the system I'm posting from is about 35 minutes fast.
I'll talk to the owner.

-- 
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]


#68638

FromBartc <bc@freeuk.com>
Date2015-08-29 16:57 +0100
Message-ID<mrskm0$8n3$1@dont-email.me>
In reply to#68630
On 29/08/2015 15:19, Alla _ wrote:
> Thank you Everyone! I have again learned a lot from you.
> Still working on this 'simple' program. Here is an improved
> version, which still has an important bug:
>
> // a program that calculates the average of an array of 3 floating-point values; with for loop
>
> #include <stdio.h>
>
> int main (void)
> {
>      float values[3];
>      float element;
>      float sum = 0;
>      int i;
>
>      printf ("Please, enter 3 floating values:  \n");
>
>      for ( i = 0; i < 3; i++)
>      {
>          if (scanf ("%f", &element) == 1)
>          {
>              if (element <= 0)
>                  printf ("Please, enter positive values\n");
>                  i--;  /* if I don't decrement i, then the program
>                        will go on 'counting' wrong values and
>                        produce erroneous result; but both i--; and
>                        i -= i do not work; I get an error message (see
>                        below)

I revised your code so that it compiles (and also so it was more 
consistent):


#include <stdio.h>

int main (void)
{
     float values[3];
     float element;
     float sum = 0;
     int i;

     printf ("Please, enter 3 floating values:  \n");

     for ( i = 0; i < 3; i++) {
     	if (scanf ("%f", &element) == 1) {
     	    if (element <= 0) {
     	    	printf ("Please, enter positive values\n");
     	    	i--;
     	    } else {
                 printf("Read %f\n",element);
     	    	values[i] = element;
     	    	sum += values[i];
     	    }
     	} else {
     	    printf ("You entered special characters\n");
     	    return;
     	}
     }
     printf ("The average of 3 values is %.2f\n", sum / 3);

     return 0;
}


When a negative value has been entered, it's not clear what you 
intentions are; are uses supposed to re-enter the number?

That won't work if all the input is on one line, as in:

  10 -20 30

As all numbers will already be read in; it will proceed to read 30 then 
one more number.  (The extra printf will show what is being processed.) 
It's easier to if numbers are entered one per line; you will have to 
specify this.

You might also consider first reading all the numbers into the array, 
then working out the average of the numbers in the array.

-- 
Bartc

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


#68641

FromBartc <bc@freeuk.com>
Date2015-08-29 17:37 +0100
Message-ID<mrsn1p$huc$1@dont-email.me>
In reply to#68638
On 29/08/2015 17:07, Stefan Ram wrote:
> Bartc <bc@freeuk.com> writes:
>> printf ("You entered special characters\n");
>> return;
>
>    »'return' with no value, in function returning non-void«

gcc doesn't trap that in default warning mode.

> Please, enter 3 floating values:
> 9999999933815812510711506376257961984.000000
> Read 9999999933815812510711506376257961984.000000
> 9999999933815812510711506376257961984.000000
> Read 9999999933815812510711506376257961984.000000
> 9999999933815812510711506376257961984.000000
> Read 9999999933815812510711506376257961984.000000
> The average of 3 values is 10000000567641112624826207124609564672.00

The program uses float calculations; try double (with scan %lf), or 
printing values using "%.6e".

-- 
Bartc

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


#68679

FromAlla _ <modelling.data@gmail.com>
Date2015-08-30 02:14 -0700
Message-ID<35c27f8e-afe0-4016-aef8-3d0411db815a@googlegroups.com>
In reply to#68638
On Saturday, August 29, 2015 at 7:57:44 PM UTC+4, Bart wrote:
> On 29/08/2015 15:19, Alla _ wrote:
> > Thank you Everyone! I have again learned a lot from you.
> > Still working on this 'simple' program. Here is an improved
> > version, which still has an important bug:
> >
> > // a program that calculates the average of an array of 3 floating-point values; with for loop
> >
> > #include <stdio.h>
> >
> > int main (void)
> > {
> >      float values[3];
> >      float element;
> >      float sum = 0;
> >      int i;
> >
> >      printf ("Please, enter 3 floating values:  \n");
> >
> >      for ( i = 0; i < 3; i++)
> >      {
> >          if (scanf ("%f", &element) == 1)
> >          {
> >              if (element <= 0)
> >                  printf ("Please, enter positive values\n");
> >                  i--;  /* if I don't decrement i, then the program
> >                        will go on 'counting' wrong values and
> >                        produce erroneous result; but both i--; and
> >                        i -= i do not work; I get an error message (see
> >                        below)
> 
> I revised your code so that it compiles (and also so it was more 
> consistent):
> 
> 
> #include <stdio.h>
> 
> int main (void)
> {
>      float values[3];
>      float element;
>      float sum = 0;
>      int i;
> 
>      printf ("Please, enter 3 floating values:  \n");
> 
>      for ( i = 0; i < 3; i++) {
>      	if (scanf ("%f", &element) == 1) {
>      	    if (element <= 0) {
>      	    	printf ("Please, enter positive values\n");
>      	    	i--;
>      	    } else {
>                  printf("Read %f\n",element);
>      	    	values[i] = element;
>      	    	sum += values[i];
>      	    }
>      	} else {
>      	    printf ("You entered special characters\n");
>      	    return;
>      	}
>      }
>      printf ("The average of 3 values is %.2f\n", sum / 3);
> 
>      return 0;
> }
> 
> 
> When a negative value has been entered, it's not clear what you 
> intentions are; are uses supposed to re-enter the number?
> 
> That won't work if all the input is on one line, as in:
> 
>   10 -20 30
> 
> As all numbers will already be read in; it will proceed to read 30 then 
> one more number.  (The extra printf will show what is being processed.) 
> It's easier to if numbers are entered one per line; you will have to 
> specify this.
> 
> You might also consider first reading all the numbers into the array, 
> then working out the average of the numbers in the array.
> 
> -- 
> Bartc

Thank you very much! 

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

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


csiph-web