Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #67931 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-08-21 04:39 -0700 |
| Last post | 2015-08-30 02:14 -0700 |
| Articles | 20 on this page of 47 — 12 participants |
Back to article view | Back to comp.lang.c
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 1 of 3 [1] 2 3 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 04:39 -0700 |
| Subject | Initializing sum to zero wouldn't compile |
| Message-ID | <5f70d0ca-8b6b-4c11-a2d3-10a5fb65c1c3@googlegroups.com> |
Hello!
Here is a simple problem I created solving
one of the exercises from Programming in C.
The program works, I hope (I tested a few
times). My question is why if I initialize
sum to zero in the main part, the program wouldn't
compile? Is it because the sum shouldn't have any
initial values in case I assign a formula to it
later in the program?
The version that works:
// a program that calculates the average of an array of
3 floating-point values
#include <stdio.h>
int main (void)
{
float values[3];
float element, average, sum;
int i;
printf ("Please, enter 3 floating values: ");
for (i = 1; i <= 3; i++)
{
scanf ("%f", &element);
values[i] = element;
sum += values[i];
}
printf ("The average of 3 values is %.2f\n", average = sum / 3);
return 0;
}
The version that doesn't:
// a program that calculates the average of an array
of 3 floating-point values
#include <stdio.h>
int main (void)
{
float values[3];
float element, average;
float sum = 0; // this causes the problem
int i;
printf ("Please, enter 3 floating values: ");
for (i = 1; i <= 3; i++)
{
scanf ("%f", &element);
values[i] = element;
sum += values[i];
}
printf ("The average of 3 values is %.2f\n", average = sum / 3);
return 0;
}
Thank you!
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-08-21 07:54 -0400 |
| Message-ID | <mr73f2$ka$1@dont-email.me> |
| In reply to | #67931 |
On 08/21/2015 07:39 AM, Alla _ wrote:
> Hello!
>
> Here is a simple problem I created solving
> one of the exercises from Programming in C.
>
> The program works, I hope (I tested a few
> times). My question is why if I initialize
> sum to zero in the main part, the program wouldn't
> compile? Is it because the sum shouldn't have any
> initial values in case I assign a formula to it
> later in the program?
...
> The version that doesn't:
> // a program that calculates the average of an array
> of 3 floating-point values
>
> #include <stdio.h>
>
> int main (void)
> {
> float values[3];
> float element, average;
> float sum = 0; // this causes the problem
> int i;
>
> printf ("Please, enter 3 floating values: ");
>
> for (i = 1; i <= 3; i++)
> {
> scanf ("%f", &element);
>
> values[i] = element;
> sum += values[i];
> }
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> return 0;
> }
That program compiles without any error or warning messages when I use
gcc 4.8.4 and the following command line options:
-std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align
-Wstrict-prototypes -Wmissing-prototypes
Which version of which compiler are you using? What options are you
setting for it? What are the messages it produces to indicate that
there's a problem?
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 05:27 -0700 |
| Message-ID | <ec44201f-1580-4625-91f3-683a937e3d34@googlegroups.com> |
| In reply to | #67932 |
On Friday, August 21, 2015 at 3:55:03 PM UTC+4, James Kuyper wrote:
> On 08/21/2015 07:39 AM, Alla _ wrote:
> > Hello!
> >
> > Here is a simple problem I created solving
> > one of the exercises from Programming in C.
> >
> > The program works, I hope (I tested a few
> > times). My question is why if I initialize
> > sum to zero in the main part, the program wouldn't
> > compile? Is it because the sum shouldn't have any
> > initial values in case I assign a formula to it
> > later in the program?
> ...
> > The version that doesn't:
> > // a program that calculates the average of an array
> > of 3 floating-point values
> >
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average;
> > float sum = 0; // this causes the problem
> > int i;
> >
> > printf ("Please, enter 3 floating values: ");
> >
> > for (i = 1; i <= 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > values[i] = element;
> > sum += values[i];
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> > }
>
> That program compiles without any error or warning messages when I use
> gcc 4.8.4 and the following command line options:
>
> -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align
> -Wstrict-prototypes -Wmissing-prototypes
>
> Which version of which compiler are you using? What options are you
> setting for it? What are the messages it produces to indicate that
> there's a problem?
> --
> James Kuyper
It's a mystery. Before posting here I have run the program
a few times, all with the same error
ld: can't link with a main executable for architecture x86_64
> collect2: ld returned 1 exit status
I have tried now one more time, and it did return the
same message; but after one more time, it did compile:
// a program that calculates the average of an array of 3 floating-point values
#include <stdio.h>
int main (void)
{
float values[3];
float element, average, sum = 0;
int i;
printf ("Please, enter 3 floating values: ");
for (i = 1; i <= 3; i++)
{
scanf ("%f", &element);
values[i] = element;
sum += values[i];
}
printf ("The average of 3 values is %.2f\n", average = sum / 3);
return 0;
}
it's a mystery. I had no problems when there was no zero
assigned to sum.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-08-21 10:28 -0700 |
| Message-ID | <lnfv3c1rg5.fsf@kst-u.example.com> |
| In reply to | #67933 |
Alla _ <modelling.data@gmail.com> writes:
> On Friday, August 21, 2015 at 3:55:03 PM UTC+4, James Kuyper wrote:
>> On 08/21/2015 07:39 AM, Alla _ wrote:
[...]
>> > The version that doesn't:
>> > // a program that calculates the average of an array
>> > of 3 floating-point values
>> >
[code snipped]
>>
>> That program compiles without any error or warning messages when I use
>> gcc 4.8.4 and the following command line options:
>>
>> -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align
>> -Wstrict-prototypes -Wmissing-prototypes
>>
>> Which version of which compiler are you using? What options are you
>> setting for it? What are the messages it produces to indicate that
>> there's a problem?
>
> It's a mystery. Before posting here I have run the program
> a few times, all with the same error
> ld: can't link with a main executable for architecture x86_64
>> collect2: ld returned 1 exit status
If you're asking about a compile-time (or, in this case, link-time)
error, please include the exact error message in your post.
If I had seen the avove message I could have told you that it very
probably had nothing to do with your source code. If there's an error
in your code (say, a syntax error or an undeclared identifier), the
compiler will usually print a message referring to the line on which the
error occurs. This looks more like some kind of configuration error,
having nothing to do with the initialization of sum.
You could as easily have gotten the same error if you had compiled the
original program, or a simple "hello, world" program. I don't know what
would have caused it. Now that you're no longer seeing the error, it's
probably not worth pursuing.
--
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]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 11:15 -0700 |
| Message-ID | <90032d80-cd43-4e9c-99f3-c6786c690ac5@googlegroups.com> |
| In reply to | #67952 |
On Friday, August 21, 2015 at 8:23:05 PM UTC+4, Keith Thompson wrote: > Alla _ <modelling.data@gmail.com> writes: > > On Friday, August 21, 2015 at 3:55:03 PM UTC+4, James Kuyper wrote: > >> On 08/21/2015 07:39 AM, Alla _ wrote: > [...] > >> > The version that doesn't: > >> > // a program that calculates the average of an array > >> > of 3 floating-point values > >> > > [code snipped] > >> > >> That program compiles without any error or warning messages when I use > >> gcc 4.8.4 and the following command line options: > >> > >> -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align > >> -Wstrict-prototypes -Wmissing-prototypes > >> > >> Which version of which compiler are you using? What options are you > >> setting for it? What are the messages it produces to indicate that > >> there's a problem? > > > > It's a mystery. Before posting here I have run the program > > a few times, all with the same error > > ld: can't link with a main executable for architecture x86_64 > >> collect2: ld returned 1 exit status > > If you're asking about a compile-time (or, in this case, link-time) > error, please include the exact error message in your post. > > If I had seen the avove message I could have told you that it very > probably had nothing to do with your source code. If there's an error > in your code (say, a syntax error or an undeclared identifier), the > compiler will usually print a message referring to the line on which the > error occurs. This looks more like some kind of configuration error, > having nothing to do with the initialization of sum. > > You could as easily have gotten the same error if you had compiled the > original program, or a simple "hello, world" program. I don't know what > would have caused it. Now that you're no longer seeing the error, it's > probably not worth pursuing. > > -- > 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" Thank you! Sorry, I should have posted the error message in my fist post. Thank you once again.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 05:30 -0700 |
| Message-ID | <cb05a46a-ee9b-469b-98a2-9b6550aec9e9@googlegroups.com> |
| In reply to | #67932 |
On Friday, August 21, 2015 at 3:55:03 PM UTC+4, James Kuyper wrote:
> On 08/21/2015 07:39 AM, Alla _ wrote:
> > Hello!
> >
> > Here is a simple problem I created solving
> > one of the exercises from Programming in C.
> >
> > The program works, I hope (I tested a few
> > times). My question is why if I initialize
> > sum to zero in the main part, the program wouldn't
> > compile? Is it because the sum shouldn't have any
> > initial values in case I assign a formula to it
> > later in the program?
> ...
> > The version that doesn't:
> > // a program that calculates the average of an array
> > of 3 floating-point values
> >
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average;
> > float sum = 0; // this causes the problem
> > int i;
> >
> > printf ("Please, enter 3 floating values: ");
> >
> > for (i = 1; i <= 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > values[i] = element;
> > sum += values[i];
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> > }
>
> That program compiles without any error or warning messages when I use
> gcc 4.8.4 and the following command line options:
>
> -std=c99 -pedantic -Wall -Wpointer-arith -Wcast-align
> -Wstrict-prototypes -Wmissing-prototypes
>
> Which version of which compiler are you using? What options are you
> setting for it? What are the messages it produces to indicate that
> there's a problem?
> --
> James Kuyper
gcc 4.2.1, Mac OS X 10.7.5
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2015-08-21 12:40 +0000 |
| Message-ID | <slrn3vfsmte74l.ck0.ike@iceland.freeshell.org> |
| In reply to | #67931 |
On 2015-08-21, Alla _ <modelling.data@gmail.com> wrote:
> Hello!
>
> Here is a simple problem I created solving
> one of the exercises from Programming in C.
>
> The program works, I hope (I tested a few
> times). My question is why if I initialize
> sum to zero in the main part, the program wouldn't
> compile? Is it because the sum shouldn't have any
> initial values in case I assign a formula to it
> later in the program?
>
> The version that works:
> // a program that calculates the average of an array of
> 3 floating-point values
>
> #include <stdio.h>
>
> int main (void)
> {
> float values[3];
> float element, average, sum;
> int i;
>
> printf ("Please, enter 3 floating values: ");
>
> for (i = 1; i <= 3; i++)
> {
> scanf ("%f", &element);
>
> values[i] = element;
> sum += values[i];
> }
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> return 0;
> }
>
> The version that doesn't:
> // a program that calculates the average of an array
> of 3 floating-point values
>
> #include <stdio.h>
>
> int main (void)
> {
> float values[3];
> float element, average;
> float sum = 0; // this causes the problem
> int i;
>
> printf ("Please, enter 3 floating values: ");
>
> for (i = 1; i <= 3; i++)
> {
> scanf ("%f", &element);
>
> values[i] = element;
> sum += values[i];
> }
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> return 0;
> }
> Thank you!
The program that initializes sum to zero compiles fine on my machine.
What makes you conclude that the program doesn't compile?
Can you show the error messaage given by the compiler?
Anyway, Both programs have at least one problem:
The array values[3] has three elements,
values[0], values[1], and values[2].
The elements used in the code above are
values[1], values[2], and values[3].
So values[0] is never used, and values[3], which is not
an element of the array, is.
Another issue is that the result of scanf() is not checked, so
if the input contains data that does not represent a valid float,
this will go unnoticed. scanf() returns the number of items read
succesfully, so in this case it would be a good idea to check that
scanf("%f", &element) returns 1.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 11:16 -0700 |
| Message-ID | <9529ab6f-d652-4a7c-95b6-078916523117@googlegroups.com> |
| In reply to | #67936 |
On Friday, August 21, 2015 at 4:42:24 PM UTC+4, Ike Naar wrote:
> On 2015-08-21, Alla _ <modelling.data@gmail.com> wrote:
> > Hello!
> >
> > Here is a simple problem I created solving
> > one of the exercises from Programming in C.
> >
> > The program works, I hope (I tested a few
> > times). My question is why if I initialize
> > sum to zero in the main part, the program wouldn't
> > compile? Is it because the sum shouldn't have any
> > initial values in case I assign a formula to it
> > later in the program?
> >
> > The version that works:
> > // a program that calculates the average of an array of
> > 3 floating-point values
> >
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average, sum;
> > int i;
> >
> > printf ("Please, enter 3 floating values: ");
> >
> > for (i = 1; i <= 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > values[i] = element;
> > sum += values[i];
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> > }
> >
> > The version that doesn't:
> > // a program that calculates the average of an array
> > of 3 floating-point values
> >
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average;
> > float sum = 0; // this causes the problem
> > int i;
> >
> > printf ("Please, enter 3 floating values: ");
> >
> > for (i = 1; i <= 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > values[i] = element;
> > sum += values[i];
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> > }
> > Thank you!
>
> The program that initializes sum to zero compiles fine on my machine.
> What makes you conclude that the program doesn't compile?
> Can you show the error messaage given by the compiler?
>
> Anyway, Both programs have at least one problem:
> The array values[3] has three elements,
> values[0], values[1], and values[2].
>
> The elements used in the code above are
> values[1], values[2], and values[3].
>
> So values[0] is never used, and values[3], which is not
> an element of the array, is.
>
> Another issue is that the result of scanf() is not checked, so
> if the input contains data that does not represent a valid float,
> this will go unnoticed. scanf() returns the number of items read
> succesfully, so in this case it would be a good idea to check that
> scanf("%f", &element) returns 1.
Thank you very much! I will rewrite the program to follow your advice;
indeed I should have checked for these pitfalls.
[toc] | [prev] | [next] | [standalone]
| From | Siri Cruz <chine.bleu@yahoo.com> |
|---|---|
| Date | 2015-08-21 07:41 -0700 |
| Message-ID | <chine.bleu-502C0D.07413321082015@88-209-239-213.giganet.hu> |
| In reply to | #67931 |
In article <5f70d0ca-8b6b-4c11-a2d3-10a5fb65c1c3@googlegroups.com>,
Alla _ <modelling.data@gmail.com> wrote:
> int main (void)
> {
> float values[3];
> float element, average;
> float sum = 0; // this causes the problem
> scanf ("%f", &element);
This is the problem. scanf expects a (double*) for a %f, not a (float*). double
and float are usually different sizes, so scanf is likely storing in an
additional variable. Or randomly bombing the stack.
--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
When is a Kenyan not a Kenyan? When he's a Canadian.
That's People's Commissioner Siri Cruz now. Punch!
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-08-21 10:56 -0400 |
| Message-ID | <h_GBx.35060$rO6.2337@fx22.iad> |
| In reply to | #67946 |
On Friday August 21 2015 10:41, in comp.lang.c, "Siri Cruz"
<chine.bleu@yahoo.com> wrote:
> In article <5f70d0ca-8b6b-4c11-a2d3-10a5fb65c1c3@googlegroups.com>,
> Alla _ <modelling.data@gmail.com> wrote:
>
>> int main (void)
>> {
>> float values[3];
>> float element, average;
>> float sum = 0; // this causes the problem
>
>> scanf ("%f", &element);
>
> This is the problem. scanf expects a (double*) for a %f,
No.
You are probably thinking of the rules for printf() format characters,
where %f accepts a value from a double (often promoted from float).
The scanf() %f format specifier populates a <<float>> value through a
pointer-to-float.
The scanf() %lf format specifier populates a <<double>> value through a
pointer-to-double.
> not a (float*).
[snip]
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2015-08-21 09:28 -0600 |
| Message-ID | <1b8u9464pu.fsf@pfeifferfamily.net> |
| In reply to | #67946 |
Siri Cruz <chine.bleu@yahoo.com> writes:
> In article <5f70d0ca-8b6b-4c11-a2d3-10a5fb65c1c3@googlegroups.com>,
> Alla _ <modelling.data@gmail.com> wrote:
>
>> int main (void)
>> {
>> float values[3];
>> float element, average;
>> float sum = 0; // this causes the problem
>
>> scanf ("%f", &element);
>
> This is the problem. scanf expects a (double*) for a %f, not a (float*). double
> and float are usually different sizes, so scanf is likely storing in an
> additional variable. Or randomly bombing the stack.
First, he's reporting a compile time (actually, from later posts, link
time) error. What you're describing would be a run time error if it
were really the problem, but as scanf(3) says:
f Matches an optionally signed floating-point number; the next
pointer must be a pointer to float.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-08-21 10:02 -0700 |
| Message-ID | <qvletadal095ot27domrhveheti136m7i4@4ax.com> |
| In reply to | #67931 |
On Fri, 21 Aug 2015 04:39:51 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>Hello!
>
>Here is a simple problem I created solving
>one of the exercises from Programming in C.
>
>The program works, I hope (I tested a few
No it does not.
>times). My question is why if I initialize
>sum to zero in the main part, the program wouldn't
>compile? Is it because the sum shouldn't have any
>initial values in case I assign a formula to it
>later in the program?
>
>The version that works:
What you have is the most pernicious form of undefined behavior,
appearing to do what is desired.
>// a program that calculates the average of an array of
>3 floating-point values
>
>#include <stdio.h>
>
>int main (void)
>{
> float values[3];
> float element, average, sum;
> int i;
>
> printf ("Please, enter 3 floating values: ");
>
> for (i = 1; i <= 3; i++)
> {
> scanf ("%f", &element);
>
> values[i] = element;
When i is 3, this statement exhibits undefined behavior. Arrays in C
are zero based. The valid indices for values are 0, 1, and 2.
> sum += values[i];
In addition to attempting to access the non-existent object values[3],
this statement adds value[1] to sum when you have no idea what the
current contents of sum are. Automatic variables are not initialized
by default and therefore have indeterminate values at the start of
your function.
> }
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> return 0;
>}
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 11:21 -0700 |
| Message-ID | <0ad67494-3d43-4c80-9fa7-872a47e54bca@googlegroups.com> |
| In reply to | #67953 |
On Friday, August 21, 2015 at 9:02:38 PM UTC+4, Barry Schwarz wrote:
> On Fri, 21 Aug 2015 04:39:51 -0700 (PDT), Alla _
> <modelling.data@gmail.com> wrote:
>
> >Hello!
> >
> >Here is a simple problem I created solving
> >one of the exercises from Programming in C.
> >
> >The program works, I hope (I tested a few
>
> No it does not.
>
> >times). My question is why if I initialize
> >sum to zero in the main part, the program wouldn't
> >compile? Is it because the sum shouldn't have any
> >initial values in case I assign a formula to it
> >later in the program?
> >
> >The version that works:
>
> What you have is the most pernicious form of undefined behavior,
> appearing to do what is desired.
>
> >// a program that calculates the average of an array of
> >3 floating-point values
> >
> >#include <stdio.h>
> >
> >int main (void)
> >{
> > float values[3];
> > float element, average, sum;
> > int i;
> >
> > printf ("Please, enter 3 floating values: ");
> >
> > for (i = 1; i <= 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > values[i] = element;
>
> When i is 3, this statement exhibits undefined behavior. Arrays in C
> are zero based. The valid indices for values are 0, 1, and 2.
>
> > sum += values[i];
>
> In addition to attempting to access the non-existent object values[3],
> this statement adds value[1] to sum when you have no idea what the
> current contents of sum are. Automatic variables are not initialized
> by default and therefore have indeterminate values at the start of
> your function.
>
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> >}
>
> --
> Remove del for email
Thank you! True, I am doing way too much mistakes:
trying to get to grips with coding. Thank you; I will
correct these crucial mistakes.
"Remove del for email" what does it mean; how can I do that?
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-08-21 22:27 -0700 |
| Message-ID | <bn1gta1bk52u8i6fgocnhv0t9ac8brfsq2@4ax.com> |
| In reply to | #67957 |
On Fri, 21 Aug 2015 11:21:31 -0700 (PDT), Alla _ <modelling.data@gmail.com> wrote: >On Friday, August 21, 2015 at 9:02:38 PM UTC+4, Barry Schwarz wrote: <snip> >> -- >> Remove del for email > >Thank you! True, I am doing way too much mistakes: >trying to get to grips with coding. Thank you; I will >correct these crucial mistakes. > >"Remove del for email" what does it mean; how can I do that? If you were using a newsreader instead of the google interface, it would not quote data following the "-- " which is an end of content marker. Material that follows is considered part of the signature and not part of the text. I have added extra characters to camouflage my email address to prevent "message scrapers" from inundating me with spam. Should the need arise for a private conversation, that instruction identifies which letters are part of the camouflage and should be deleted from the address. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-22 02:25 -0700 |
| Message-ID | <b7978739-a3b8-4e7b-bdd2-ea30bf195653@googlegroups.com> |
| In reply to | #67981 |
On Saturday, August 22, 2015 at 9:27:06 AM UTC+4, Barry Schwarz wrote: > On Fri, 21 Aug 2015 11:21:31 -0700 (PDT), Alla _ > <modelling.data@gmail.com> wrote: > > >On Friday, August 21, 2015 at 9:02:38 PM UTC+4, Barry Schwarz wrote: > > <snip> > > >> -- > >> Remove del for email > > > >Thank you! True, I am doing way too much mistakes: > >trying to get to grips with coding. Thank you; I will > >correct these crucial mistakes. > > > >"Remove del for email" what does it mean; how can I do that? > > If you were using a newsreader instead of the google interface, it > would not quote data following the "-- " which is an end of content > marker. Material that follows is considered part of the signature and > not part of the text. > > I have added extra characters to camouflage my email address to > prevent "message scrapers" from inundating me with spam. Should the > need arise for a private conversation, that instruction identifies > which letters are part of the camouflage and should be deleted from > the address. > > -- > Remove del for email Thank you for explanation ))
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-22 03:50 -0700 |
| Message-ID | <f31a40a6-3db3-4cf5-a251-73779f5b9b9b@googlegroups.com> |
| In reply to | #67931 |
I am trying to correct my errors in this
program. I try to check if the user enters
a float or an integer.
Again I miss ground understanding:
the compiler yells at me by posting the error
message
In function 'main':
error: invalid operands to binary %
(have 'float' and 'int')
I understand that I have initialized element as
a float variable. Why the if statement doesn't
except the test for a remainder?
// a program that calculates the average of an array
of 3 floating-point values
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
float values[3];
float element, average, sum;
int i;
bool is_float;
printf ("Please, enter 3 floating values: \n");
for (i = 0; i < 3; i++)
{
scanf ("%f", &element);
is_float = 1;
if (element % 1 == 0)
is_float = 0;
printf ("Sorry, invalid input\n");
break;
if (is_float)
values[i] = element;
sum += values[i];
}
printf ("The average of 3 values is %.2f\n", average = sum / 3);
return 0;
}
Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-08-22 13:18 +0100 |
| Message-ID | <87r3mv7bys.fsf@bsb.me.uk> |
| In reply to | #67999 |
Alla _ <modelling.data@gmail.com> writes:
> I am trying to correct my errors in this
> program. I try to check if the user enters
> a float or an integer.
> Again I miss ground understanding:
> the compiler yells at me by posting the error
> message
> In function 'main':
> error: invalid operands to binary %
> (have 'float' and 'int')
It's just how C works. % is only for integers. Your test
if (element % 1 == 0)
seems to be checking that element has a zero fractional part. You can
do that, in C99 and later, by using the remainder function declared in
math.h (and you need to link with the math library).
> I understand that I have initialized element as
> a float variable. Why the if statement doesn't
> except the test for a remainder?
>
> // a program that calculates the average of an array
> of 3 floating-point values
>
> #include <stdio.h>
> #include <stdbool.h>
>
> int main (void)
> {
> float values[3];
> float element, average, sum;
sum needs to be set to 0.
> int i;
> bool is_float;
>
> printf ("Please, enter 3 floating values: \n");
>
> for (i = 0; i < 3; i++)
> {
> scanf ("%f", &element);
You should test the result of scanf. All input should be tested. It's
a good idea to get into the habit now rather then having to learn to do
it later.
> is_float = 1;
>
> if (element % 1 == 0)
> is_float = 0;
> printf ("Sorry, invalid input\n");
> break;
C is not Python! Only "is_float = 0;" is in the body of the if
statement. The other statements will always be executed.
> if (is_float)
> values[i] = element;
> sum += values[i];
Again, the indentation does not reflect the structure.
But why do you care about the fractional part of the number? And why
are you putting the numbers into an array and then ignoring the array?
> }
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
Why assign to average here? You could just print sum/3.
> return 0;
> }
> Thank you!
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-22 11:21 -0700 |
| Message-ID | <c0acb3a0-aa9d-43aa-b4ae-c00e7622e43a@googlegroups.com> |
| In reply to | #68002 |
On Saturday, August 22, 2015 at 4:19:01 PM UTC+4, Ben Bacarisse wrote:
> Alla _ <modelling.data@gmail.com> writes:
>
> > I am trying to correct my errors in this
> > program. I try to check if the user enters
> > a float or an integer.
> > Again I miss ground understanding:
> > the compiler yells at me by posting the error
> > message
> > In function 'main':
> > error: invalid operands to binary %
> > (have 'float' and 'int')
>
> It's just how C works. % is only for integers. Your test
>
> if (element % 1 == 0)
>
> seems to be checking that element has a zero fractional part. You can
> do that, in C99 and later, by using the remainder function declared in
> math.h (and you need to link with the math library).
>
> > I understand that I have initialized element as
> > a float variable. Why the if statement doesn't
> > except the test for a remainder?
> >
> > // a program that calculates the average of an array
> > of 3 floating-point values
> >
> > #include <stdio.h>
> > #include <stdbool.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average, sum;
>
> sum needs to be set to 0.
>
> > int i;
> > bool is_float;
> >
> > printf ("Please, enter 3 floating values: \n");
> >
> > for (i = 0; i < 3; i++)
> > {
> > scanf ("%f", &element);
>
> You should test the result of scanf. All input should be tested. It's
> a good idea to get into the habit now rather then having to learn to do
> it later.
>
> > is_float = 1;
> >
> > if (element % 1 == 0)
> > is_float = 0;
> > printf ("Sorry, invalid input\n");
> > break;
>
> C is not Python! Only "is_float = 0;" is in the body of the if
> statement. The other statements will always be executed.
>
> > if (is_float)
> > values[i] = element;
> > sum += values[i];
>
> Again, the indentation does not reflect the structure.
>
> But why do you care about the fractional part of the number? And why
> are you putting the numbers into an array and then ignoring the array?
>
> > }
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> Why assign to average here? You could just print sum/3.
>
> > return 0;
> > }
> > Thank you!
>
> --
> Ben.
Thank you so much. Feel ashamed about forgetting such
basic but important issues as braces after the if.
I have corrected these errors. Thank you!
Actually, with this version I am trying to test
scanf for invalid inputs, such as integers or 0.
// a program that calculates the average of an array
of 3 floating-point values
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
float values[3];
float element, average;
float sum = 0;
int i = 0, n = 0;
bool is_float;
printf ("Please, enter 3 floating values: \n");
do
{
scanf ("%f", &element);
is_float = 1;
n = (int) element;
i++;
if (n - element == 0 || element == 0 )
{
is_float = 0;
printf ("Sorry, invalid input\n");
break;
}
else
{
values[i] = element;
sum += values[i];
}
}
while (i < 3);
printf ("The average of 3 values is %.2f\n", average = sum / 3);
return 0;
}
Same with for loop:
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
float values[3];
float element, average;
float sum = 0;
int i, n;
bool is_float;
printf ("Please, enter 3 floating values: \n");
for ( i = 0; i < 3; i++)
{
scanf ("%f", &element);
is_float = 1;
n = (int) element;
if (n - element == 0 || element == 0 )
{
is_float = 0;
printf ("Sorry, invalid input\n");
break;
}
else
{
values[i] = element;
sum += values[i];
}
}
printf ("The average of 3 values is %.2f\n", sum / 3);
return 0;
}
Now I am struggling with the last printf. I looking for a way
to restrict printf to the valid result, and not show up
after the "break", i.e. in case the user types in an
incorrect number.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-08-22 21:55 +0100 |
| Message-ID | <87y4h359i3.fsf@bsb.me.uk> |
| In reply to | #68033 |
Alla _ <modelling.data@gmail.com> writes:
<snip>
> // a program that calculates the average of an array
> of 3 floating-point values
That's not really true as the array is irrelevant. The program will
work perfect without it -- it will simply be shorter.
Also, there is a mysterious condition on what values are acceptable.
The most mysterious part of the program is not mentioned in the comment
at the top.
> #include <stdio.h>
> #include <stdbool.h>
>
> int main (void)
> {
> float values[3];
> float element, average;
> float sum = 0;
> int i = 0, n = 0;
> bool is_float;
>
>
> printf ("Please, enter 3 floating values: \n");
>
> do
> {
> scanf ("%f", &element);
>
> is_float = 1;
> n = (int) element;
> i++;
>
> if (n - element == 0 || element == 0 )
> {
> is_float = 0;
> printf ("Sorry, invalid input\n");
> break;
> }
>
> else
> {
> values[i] = element;
> sum += values[i];
> }
>
> }
> while (i < 3);
>
> printf ("The average of 3 values is %.2f\n", average = sum / 3);
>
> return 0;
> }
>
> Same with for loop:
> #include <stdio.h>
> #include <stdbool.h>
>
> int main (void)
> {
> float values[3];
> float element, average;
> float sum = 0;
> int i, n;
> bool is_float;
>
>
> printf ("Please, enter 3 floating values: \n");
>
> for ( i = 0; i < 3; i++)
> {
> scanf ("%f", &element);
>
> is_float = 1;
> n = (int) element;
>
> if (n - element == 0 || element == 0 )
> {
> is_float = 0;
> printf ("Sorry, invalid input\n");
> break;
> }
>
> else
> {
> values[i] = element;
> sum += values[i];
> }
> }
> printf ("The average of 3 values is %.2f\n", sum / 3);
>
> return 0;
> }
There is no need for is_float, average or the array in either program.
Keep it simple.
> Now I am struggling with the last printf. I looking for a way
> to restrict printf to the valid result, and not show up
> after the "break", i.e. in case the user types in an
> incorrect number.
Why are numbers where (int)element - element == 0 || element == 0
incorrect? They look like correct numbers to me.
Why are you not testing the result of scanf? Is the book telling you
not to?
As to your question, what is the value of i after the loop if it ran to
completion? What value does it have if the loop was terminated with a
break?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-26 01:36 -0700 |
| Message-ID | <5e85b7d7-e360-4224-acbf-bf9d05b12ca2@googlegroups.com> |
| In reply to | #68044 |
On Sunday, August 23, 2015 at 12:55:14 AM UTC+4, Ben Bacarisse wrote:
> Alla _ <modelling.data@gmail.com> writes:
> <snip>
> > // a program that calculates the average of an array
> > of 3 floating-point values
>
> That's not really true as the array is irrelevant. The program will
> work perfect without it -- it will simply be shorter.
>
> Also, there is a mysterious condition on what values are acceptable.
> The most mysterious part of the program is not mentioned in the comment
> at the top.
>
> > #include <stdio.h>
> > #include <stdbool.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average;
> > float sum = 0;
> > int i = 0, n = 0;
> > bool is_float;
> >
> >
> > printf ("Please, enter 3 floating values: \n");
> >
> > do
> > {
> > scanf ("%f", &element);
> >
> > is_float = 1;
> > n = (int) element;
> > i++;
> >
> > if (n - element == 0 || element == 0 )
> > {
> > is_float = 0;
> > printf ("Sorry, invalid input\n");
> > break;
> > }
> >
> > else
> > {
> > values[i] = element;
> > sum += values[i];
> > }
> >
> > }
> > while (i < 3);
> >
> > printf ("The average of 3 values is %.2f\n", average = sum / 3);
> >
> > return 0;
> > }
> >
> > Same with for loop:
> > #include <stdio.h>
> > #include <stdbool.h>
> >
> > int main (void)
> > {
> > float values[3];
> > float element, average;
> > float sum = 0;
> > int i, n;
> > bool is_float;
> >
> >
> > printf ("Please, enter 3 floating values: \n");
> >
> > for ( i = 0; i < 3; i++)
> > {
> > scanf ("%f", &element);
> >
> > is_float = 1;
> > n = (int) element;
> >
> > if (n - element == 0 || element == 0 )
> > {
> > is_float = 0;
> > printf ("Sorry, invalid input\n");
> > break;
> > }
> >
> > else
> > {
> > values[i] = element;
> > sum += values[i];
> > }
> > }
> > printf ("The average of 3 values is %.2f\n", sum / 3);
> >
> > return 0;
> > }
>
> There is no need for is_float, average or the array in either program.
> Keep it simple.
>
> > Now I am struggling with the last printf. I looking for a way
> > to restrict printf to the valid result, and not show up
> > after the "break", i.e. in case the user types in an
> > incorrect number.
>
> Why are numbers where (int)element - element == 0 || element == 0
> incorrect? They look like correct numbers to me.
>
> Why are you not testing the result of scanf? Is the book telling you
> not to?
>
> As to your question, what is the value of i after the loop if it ran to
> completion? What value does it have if the loop was terminated with a
> break?
>
> --
> Ben.
Thank you for your questions, they are very helpful.
Actually, I hoped that I am testing scanf by using
if (n - element == 0 || element == 0 ). Now I see
that I don't understand what "to test scanf" means.
I would be grateful if you explain this.
As to numbers like 1.0, these are indeed float values,
but I made the program test for these ones and discard
them as invalid, because if the user enters, for example, 1 or 2,
the program will consider them as float values, i.e. 1.0, if
I understand that correctly.
"As to your question, what is the value of i after
the loop if it ran to completion? What value does it have
if the loop was terminated with a break?" The value of i
upon completion is 2 (after user enters 3 values from i=0 to i=2).
When the loop is terminated with break statement, the value of i
can be anything from 0 to 2, depending when the user enters the "invalid" value.
Thank you!
I
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.c
csiph-web