I'm trying to repair a program that computes averages of several values inputted by the user. All values are scanned on one line and taken as an array. My program is nowhere near complete and I'm trying to fix it up little by little.
The one part that I am currently stuck on is with the function that will compute the average of all values. The function is called tableAverage().
I included all headers, functions, and the main in one file for now so you guys can easily test it out.
When I compile this, I am given the error:
averages.c: In function ‘tableAverage’:
averages.c:104: error: invalid operands to binary + (have ‘double’ and ‘double *’)
Here's my code:
/* File: averages.c */
/* A program to read values, compute their averages, and to print
* the values greater than or less than the average.
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXVALS 100 /* max number of values we can process */
#define DEBUG
int tableFill(double a[], int max);
void tablePrint(double a[], int num);
double tableAverage(double a[], int num);
int tableMatchingElements(double a[], int num, double target);
int main()
{
// Declarations
double table[MAXVALS]; /* array to hold input values */
int n; /* number of values in "table" */
double average; /* average input value */
int equal; /* number of values the same as average */
// Get the number of inputs
n = tableFill(table, MAXVALS);
#ifdef DEBUG
printf("debug: n = %d\n",n);
#endif
// Get the average of all inputs
average = tableAverage(table, n);
#ifdef DEBUG
printf("debug: average = %f\n");
#endif
// Print the average
printf("Average of the %d values read is: %lf\n", n, average);
equal = tableMatchingElements(table, n, average);
#ifdef DEBUG
printf("debug: equal = %d\n", equal);
#endif
printf("There are %d values equal to the average.\n", equal);
printf("The values greater than the average:\n");
tablePrint(table, n);
printf("The values less than the average:\n");
tablePrint(table, n);
}
int tableFill(double a[], int max)
{
double next; /* next input value */
int r; /* return from trying to read values */
int cnt = 0; /* count of values read */
// While user has more input
while ((r = scanf("%lf", &next)) != EOF)
{
if (r != 1) /* bad return from scanf */
{
printf("Error in the input after reading %d values.\n",
cnt);
break;
}
if (cnt == max) /* no room to store this value */
{
printf("Array full after reading %d values.\n", cnt);
break;
}
a[cnt++] = next; /* save element in array */
}
return cnt;
}
void tablePrint(double a[], int num)
{
int i;
for (i = 0; i < num; i++)
printf("%f\n", a[i]);
}
double tableAverage(double a[], int num)
{
// Declarations
double avg = 0;
double total = 0;
while ( a != 0)
{
total += a;
avg = total/num;
}
return avg;
/* return 0.0; * doesn't really compute the average */
}
int tableMatchingElements(double a[], int num, double target)
{
return 0; /* number of values equal to the target */
}
Does anyone know what seems to be the problem?