[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

error in code

Bill Cunningham

5/28/2011 7:28:00 PM

I am trying here to calculate a mean of 3 values. I do not understand
the errors the compiler returned I've never seen them.

#include <stdio.h>

void mean(double *avg, int periods)
{
double val;
val = *avg / periods;
printf("%.2f\n", val);
}

int main(void)
{
double a[] = 2.2, 4.2, 5.00;
mean(a, 3);
}
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

What's it trying to tell me?

Bill



49 Answers

pozz

5/28/2011 7:50:00 PM

0

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.



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

or define a generic macro if you're going to use this syntax in several
points:

#define SIZEARRAY(a) (sizeof(a) / sizeof(a[0]))

In this way, you can modify the number of elements in the
initialization, but you haven't to modify the number of elements that
will be automatically calculated by the preprocessor.

Of course, this is valid only for static allocated array. You you
dinamically allocate an array, you are responsible to take its lenght in
some variable.

> }
> p.c: In function `main':
> p.c:12: invalid initializer
> p.c:12: parse error before numeric constant
>
> What's it trying to tell me?

See above.

Bill Cunningham

5/28/2011 8:12:00 PM

0

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.

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

> or define a generic macro if you're going to use this syntax in
> several points:
>
> #define SIZEARRAY(a) (sizeof(a) / sizeof(a[0]))
>
> In this way, you can modify the number of elements in the
> initialization, but you haven't to modify the number of elements that
> will be automatically calculated by the preprocessor.
>
> Of course, this is valid only for static allocated array. You you
> dinamically allocate an array, you are responsible to take its lenght
> in some variable.
>
>> }
>> p.c: In function `main':
>> p.c:12: invalid initializer
>> p.c:12: parse error before numeric constant
>>
>> What's it trying to tell me?
>
> See above.


Shao Miller

5/28/2011 8:34:00 PM

0

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

Lew Pitcher

5/28/2011 8:35:00 PM

0

On May 28, 2011 16:12, in comp.lang.c, nospam@nspam.invalid 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.

Bill, you've done it before. Why can't you write a "cycle" (aka a "loop")
this time?

Think
for (....) {/*something*/}
or
while (....) {/*something*/}
or
do {/*something*/} while (....);


> It's these little
> caveats in C that confuse me most. Functions I am grasping. Writing cycles
> not so much.
>
>>> 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?

Again, you know this.

What does sizeof(int) mean?
What does sizeof(double) mean?
What does sizeof("string") mean?

>> or define a generic macro if you're going to use this syntax in
>> several points:
>>
>> #define SIZEARRAY(a) (sizeof(a) / sizeof(a[0]))
>>
>> In this way, you can modify the number of elements in the
>> initialization, but you haven't to modify the number of elements that
>> will be automatically calculated by the preprocessor.
>>
>> Of course, this is valid only for static allocated array. You you
>> dinamically allocate an array, you are responsible to take its lenght
>> in some variable.
>>
>>> }
>>> p.c: In function `main':
>>> p.c:12: invalid initializer
>>> p.c:12: parse error before numeric constant
>>>
>>> What's it trying to tell me?
>>
>> See above.
>
>

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------


Geoff

5/28/2011 8:47:00 PM

0

On Sat, 28 May 2011 15:28:24 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

> I am trying here to calculate a mean of 3 values. I do not understand
>the errors the compiler returned I've never seen them.
>
>#include <stdio.h>
>
>void mean(double *avg, int periods)
>{
> double val;
> val = *avg / periods;
> printf("%.2f\n", val);
>}
>
>int main(void)
>{
> double a[] = 2.2, 4.2, 5.00;
> mean(a, 3);
>}
>p.c: In function `main':
>p.c:12: invalid initializer
>p.c:12: parse error before numeric constant
>
> What's it trying to tell me?
>

It's telling you the array of doubles, a[] was initialized
incorrectly. You must use braces to initialize an array.
The parse error is a secondary effect of the bad syntax of your array
initialization.

Bill Cunningham

5/28/2011 9:13:00 PM

0

Shao Miller wrote:
> 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;
> }

I'm a little confused here. accumulator+accumulator=values[i];
The i is being iterated. The accumulator is where the value is being stored
right?

Bill


Geoff

5/28/2011 9:21:00 PM

0

On Sat, 28 May 2011 16:12:03 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>> mean(a, sizeof(a) / sizeof(a[0]));
>
>What's the sizeof(a[0]) mean?

It's the size, in bytes, (whatever they are), of the first element of
the array "a". It's better than writing sizeof(double) because if you
change the declaration of "a" from double a[] to float a[] you don't
have to change the calculation of the size.

sizeof(a) / sizeof(a[0]) is "the total length of the array divided by
the length of any member of the array" (i.e., the count of the members
of the array).

Geoff

5/28/2011 9:26:00 PM

0

On Sat, 28 May 2011 17:12:45 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>Shao Miller wrote:
>> 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;
>> }
>
> I'm a little confused here. accumulator+accumulator=values[i];
>The i is being iterated. The accumulator is where the value is being stored
>right?
>
>Bill
>

It would be better written as:

void mean(double *array, int periods)
{
int i;
double mean;
double sum;

sum = 0;
for (i=0; i < periods; i++)
sum = sum + array[i];

mean = sum / periods;

printf("%.2f\n", mean);
}

Shao Miller

5/28/2011 9:29:00 PM

0

On 5/28/2011 4:12 PM, Bill Cunningham wrote:
> Shao Miller wrote:
>> 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;
>> }
>
> I'm a little confused here. accumulator+accumulator=values[i];
> The i is being iterated. The accumulator is where the value is being stored
> right?

The line:

accumulator += values[i];

is similar to:

accumulator = accumulator + values[i];

and not to:

accumulator + accumulator = values[i];

Yes. As its name suggests, the 'accumulator' is accumulating every
value in the array; it's the sum.

John Doe

5/28/2011 10:06:00 PM

0

On Sat, 28 May 2011 15:28:24 -0400, Bill Cunningham wrote:

> void mean(double *avg, int periods)
> {
> double val;
> val = *avg / periods;
> printf("%.2f\n", val);
> }
> }

Note the excess closing brace.