error in code

B

Bill Cunningham

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
 
B

Bill Cunningham

pozz said:
Il 28/05/2011 21:28, Bill Cunningham ha scritto:

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.
 
S

Shao Miller

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

Lew Pitcher

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.
 
G

Geoff

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.
 
B

Bill Cunningham

Shao said:
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; <-
average = accumulator / periods;
return average;
}


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

Bill
 
G

Geoff

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

Geoff

Shao said:
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; <-
average = accumulator / periods;
return average;
}


I'm a little confused here. accumulator+accumulator=values;
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;

mean = sum / periods;

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

Shao Miller

Shao said:
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;<-
average = accumulator / periods;
return average;
}


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


The line:

accumulator += values;

is similar to:

accumulator = accumulator + values;

and not to:

accumulator + accumulator = values;

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

Ian Collins

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?

To get a C programming bog and read it.
 
A

Angel

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?

That you should get a good tutorial on the syntax of C, and read it.

The error means literally what it says, you initialized your array
wrongly. An array initializer must be between accolades.
 
I

Ian Collins

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?

That you should get a good tutorial on the syntax of C, and read it.

The error means literally what it says, you initialized your array
wrongly. An array initializer must be between accolades.

accolades?

I guess you aren't a native English speaker. In English an accolade is
some form of award or praise!
 
K

Keith Thompson

Bill Cunningham said:
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?

That you need to consult whatever C reference you're using and re-read
the section that discusses the syntax of array initializers.
 
A

Angel

Well well, we learn something new every day, don't we!

Picking up hints to write better C code, improving my English and even
learning something about sheet music. And to think some say Internet is
nothing but a waste of time. ^^
 
J

Joe Pfeiffer

Bill Cunningham said:
double a[] = 2.2, 4.2, 5.00;
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?

Depending on how long you've been up, it's telling you that you need
either sleep or coffee. :)

You need braces around the initializer list:
double a[] = {2.2, 4.2, 5.00};
 
J

Joe Pfeiffer

Bill Cunningham said:
Shao Miller wrote:
accumulator += values; <-


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


No, it means

accumulator = accumulator + values;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

average problem 13
pointer array problem? 7
How to fix this code? 1
avg 36
error 20
puzzling error 49
How can I view / open / render / display a pdf file with c code? 0
nodes 4

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top