avg

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
L

Lew Pitcher

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, the error message suggests that you examine the line
return num / n;

It tells you that, in that line, one or more of the division operands are
incorrect.

That means that either
num
or
n
cannot participate in a division.

What type of object is
num
?

What type of object is
n
?

The reason your compiler complains is because
num
is not of a type that can participate in a division operation.
The
num
object is a /pointer/ type. Pointers can't be divided.

So, what /should you have written?


HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
O

osmium

Bill Cunningham 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 /

No, you are not close! Forget the indicated errors, the logic is no good.
What is the ONE function of the variable i? You are using it to try to do
two entirely different things.

hint:
double sum = 0.0;
 
K

KevinSimonson

= 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;
=
=
=}

Bill, Lew Pitcher gave you some good advice for understanding
what the compiler is trying to tell you, but he didn't mention your
use of the variable <i>, and I'm pretty sure that there's a logical
error there that will plague you once you've fixed the semantical er-
ror. What is the purpose of variable <i>? You appear to be using it
in your <for> loop to index through the array, but you're also using
it as a sum of all the variables in the array. Are you sure you want
to use it for both of those?

For example, if <n> is 20, and if <num[0]> is 35, then <i> is go-
ing to have value 20 after the first time through the loop, and the
19 other elements of <num> are never going to be evaluated, since the
<for> loop ends when <i> is greater than or equal to 20. I don't
think that's what you want.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
M

Morris Keesan

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

No, you're not even remotely close.
Try to do a little more desk work, i.e. looking at your code and thinking
about it on your own, before you embarrass yourself like this in public.
#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
 
B

Bill Cunningham

Morris Keesan said:
No, you're not even remotely close.
Try to do a little more desk work, i.e. looking at your code and thinking
about it on your own, before you embarrass yourself like this in public.

Well I googled some sites and tried to look for average calculation
examples. I found a couple. But they were all run from main and nothing like
what I want. So I tried to find something and messed it up. So I thought I'd
try usenet.

Bill
 
B

Bill Cunningham

No, you are not close! Forget the indicated errors, the logic is no good.
What is the ONE function of the variable i? You are using it to try to do
two entirely different things.

hint:
double sum = 0.0;

Ok I need another loop? Why is sum declared 0.0 ?

Bill
 
L

Lew Pitcher

Well I googled some sites and tried to look for average calculation
examples. I found a couple. But they were all run from main and nothing
like what I want. So I tried to find something and messed it up. So I
thought I'd try usenet.

Bill, can you explain what you mean by "average"?
If you didn't have a computer, but just had a list of numbers written down
on a piece of paper, how would /you/ compute the "average" of that list?

Your explanation of how to take an average manually should sound something
like: "Starting with a sum of 0, add each successive number to the sum.
When you run out of numbers, divide the sum by the number of numbers that
were summed".

Now, that makes a pretty good description of what you want your /function/
to do. Write /that/ code.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Bill Cunningham

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

No, you are not close! Forget the indicated errors, the logic is no good.
What is the ONE function of the variable i? You are using it to try to do
two entirely different things.

hint:
double sum = 0.0;

Well I really don't think I need to figure out how many doubles num
points to because that is what i is for. That value is entered. It's a
summation of the doubles that num points to that I think I need to achieve.
Bill
 
D

dada

Bill said:
Well I googled some sites and tried to look for average calculation
examples. I found a couple. But they were all run from main and nothing like
what I want. So I tried to find something and messed it up. So I thought I'd
try usenet.

Bill


Bill,
Maybe you should try to start with some pseudo code...

Something like this (this is a made up syntax that I just came up with,
just for you):


Function : Average
Purpose : To average a passed in array of doubles and return the result
Inputs : array of doubles
number of values in the array
Outputs : Returns the computed average of the array

Pseudo code :

Function average ( double : values array ; int : numValues )
: return double

begin

Create a double called sum and initialize it to 0.0
loop through all of the values from 0 to numValues - 1 using

iterator called "index"
set sum = sum + values [ index ]
end loop ;

set return value to sum divided by numValues

end function average


Play computer, run through your Pseudo code by hand to make sure it will
function as expected, and THEN translate it to C code after you
understand what it is doing.

Hope this helps,
Joe
 
O

osmium

Bill Cunningham said:
Ok I need another loop? Why is sum declared 0.0 ?

No, one loop is enough. sum is initialized to 0.0 because it emphasizes
that the know that sum is a double and not an int. It would probably work
with simply 0. You start at zero for the same reason that you would take a
banana someone left on a scale before weighing whatever it is that YOU
wanted to weigh.
 
O

osmium

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

No, you are not close! Forget the indicated errors, the logic is no
good. What is the ONE function of the variable i? You are using it to
try to do two entirely different things.

hint:
double sum = 0.0;

Well I really don't think I need to figure out how many doubles num
points to because that is what i is for. That value is entered. It's a
summation of the doubles that num points to that I think I need to
achieve.

If there is a question there I couldn't find it, so I have no comment.
 
B

Bill Cunningham

Function : Average
Purpose : To average a passed in array of doubles and return the result
Inputs : array of doubles
number of values in the array
Outputs : Returns the computed average of the array

Pseudo code :

Function average ( double : values array ; int : numValues )
: return double

begin

Create a double called sum and initialize it to 0.0
loop through all of the values from 0 to numValues - 1 using
iterator called "index"
set sum = sum + values [ index ] /*IS this a lvaule or
end loop ; rvalue */

set return value to sum divided by numValues

end function average


Play computer, run through your Pseudo code by hand to make sure it will
function as expected, and THEN translate it to C code after you understand
what it is doing.

Hope this helps,
Joe
 
B

Bill Cunningham

dada said:
Bill said:
Well I googled some sites and tried to look for average calculation
examples. I found a couple. But they were all run from main and nothing
like what I want. So I tried to find something and messed it up. So I
thought I'd try usenet.

Bill


Bill,
Maybe you should try to start with some pseudo code...

Something like this (this is a made up syntax that I just came up with,
just for you):


Function : Average
Purpose : To average a passed in array of doubles and return the result
Inputs : array of doubles
number of values in the array
Outputs : Returns the computed average of the array

Pseudo code :

Function average ( double : values array ; int : numValues )
: return double

begin

Create a double called sum and initialize it to 0.0
loop through all of the values from 0 to numValues - 1 using
iterator called "index"
set sum = sum + values [ index ]
end loop ;

set return value to sum divided by numValues

end function average


Play computer, run through your Pseudo code by hand to make sure it will
function as expected, and THEN translate it to C code after you understand
what it is doing.

Hope this helps,
Joe
#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;
}
p.c: In function `Avg':
p.c:8: subscripted value is neither array nor pointer

compiler errors. The line I am messing up on is the lvalue or the rvalue I
don't know which way to read it.

Bill
 
B

Ben Bacarisse

Lew Pitcher said:
Bill, can you explain what you mean by "average"?
If you didn't have a computer, but just had a list of numbers written down
on a piece of paper, how would /you/ compute the "average" of that list?

Your explanation of how to take an average manually should sound something
like: "Starting with a sum of 0, add each successive number to the sum.
When you run out of numbers, divide the sum by the number of numbers that
were summed".

This is a computer programmer speaking. Ask most non-programmers to
average a list of numbers and they don't "start with a sum of 0" --
they just add the numbers up. And the won't "add each successive
number" -- the most common method it to add parts of each number (the
digits) but some people will do other things like look for close sums
and keep a running error. It takes a lot or artificiality to get most
people to act like a computer.
 
L

Lew Pitcher

dada said:
Bill said:
No, you're not even remotely close.
Try to do a little more desk work, i.e. looking at your code and
thinking
about it on your own, before you embarrass yourself like this in
public.

Well I googled some sites and tried to look for average calculation
examples. I found a couple. But they were all run from main and nothing
like what I want. So I tried to find something and messed it up. So I
thought I'd try usenet.

Bill


Bill,
Maybe you should try to start with some pseudo code...

Something like this (this is a made up syntax that I just came up with,
just for you):


Function : Average
Purpose : To average a passed in array of doubles and return the result
Inputs : array of doubles
number of values in the array
Outputs : Returns the computed average of the array

Pseudo code :

Function average ( double : values array ; int : numValues )
: return double

begin

Create a double called sum and initialize it to 0.0
loop through all of the values from 0 to numValues - 1 using
iterator called "index"
set sum = sum + values [ index ]
end loop ;

set return value to sum divided by numValues

end function average


Play computer, run through your Pseudo code by hand to make sure it will
function as expected, and THEN translate it to C code after you
understand what it is doing.

Hope this helps,
Joe
#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


Bill, n is neither an array nor a pointer. Neither is i. Why are you
treating them as such?


rvalue?
return num/n; how is it read? */
}

double Avg(double num[], int n)
/* num[] is a table of doubles that we will find the average of */
/* n is a count of the number of doubles in the table */
{
double sum = 0.0; /* starting with a sum of 0 */
int index;

/* successively add each number to the sum until we run out of numbers */
for (index = 0; index < n; ++index)
sum = sum + num[index];

/* average is the sum of the numbers divided by the number of numbers */
return sum / n;
}


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
M

Mara Guida

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

Bill Cunningham

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
 
T

Thomas Matthews

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.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top