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