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 06:48 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jdcbai$7uf$1@dont-email.me> (permalink) |
| References | <4ef99d34$0$1379$4fafbaef@reader2.news.tin.it> |
On 12/27/2011 05:25 AM, superpollo wrote:
> http://rosettacode.org/wiki/Averages/Root_mean_square#C
>
>
> ...
> [~/superpollo]$ cat rms_rc.c
> #include <stdio.h>
> #include <math.h>
>
> double rms(double *v, int n)
> {
> int i;
> double sum = 0.0;
> for (i = 0; i < n; i++)
> sum += v[i] * v[i];
> return sqrt(sum / n);
> }
>
> int main()
> {
> double v[] = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
> printf("%f\n", rms(v, sizeof(v) / sizeof(double)));
>
> return 0;
> }
> [~/superpollo]$ cc -lm rms_rc.c && ./a.out
> 6.204837
> ...
>
> that works fine, but i am quite unhappy with having to say to the rms
> function how big the array is... after all, i guess, shouldn't the
> function be able to figure it out herself?
How? The method you use below doesn't work. The only alternative I can
think of to passing in the length of the array is to put a sentinel
value at the end of the array like the null character at the end of a
string. For floating point values, nan() is a reasonable value to use,
in which case you can test for the end of the array using isnan(v[i]).
> ... to i tried:
>
> ...
> [~/superpollo]$ cat rms.c
> #include <stdio.h>
> #include <math.h>
>
> double rms(double *v)
> {
> const int n = sizeof(v) / sizeof(double);
v is pointer to double. sizeof(v) is therefore the same as
sizeof(double*). 'n' is therefore a fixed value:
sizeof(double*)/sizeof(double). It's value has nothing to do with how
long the array whose first member 'v' points at. On many systems, it
will have a value of 0.
> int i;
> double sum = 0.0;
> for (i = 0; i < n; i++)
> sum += v[i] * v[i];
> return sqrt(sum / n);
If 'n' does have a value of zero, that division means that your program
has undefined behavior. Whether or not 'n' is 0 can be determined at
compile time, so a sufficiently smart compiler could even refuse to
compile your code.
--
James Kuyper
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