average problem

B

Bill Cunningham

#include <stdio.h>

double Sma(double *num, int val)
{
int i, res = 0;
for (i = 0; i < val; i++) {
res += num;
}
return res / (double) val;
}


int main(void)
{
double d, *p = (12.5 12 .5);
d = Sma(p, 3);
printf("%.2f\n", d);
}


p.c: In function `main':
p.c:5: parse error before numeric constant


I wrote this code and got this error. Where's the parse error? I'm not
seeing it.

Bill
 
M

Morris Keesan

p.c: In function `main':
p.c:5: parse error before numeric constant


I wrote this code and got this error. Where's the parse error?

double d, *p = (12.5 12 .5);

Also, there's a conceptual error here. What do you think you're
initializing the pointer p to point at?
 
D

dada

Bill Cunningham wrote:

double d, *p = (12.5 12 .5);

Bill,

p is a pointer to double, what is it's value ?
It does not have one. "(12.5 12 .5)" is gibberish.

p.c: In function `main':
p.c:5: parse error before numeric constant


I wrote this code and got this error. Where's the parse error? I'm not
seeing it.

Bill

Try this :

#include <stdio.h>

double Sma(double *num, int val)
{
int i, res = 0;
for (i = 0; i < val; i++) {
res += num;
}
return res / (double) val;
}


int main(void)
{
double d, p[] = {12.5, 12, .5};
double r[] = {1, 2, 3, 4};
double s[] = {3, 3};
d = Sma(p, 3);
printf("%.2f\n", d);

d = Sma(r, 4);
printf("%.2f\n", d);

d = Sma(s, 2);
printf("%.2f\n", d);

return 0 ;
}

dada@thud:~/junk$ gcc -ansi -Wall test.c -pedantic
dada@thud:~/junk$ ./a.out
8.00
2.50
3.00
dada@thud:~/junk$

And then try to figure out where you went wrong.

Joe
 
D

dada

dada said:
Bill Cunningham wrote:



Bill,

p is a pointer to double, what is it's value ?
It does not have one. "(12.5 12 .5)" is gibberish.

or this :

dada@thud:~/junk$ more test.c
#include <stdio.h>

double Sma(double *num, int val )
{
int i, res = 0;

for (i = 0; i < val; i++) {
res += num;
}
return res / (double) val;
}


int main(void)
{
double d, p[] = {12.5, 12, .5};
double r[] = {1, 2, 3, 4};
double s[] = {3, 3};
d = Sma(p, sizeof p / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(r, sizeof r / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(s, sizeof s / sizeof ( double ) );
printf("%.2f\n", d);

return 0 ;
}

dada@thud:~/junk$ gcc -ansi -Wall test.c -pedantic
dada@thud:~/junk$ ./a.out
8.00
2.50
3.00
dada@thud:~/junk$
And then try to figure out where you went wrong.

Joe

or
 
O

osmium

Bill Cunningham said:
#include <stdio.h>

double Sma(double *num, int val)
{
int i, res = 0;
for (i = 0; i < val; i++) {
res += num;
}
return res / (double) val;
}


int main(void)
{
double d, *p = (12.5 12 .5);
d = Sma(p, 3);
printf("%.2f\n", d);
}


p.c: In function `main':
p.c:5: parse error before numeric constant


I wrote this code and got this error. Where's the parse error? I'm not
seeing it.


It looks to me like you want to use an array. The index of K&R says array
declarations are covered on p. 22 and array initializations are covered on
p. 86. There seem to be [] and {} involved in some way and you don't have
any of either of those in the immediate vicinity of where your error message
is coming from.
 
B

Bill Cunningham

int main(void)
{
double d, p[] = {12.5, 12, .5};
double r[] = {1, 2, 3, 4};
double s[] = {3, 3};
d = Sma(p, sizeof p / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(r, sizeof r / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(s, sizeof s / sizeof ( double ) );
printf("%.2f\n", d);

return 0 ;
}

Would I need to put sizeof operator in the Sma function? And that double
cast to val. Is it necessary? I put it there so there could be division
betwwen two different types.

Bill
 
U

user923005

#include <stdio.h>

double Sma(double *num, int val)
{
    int i, res = 0;
    for (i = 0; i < val; i++) {
        res += num;
    }
    return res / (double) val;

}

int main(void)
{
    double d, *p = (12.5 12 .5);
    d = Sma(p, 3);
    printf("%.2f\n", d);

}

p.c: In function `main':
p.c:5: parse error before numeric constant

    I wrote this code and got this error. Where's the parse error? I'm not
seeing it.


Maybe:

C:\tmp>gcc -Wall -ansi -pedantic t.c

C:\tmp>a
8.33

C:\tmp>type t.c
#include <stdio.h>
double Sma(double *num, int val)
{
int i;
double res = 0;
for (i = 0; i < val; i++) {
res += num;
}
return res / val;
}

int main(void)
{
double d,
p[] = {12.5, 12.0, 0.5};
size_t psize = sizeof p / sizeof p[0];
d = Sma(p, psize);
printf("%.2f\n", d);
return 0;
}
 
B

Bill Cunningham

It looks to me like you want to use an array. The index of K&R says array
declarations are covered on p. 22 and array initializations are covered on
p. 86. There seem to be [] and {} involved in some way and you don't have
any of either of those in the immediate vicinity of where your error
message is coming from.

There was some cutting and pasting here. The error occured at this line.

double d, *p = (12.5 12 .5);

I always thought pointers and arrays are interchangable. Must this be used

double p[]={12.5,12,.5};

There is no way to set this up like this?

double *p;

/*so on? like for example I'll try to do this right. */


*(p+1)=12.5;
*(p+2)=.5;

Do you know what I'm meaning here? Successive pointer elements much like
successive array elements.

Bill
 
D

dada

Bill said:
int main(void)
{
double d, p[] = {12.5, 12, .5};
double r[] = {1, 2, 3, 4};
double s[] = {3, 3};
d = Sma(p, sizeof p / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(r, sizeof r / sizeof ( double ) );
printf("%.2f\n", d);

d = Sma(s, sizeof s / sizeof ( double ) );
printf("%.2f\n", d);

return 0 ;
}

Would I need to put sizeof operator in the Sma function? And that double
cast to val. Is it necessary? I put it there so there could be division
betwwen two different types.

Bill
Actually I didn't look real close at the SMA function. I think that you
might want to change it to look like this:

double Sma(double *num, int val )
{
int i ;
double res = 0.0 ;

for (i = 0; i < val; i++) {
res += num;
}
return res / val;
}

dada@thud:~/junk$ gcc -ansi -Wall test.c -pedantic
dada@thud:~/junk$ ./a.out
8.33
2.50
3.00
dada@thud:~/junk$



with res defined as an int you lose precision, which may or not be what
you want.

By the way Bill. I like this example better than some of the others that
you have posted in the past. In those others you seemed to be trying to
do too much at once (argc, argv, ponter manipulation, converting char
strings to int/float, etc).

in my experience it is better to make sure you understand the simple
concepts such as used in this program, and then move on to new concepts,
using what you have learned previously as a base to build on.

Hope this helps.
Joe
 
D

dada

Bill said:
It looks to me like you want to use an array. The index of K&R says array
declarations are covered on p. 22 and array initializations are covered on
p. 86. There seem to be [] and {} involved in some way and you don't have
any of either of those in the immediate vicinity of where your error
message is coming from.

There was some cutting and pasting here. The error occured at this line.

double d, *p = (12.5 12 .5);

I always thought pointers and arrays are interchangable. Must this be used

double p[]={12.5,12,.5};

There is no way to set this up like this?

double *p;

/*so on? like for example I'll try to do this right. */


*(p+1)=12.5;
*(p+2)=.5;

Do you know what I'm meaning here? Successive pointer elements much like
successive array elements.

Bill

if you want to use that notation you have to allocate the memory first...

double p[] = { 0.0, 0.0, 0.0 } ;

allocates at *compile time* the space for 3 doubles.
When you call a function e.g. sma ( p, 3 ) ;

what gets passed to sma is a ponter to the first element in the array.

on the other hand,

double *p ;

only allocates the space for a single pointer to a double.

double *p = NULL ; is OK
double *p = 0xFFFF ; *might* be ok, on say an embedded system where you
know that for some reason the address 0xFFFF points to the place that a
double resides (perhaps as some sort of hardware device register).

However, to use p "normally" you need to allocate space for your values

p = malloc ( 5 * sizeof ( double ) ) ;

will allocate space for 5 doubles, and return a pointer to that space
and put it in p.

you can then do dereference p and put your data threre
*p = 0.0 ;
p++ ;
*p = 4.4 ;
p++ ;

.... etc

which will place your data in the space that you provided for it.

However, if *I* was doing this in a real program, depending upon the
application I would probably do


#define ARRAY_SIZE 5

double p [ ARRAY_SIZE ] ;
p [ 0 ] = 0.0 ;
p [ 1 ] = 4.4 ;

etc

and then

sma ( p, ARRAY_SIZE ) ;

or something similar.

Joe
 
O

osmium

Bill Cunningham said:
It looks to me like you want to use an array. The index of K&R says
array declarations are covered on p. 22 and array initializations are
covered on p. 86. There seem to be [] and {} involved in some way and
you don't have any of either of those in the immediate vicinity of where
your error message is coming from.

There was some cutting and pasting here. The error occured at this
line.

double d, *p = (12.5 12 .5);

I understood that.
I always thought pointers and arrays are interchangable. Must this be used

double p[]={12.5,12,.5};

I would suggest you continue that approach, there are other ways but the way
on the line above conforms to the function you supplied. But it still will
not work, this just gets you over the first hurdle. After you get
comfortable with the easy way you can start over with some more esoteric
approach.

There is a similarity between pointers and arrays, they are not
interchangeable. An array typically has (contains) more than one value; a
pointer has only one value, but the value can be changed, just like any
other variable.

Someone suggested a few weeks ago that you establish a card file as a memory
aid, an *excellent* idea. How many cards do you have in your file by now?
 
B

Bill Cunningham

Someone suggested a few weeks ago that you establish a card file as a
memory aid, an *excellent* idea. How many cards do you have in your file
by now?

I did not realize that. But it does sound like a good idea.
I understand return values, character conversions. Most of what printf
does though there are other things that exist like ^[] format specifiers I
haven't came across in anything I do. As far as this technique of counting
I am pounding it into my skull and I think I will master it. I think my
plate is full now. I will keep working on the counting algorithm.

Bill
 
C

Curtis Dyer

It looks to me like you want to use an array. The index of K&R
says array declarations are covered on p. 22 and array
initializations are covered on p. 86. There seem to be [] and
{} involved in some way and you don't have any of either of
those in the immediate vicinity of where your error message is
coming from.

There was some cutting and pasting here. The error occured
at this line.

double d, *p = (12.5 12 .5);

I always thought pointers and arrays are interchangable.

This is not the case, as others have mentioned. Have a look at
Section 6 in the C FAQ: <http://www.c-faq.com/>. The first few
questions covered there should help you get the distinction
between arrays and pointers.
Must this be used

double p[]={12.5,12,.5};

Well, it would seem to be the most concise and natural approach,
given the context of your example.
There is no way to set this up like this?

double *p;

If you really wanted a pointer in your example, you could have
used:

double d[] = { 12.5, 12.0, 0.5 };
double *p = d;

Alternatively, you could have allocated the memory at run-time
using malloc().

But, again, it doesn't seem very useful for the original code you
posted.

<snip>
 
K

Keith Thompson

Bill Cunningham said:
double d, *p = (12.5 12 .5); [...]

p.c: In function `main':
p.c:5: parse error before numeric constant


I wrote this code and got this error. Where's the parse error? I'm not
seeing it.

Others have already explained the error.

One thing that you might find helpful is to avoid declaring multiple
objects on line one. If you had instead written:

double d;
double *p = (12.5 12 .5); /* error here */

it would have been easier to see where the problem is. Declaring
multiple objects on a line is generally not recommended for other
reasons as well. For example:

int *x, y;

declares x as an int* and y as an int. It's easy for a beginner to
assume that it declares them both as int*. (C's declaration syntax is
admittedly difficult.) Writing this as:

int *x;
int y;

or as

int *x;
int *y;

makes it much easier to avoid errors like this, even for experienced
programmers.

As for what you're trying to do, you want p, of type double*, to point
to the first element of an array with elements 12.5, 12, and 0.5.
With strings, this is easy:

char *ptr = "hello";

The string literal "hello" creates the array for you, and lets you
initialize ptr to point to its first element.

In C90, there's no corresponding construct for arrays of doubles.

C99 introduces compound literals, so you can write:

double *p = (double[]){12.5, 12.0, 0.5};

But I don't think I'd advise you to use it. For one thing, it's
specific to C99. For another, given the declaration above, there's no
way to get the number of elements.
 

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


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top