avg

  • Thread starter Bill Cunningham
  • Start date
D

dada

Bill said:
Mara Guida said:
Bill said:
#include <stdio.h>

double Avg(double *num, int n)
{
double sum = 0.0;
int i;
for (i = 0; i <= n; ++i)
sum = sum + n; /* rather confusing line. lvalue or
rvalue?
return num/n; how is it read? */
}

int main()
{
double v[] = { 22, 23.5, 2.5 };
printf("%.2f\n", Avg(v, 3));
return 0;
}

Bill, try to name your variables with more descriptive names.

Here is your program again with different names for variables. That is
the only change.


#include <stdio.h>

double Avg(double *num_list, int n_elems)
{
double sum = 0.0;
int index;
for (index = 0; index <= n_elems; ++index)
sum = sum + n_elems[index]; /* rather confusing line.
lvalue or rvalue?
return num_list/elems; how is it read? */
}

int main()
{
double values[] = { 22, 23.5, 2.5 };
printf("%.2f\n", Avg(values, 3));
return 0;
}


Also, notice your comment is two lines long and "hides" the return
statement from your function.


OK I figured it out. That += was confusing me. sum=sum+index[num]; makes
sense.
sum+=index[num] confuses me. I guess everything is being loaded into the
left side of the equals. Whew. It works now.

Bill

Well Bill,
for what it's worth, I very rarely use the form "+=" (as in A += B,
because when I am scanning code I have to stop and verify what it is
doing. For me A = A + B is much easier to parse visually.

Have a good evening
Joe
 
N

Nick Keighley

    I am trying to write a function that averages. This is what I have and
the compiler diagnostics. I think I'm close.

#include <stdio.h>

double Avg(double *num, int n)
{
    double i;
    for (i = 0; i < n; ++i)
 i += num[n];
    return num / n;}

p.c: In function `Avg':
p.c:8: invalid operands to binary /

haven't you written this before. Check the archives.
 
B

Ben Bacarisse

Thomas Matthews said:
Bill said:
I am trying to write a function that averages. This is what I
have and the compiler diagnostics. I think I'm close.

#include <stdio.h>

double Avg(double *num, int n)
{
double i;
for (i = 0; i < n; ++i)
i += num[n];
return num / n;
}
p.c: In function `Avg':
p.c:8: invalid operands to binary /

Bill
Since I'm tired, I'll give you the answer:
double Arithmetic_Mean(double * array_of_numbers,
int quantity_of_numbers)
{
double sum = 0.0;
double average = 0.0;
unsigned int index = 0;
do
{
if (array_of_numbers == NULL)
{
/* This should communicate an error value
* to the client.
*/
break;
}
for (index = 0; index < quantity_of_numbers; ++index)
{
sum += array_of_numbers[index];
}
average = sum / ((1.0) * quantity_of_numbers);
} while (false);
return average;
}

Notes:
1. The above code has not been tested.
2. The (1.0) in the average computation is used to convert
the quantity from an int to a double without casts.

No need to do this at all.

How did it end up with a pointless do while loop? I can see that you
"use" the loop because you have a break, but the structure is simple
enough without any such trickery:

double average = 0.0;
if (array_of_numbers != NULL)
{
double sum = 0.0;
int index = 0;
for (index = 0; index < quantity_of_numbers; ++index)
{
sum += array_of_numbers[index];
}
average = sum / quantity_of_numbers;
}
return average;

Note that since the quantity_of_numbers parameter is an int, it is
very unwise to test index < it. I've changed index to be an int, but
you can also change the interface to pass an unsigned (size_t is the
obvious choice).

You could initialise average to NAN if you have them available; that
would be a logical choice to return when there is no average.
 
N

Nick Keighley

Thomas Matthews said:
Bill said:
    I am trying to write a function that averages. This is what I
have and the compiler diagnostics. I think I'm close.
#include <stdio.h>
double Avg(double *num, int n)
{
    double i;
    for (i = 0; i < n; ++i)
 i += num[n];
    return num / n;
}
p.c: In function `Avg':
p.c:8: invalid operands to binary /
Bill
Since I'm tired, I'll give you the answer:
double Arithmetic_Mean(double * array_of_numbers,
                       int      quantity_of_numbers)
{
  double sum = 0.0;
  double average = 0.0;
  unsigned int index = 0;
  do
  {
    if (array_of_numbers == NULL)
    {
      /* This should communicate an error value
       * to the client.
       */
      break;
    }
    for (index = 0; index < quantity_of_numbers; ++index)
    {
      sum += array_of_numbers[index];
    }
    average = sum / ((1.0) * quantity_of_numbers);
  } while (false);
  return average;
}
Notes:
1.  The above code has not been tested.
2.  The (1.0) in the average computation is used to convert
    the quantity from an int to a double without casts.

No need to do this at all.

How did it end up with a pointless do while loop?  I can see that you
"use" the loop because you have a break, but the structure is simple
enough without any such trickery:

   double average = 0.0;
   if (array_of_numbers != NULL)
   {
       double sum = 0.0;
       int index = 0;
       for (index = 0; index < quantity_of_numbers; ++index)
       {
           sum += array_of_numbers[index];
       }
       average = sum / quantity_of_numbers;
   }
   return average;

Note that since the quantity_of_numbers parameter is an int, it is
very unwise to test index < it.  I've changed index to be an int, but
you can also change the interface to pass an unsigned (size_t is the
obvious choice).

You could initialise average to NAN if you have them available; that
would be a logical choice to return when there is no average.

what if quantity_of_numbers is zero?
 
B

Ben Bacarisse

Nick Keighley said:
Thomas Matthews <[email protected]> writes:
Since I'm tired, I'll give you the answer:
double Arithmetic_Mean(double * array_of_numbers,
                       int      quantity_of_numbers)
{
  double sum = 0.0;
  double average = 0.0;
  unsigned int index = 0;
  do
  {
    if (array_of_numbers == NULL)
    {
      /* This should communicate an error value
       * to the client.
       */
      break;
    }
    for (index = 0; index < quantity_of_numbers; ++index)
    {
      sum += array_of_numbers[index];
    }
    average = sum / ((1.0) * quantity_of_numbers);
  } while (false);
  return average;
}
Notes:
1.  The above code has not been tested.
2.  The (1.0) in the average computation is used to convert
    the quantity from an int to a double without casts.

No need to do this at all.

How did it end up with a pointless do while loop?  I can see that you
"use" the loop because you have a break, but the structure is simple
enough without any such trickery:

   double average = 0.0;
   if (array_of_numbers != NULL)
   {
       double sum = 0.0;
       int index = 0;
       for (index = 0; index < quantity_of_numbers; ++index)
       {
           sum += array_of_numbers[index];
       }
       average = sum / quantity_of_numbers;
   }
   return average;
what if quantity_of_numbers is zero?

Good point. I just copied the original logic. It would e better to
write:

if (array_of_numbers != NULL && quantity_of_numbers > 0)

though it is just possible that the original is intended to work only
when there is a non-zero length array pointed to by array_of_numbers.
I.e. that you pass NULL rather than a zero length. If that was the
idea, I would say it is not a good one.
 
B

Bill Cunningham

haven't you written this before. Check the archives.

Yes. Indeed I have. I lost the program when I was messing with my mbr
and had to put back into the HD what was saved on my DVD. Unfortunately the
little program I had was lost. Indeed I have inquired. But please don't get
the wrong idea. The little snippet I had was lost forever and I googled alot
before I posted here again. I am going to try now to alter the code to
create a geometric mean. Maybe a harmonic and golden mean too. I had a
little program I wrote called r. It would return the answer given between
and radical and radicand. I will rewrite it and not bother anyone here. Yes
you are correct Nick.

Bill
 
B

Bill Cunningham

Since I'm tired, I'll give you the answer:
double Arithmetic_Mean(double * array_of_numbers,
int quantity_of_numbers)
{
double sum = 0.0;
double average = 0.0;
unsigned int index = 0;
do
{
if (array_of_numbers == NULL)
{
/* This should communicate an error value
* to the client.
*/
break;
}
for (index = 0; index < quantity_of_numbers; ++index)
{
sum += array_of_numbers[index];
}
average = sum / ((1.0) * quantity_of_numbers);
} while (false);
return average;
}

Notes:
1. The above code has not been tested.
2. The (1.0) in the average computation is used to convert
the quantity from an int to a double without casts.
Oh my Thomas I cannot read this. It's way above my abilities. I
certainly wish I could.

Bill
 
B

Bill Cunningham

Oh my Thomas I cannot read this. It's way above my abilities. I
certainly wish I could.

BTW I've been seeing true and false in some code lately. In this and in
tree code I've looked at lately. Is this part of C ? I've never come across
it before. Not C or C++ at all. I started with C++ btw and decided it was a
trumped up version of C and I was converted.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I am going to try now to alter the code to
create a geometric mean. Maybe a harmonic and golden mean too.

The "golden mean" is another term for the "golden ratio". It's a
specific constant, not something you compute from a list of numbers.
 
K

Keith Thompson

Bill Cunningham said:
BTW I've been seeing true and false in some code lately. In this and in
tree code I've looked at lately. Is this part of C ? I've never come across
it before. Not C or C++ at all. I started with C++ btw and decided it was a
trumped up version of C and I was converted.

In C++, true and false are keywords, literals of the predefined type
bool.

In C90, they don't exist unless you define them.

In C99, they're declared in <stdbool.h> as macros that expand to the
constants 1 and 0, respectively.

See also section 9 of the comp.lang.c FAQ.
 
O

osmium

Richard Heathfield said:
Nevertheless, it can be constructed from a list of numbers, provided
the list has at least two elements.

For God's sake Bill, don't get on this golden mean kick. You will drive
yourself and a lot of the observers crazy. There is no good that can come
to you from fiddling with it. You seem to have a hankering for statistics,
you can do the mode and median and learn something from them. It takes
more than a catchy name to make a learning exercise worthwhile.
 
N

Nick Keighley

Nick Keighley said:
On 6 Sep, 12:49, Ben Bacarisse <[email protected]> wrote:
   double average = 0.0;
   if (array_of_numbers != NULL)
   {
       double sum = 0.0;
       int index = 0;
       for (index = 0; index < quantity_of_numbers; ++index)
       {
           sum += array_of_numbers[index];
       }
       average = sum / quantity_of_numbers;
   }
   return average;
what if quantity_of_numbers is zero?

Good point.  I just copied the original logic.  It would e better to
write:

  if (array_of_numbers != NULL && quantity_of_numbers > 0)

though it is just possible that the original is intended to work only
when there is a non-zero length array pointed to by array_of_numbers.
I.e. that you pass NULL rather than a zero length.  If that was the
idea, I would say it is not a good one.

it was a bug I remembered hunting in a program that calculated
the average number of bytes in packets received. If no packets
were received it gave a very odd answer.
 
B

Bill Cunningham

Richard Heathfield said:
double golden_mean(double *p, size_t len)
{
double left = 1.0;
double right = 1.0;
int counter = 0;
double ratio = 0.0;
double sum = 0.0;

/* use values if supplied */
if(len > 0)
{
left = p[0];
if(len > 1)
{
right = p[1];
}
}
/* make sure this is going to work! */
if(right + left == 0.0)
{
++right;
}
/* roll it up a bit */
for(counter = 0; counter < 100; counter++)
{
sum = left + right;
left = right;
right = sum;
}
/* calculate golden mean */
ratio = right / left;

return ratio;
}

I got this with your code Richard.

ratio.c:1: parse error before "size_t"
ratio.c: In function `golden_mean':
ratio.c:10: `len' undeclared (first use in this function)
ratio.c:10: (Each undeclared identifier is reported only once
ratio.c:10: for each function it appears in.)
ratio.c:12: `p' undeclared (first use in this function)

It wouldn't compile.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Richard Heathfield said:
double golden_mean(double *p, size_t len)
{ [snip]
}

I got this with your code Richard.

ratio.c:1: parse error before "size_t"
ratio.c: In function `golden_mean':
ratio.c:10: `len' undeclared (first use in this function)
ratio.c:10: (Each undeclared identifier is reported only once
ratio.c:10: for each function it appears in.)
ratio.c:12: `p' undeclared (first use in this function)

It wouldn't compile.

Try adding #include <stddef.h> to the top.

Richard posted a code fragment, not a complete program.

And I'd advise you to ignore it. It was a joke. You can't
*meaningfully* calculate a golden mean from a list of numbers.
 
B

Bill Cunningham

Try adding #include <stddef.h> to the top.

Richard posted a code fragment, not a complete program.

And I'd advise you to ignore it. It was a joke. You can't
*meaningfully* calculate a golden mean from a list of numbers.

Well here I have a lame attempt at trying to compute and average with
one parameter. The compile warned about comparison between pointer and
integer.

#include <stdio.h>

double a(double *num)
{
int i;
double sum = 0.0;
for (i = 0; i != num; ++i)
sum = sum + i[num];
}

Notice the second parameter has been removed. I would like the program to
keep track on its own how many doubles *p points to. I probably need another
counter.

Bill
 
K

Keith Thompson

Richard Heathfield said:
i has type int. num has type double *. Why are you comparing them?
It's like comparing semolina with the Suez Crisis.

Well, semolina tastes better.
 
N

Nick Keighley

    Well here I have a lame attempt at trying to compute and average with
one parameter. The compile warned about comparison between pointer and
integer.

#include <stdio.h>

double a(double *num)
{

I don't see how you expect this to work. What is num pointing to?
How does the function knwo when it has reached the last number.
    int i;
    double sum = 0.0;
    for (i = 0; i != num; ++i)

i is an int num is a double* you can't compare them.
There is no simple way to change this into code that
makes sense.[*]

        sum = sum + i[num];

why did you calculete i[num]?
}

Notice the second parameter has been removed.
why?

I would like the program to
keep track on its own how many doubles *p points to.
how?

I probably need another counter.

Doesn't that mean adding the parameter back in?

[*]

double a (double *num)
{
int i;
double sum = 0.0;
for (i = 0; i != *num; ++i)
sum = sum + i[num + 1];
}
 

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

error in code 48
avg error 30
pointer array problem? 7
seg fault 11
strange warning 192
C coding a rotate function (help me pleasee) 1
average problem 13
standard deviation 19

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top