averages 2

B

Beej Jorgensen

Bill Cunningham said:
I don't know if I am bringing my problems to the forum to quickly
without trying it myself.

Google for the answers, too. Google is often a vastly better resource
than Usenet, especially for syntax error messages.

-Beej
 
N

Nick Keighley

 suspect the answer to these are one and the same: the first double
is number of doubles to add.

you've screwed up the attributions AGAIN
    I was unclear about using an int and double. One can be stored in the
other but not vice versa.

no. int and double are two different types and in general
one cannot be stored in the other. Why don't you just pass two
parameters?

double sma (int count, double[] numbers);
I am not looking for only one double.

you want an array
With arrays
you don't have to set aside a certain amount of memory

yes you do that is exactly what an array does
and one does not have to use malloc.

arrays and malloced memory are both blocks of memory.
If you write things properly then your function need not know
the difference.

int main (void)
{
double numa [42];
double *nump;

/* use an array */
sma (42, numa);

/* use malloc */
nump = malloc(1000);
if (nump == NULL) abort();
sma (1000, nump);
free (nump);

return 0;
}


I don't know how I would see how many doubles would be put
together.

you pass it as a parameter
    double * would take as many doubles as I gave it. (buffer control
problem??)

what is a buffer control problem?
 
N

Nick Keighley

news:c764e4b7-1662-44cc-808e-21eab51912c4@b14g2000yqd.googlegroups.com...
Well then, how about:

don't post fragments. Some people can get away with it
but you hack things about too much.
double Sma (double *num) {
  int count=*num,i;

what value does i have?
  for (*num=0;i;i--) {

why did you zero the first element of num?
why is i decrementing?
    *num+=*(num+i+1);

you are using the first element as the sum?
I think *num is being used for too many things.
Keep It Simple. Use another variable for the sum.
  }
  return *num/count;

}

Has the i been previously declared?
well?

The for statement I understand as a dereference.

there are derefrences in there. I doubt you understand though
After trying to remember what I think I once knew and looking it up for the
nth time I think something like the above is what I would come up with. I
will see if it compiles.

your layout is horrid

double sma (int count, double *num)
{
int i;
double sum = 0;

/* don't try and be clever (you aren't).
write simple code that does the job in a straight-forward way */
for (i = 0; i < count; i++)
{
sum += *(num + i);
/* or easier to understand
sum += num ;
*/
}

return sum / count;
}

don't use num for three different things (are you short
of memory or something?). don't count backwards in your
for-loop. use array notation when it is clearer.

Don't try be clever
 
N

Nick Keighley

    I didn't mean to start something. I admit to laziness. I don't like to
have to go through kandr2 again and again. 3 days after I read it I've
forgotten it if I understood what I was reading. Maybe it is just stupidity.

when I was learning C I wrote notes on bits of card.
I had about 10 cards. Cam in handy when I was helping
someone learn C. I've done it with other languages.
 
L

luserXtrog

don't post fragments. Some people can get away with it
but you hack things about too much.

Most of that fragment was mine.
I deserve the blame.
what value does i have?

My oops.
i should have been initialized to count.
why did you zero the first element of num?

To reuse it as an accumulator, since the count
which it presumably previously held had been
copied.
why is i decrementing?

So I could use a bare i as the loop test.
you are using the first element as the sum?
I think *num is being used for too many things.
Keep It Simple. Use another variable for the sum.

Agreed. But it was already doing too many things.
there are derefrences in there. I doubt you understand though


your layout is horrid

Again, mea culpa.
double sma (int count, double *num)
{
  int  i;
  double sum = 0;

  /* don't try and be clever (you aren't).
     write simple code that does the job in a straight-forward way */
  for (i = 0; i < count; i++)
  {
    sum += *(num + i);
    /* or easier to understand
    sum += num ;
   */
  }

  return sum / count;

}

don't use num for three different things (are you short
of memory or something?). don't count backwards in your
for-loop. use array notation when it is clearer.

Don't try be clever


Cannot promise.
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top