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


Groups > comp.lang.c > #4938

Re: error in code

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: error in code
Date 2011-05-28 15:33 -0500
Organization A noiseless patient Spider
Message-ID <irrm6t$17v$1@dont-email.me> (permalink)
References <4de14d0c$0$1943$bbae4d71@news.suddenlink.net> <irrjl0$iuv$1@nnrp.ngi.it> <4de15747$0$1954$bbae4d71@news.suddenlink.net>

Show all headers | View raw


On 5/28/2011 3:12 PM, Bill Cunningham wrote:
> pozz wrote:
>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>> void mean(double *avg, int periods)
>>> {
>>>       double val;
>>>       val = *avg / periods;
>>>       printf("%.2f\n", val);
>>> }
>>
>> avg is a pointer to double. *avg is a double. If you write
>>
>>    *avg / periods
>>
>> your dividing one double value by periods.
>>
>> In your case, avg points to an array of double values. The number of
>> elements in the array is periods. So you have to write a cycle to sum
>> all the periods elements together. At last, you can divide by periods.
>
> That's what I don't know how to do. Write a cycle. It's these little caveats
> in C that confuse me most. Functions I am grasping. Writing cycles not so
> much.
>

"A cycle" might also be called "a loop".

   double mean(double * values, int periods) {
       double accumulator;
       int i;
       double average;

       accumulator = 0;
       for (i = 0; i < periods; ++i)
         accumulator += values[i];
       average = accumulator / periods;
       return average;
     }

>>> int main(void)
>>> {
>>>       double a[] = 2.2, 4.2, 5.00;
>>                      ^
>>
>> You are initializing an array. In C you can do that with curly
>> backets {}:
>>    double a[] = { 2.2, 4.2, 5.00 };
>>
>>>       mean(a, 3);
>>                 ^
>>
>> In this case you are passing the number of elements of a[] array. I
>> suggest to use a syntax like the following:
>>
>>    mean(a, sizeof(a) / sizeof(a[0]));
>
> What's the sizeof(a[0]) mean?

Given:

   double a[] = { 2.2, 4.2, 5.00 };

'sizeof a' yields the size of the 'a' array, in bytes.  'sizeof a[0]' 
(or 'sizeof *a') gives the size of a single element of the 'a' array, in 
bytes.  Since a single element is a 'double' in this case, 'sizeof a[0]' 
gives the same value as 'sizeof (double)'.

If you divide the size of the array by the size of one of its elements, 
you get the number of elements in the array.

>
>> or define a generic macro if you're going to use this syntax in
>> several points:
>>
>>    #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))
>>

Thus, the above macro gives the number of elements in an array.  Same 
idea as:

   #define COUNTOF(array) (sizeof (array) / sizeof *(array))

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


Thread

error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:28 -0400
  Re: error in code pozz <pozzugno@gmail.com> - 2011-05-28 21:49 +0200
    Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 16:12 -0400
      Re: error in code Shao Miller <sha0.miller@gmail.com> - 2011-05-28 15:33 -0500
        Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 17:12 -0400
          Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 14:26 -0700
          Re: error in code Shao Miller <sha0.miller@gmail.com> - 2011-05-28 16:29 -0500
          Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-28 17:26 -0600
      Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 14:20 -0700
        Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-29 02:16 -0400
      Re: error in code Lew Pitcher <lpitcher@teksavvy.com> - 2011-05-28 16:35 -0400
        Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-29 15:50 -0400
        Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-29 20:39 -0600
  Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 13:46 -0700
  Re: error in code Nobody <nobody@nowhere.com> - 2011-05-28 23:06 +0100
    Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:10 +1200
  Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:09 +1200
  Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:28 +0000
    Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:37 +1200
      Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:45 +0000
        Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:48 +1200
          Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:56 +0000
            Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-29 00:38 +0000
        Re: error in code "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-28 23:11 -0400
  Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-28 15:54 -0700
  Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-28 17:23 -0600
  Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-29 19:30 +0200
    Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 18:14 -0700
      Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-29 18:50 -0700
        Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 19:10 -0700
          Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-30 02:54 +0000
            Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 20:35 -0700
              Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-30 06:15 +0000
      Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-30 08:46 +0200
        Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-30 18:50 -0700
          Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-31 10:49 -0700
    Re: error in code Peter Nilsson <airia@acay.com.au> - 2011-05-29 21:04 -0700
      Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-30 08:44 +0200
      Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-31 01:35 -0700
        Re: error in code pacman@kosh.dhis.org (Alan Curry) - 2011-05-31 23:36 +0000
          Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-01 03:01 -0700
            Re: error in code Keith Thompson <kst-u@mib.org> - 2011-06-01 08:08 -0700
              Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-01 12:50 -0700
                Re: error in code Keith Thompson <kst-u@mib.org> - 2011-06-01 13:26 -0700
                Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-02 18:10 -0700
                Re: error in code jacob navia <jacob@spamsink.net> - 2011-06-03 11:55 +0200
                Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-03 19:37 -0700
                Re: error in code jacob navia <jacob@spamsink.net> - 2011-06-04 08:13 +0200
      Re: error in code Nisse Engström <news.NOSPAM.idf0Z@luden.se> - 2011-06-01 09:02 +0200

csiph-web