Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: rms from rosettacode |
| Date | 2011-12-27 14:11 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4EFA1859.2060808@verizon.net> (permalink) |
| References | <4ef99d34$0$1379$4fafbaef@reader2.news.tin.it> <jdd1v3$7ka$1@dont-email.me> <4efa0f91$0$1375$4fafbaef@reader2.news.tin.it> |
On 12/27/2011 01:33 PM, superpollo wrote:
> BartC ha scritto:
>> "superpollo" <superpollo@tznvy.pbz> wrote in message
>> news:4ef99d34$0$1379$4fafbaef@reader2.news.tin.it...
>>> http://rosettacode.org/wiki/Averages/Root_mean_square#C
>>
>>> #include <stdio.h>
>>> #include <math.h>
>>>
>>> double rms(double *v)
>>> {
>>> const int n = sizeof(v) / sizeof(double);
>>> int i;
>>> double sum = 0.0;
>>> for (i = 0; i < n; i++)
>>> sum += v[i] * v[i];
>>> return sqrt(sum / n);
>>> }
>>
>>> [~/superpollo]$ cc -lm rms.c && ./a.out
>>> nan
>>
>>> what gives?
>>
>> Try printing the value of n. You'll probably find it's zero. Your
>> for-loop while keep going for quite a while, since it starts at i=0, and
>> won't stop until i<0, perhaps two billion iterations later, by which
>> time you will have long since breached the array bounds.
>>
>> (BTW size(v) is just the number of bytes in a pointer. The array size
>> must be made known explicity. Or you can use rand() to just guess the
>> size; it will probably get it right more often than your code!)
>
> so, to summarize: it is not possible for a function that gets an array
> passed to figure by herself the size, right? so the *idiomatic* way to
> give that info to the funtion should be that of the original rosettacode
> code, or is there another way?
There's an wide variety of different ways, but all of the ones I can
think of boil down to passing to the function either a count of the
number of elements in the array, or putting a sentinel value in the
array. The wide variety comes in mainly from the many different ways you
can pass in the count, but also from freedom you have in choosing
sentinel values. Since the count and the array are closely related, I
like to package them together in a struct, making use of the flexible
array member feature introduced in C99:
struct int_vector
{
size_t n;
int array[];
}
double sum(struct int_vector* v)
{
double sum = 0.0;
for(int i = 0; i < v->n; i++
sum += v.array[i];
return sum;
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 11:25 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 06:48 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:01 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 07:10 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:36 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 10:04 -0500
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:42 -0800
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:46 -0500
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 09:49 -0800
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 18:14 +0000
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 19:33 +0100
Re: rms from rosettacode jt@toerring.de (Jens Thoms Toerring) - 2011-12-27 19:04 +0000
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:45 -0800
Re: rms from rosettacode blp@cs.stanford.edu (Ben Pfaff) - 2011-12-27 14:57 -0800
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 19:06 +0000
Re: rms from rosettacode Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-27 16:16 -0800
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 14:11 -0500
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
Re: rms from rosettacode Johann Klammer <klammerr@NOSPAM.aon.at> - 2011-12-28 00:23 +0100
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 15:54 -0800
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 22:53 -0800
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-28 06:29 +0000
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-28 02:45 -0800
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-27 19:53 +0000
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 21:31 +0100
Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-27 20:49 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:42 +0100
Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-29 08:18 -0800
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:54 +0100
Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:12 +0200
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:38 +0100
Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:26 -0800
Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-28 21:35 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:10 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-28 08:03 -0500
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-28 13:32 +0000
Re: rms from rosettacode Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-28 13:41 +0000
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:29 +0000
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-28 13:47 -0800
Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:19 +0200
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:11 +0000
csiph-web