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?

Recommended Answers

All 5 Replies

while ( a != 0)
{
total += a;
}

This bit of code doesn't make sense. "a" is an array of doubles. So when you say "a", "a" is a reference/memory address. If you want to access the value stored in "a" you have to change the code to something like this:

while ( a[index] != 0)
{
total += a[index];
}

Where 'index' above is some valid index into the array, so if your array is of size 10, 0-9 would all be valid indexes. But using a while loop in this context doesn't make sense either, so I suggest that you use a for loop and do something like for(int i = 0; i < ARRAY_SIZE; i++) and put your calculations inside of that for loop.

commented: good +7

JC covered it pretty well.

but let me comment on your indentation. it's a pet peeve.

when you use a brace '{' to open a block of code, *EVERY* line after that is indented one level to the right, until another brace is found. at which point a closing brace '}' returns indentation of subsequent code back to the previous level (to the left), while another opening brace '{' indents the subsequent code one more level to the right


what you have here (not even counting the errors) is terrible style and makes it really hard to read , especially in large programs

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 */
}

I edited my function to use a for loop.
But I'm still getting a 0 at output for my average.
Could someone take a look at my program and see what I did wrong please?

double tableAverage(double a[], int num)
{
 // Declarations
 double avg = 0;
 double total = 0;
 int i;                 // Counter


       for(i = 0; i <= num; i++)
       {
               total += a[MAXVALS];
               #ifdef DEBUG
               printf("debug: total = %f\n", total);
               #endif
       }


        avg = total/num;

       return avg;
}

Look closely into you for loop. This is the statement that you have written

total += a[MAXVALS];

What this means is that you are adding the value stored in a[100] num times. Is this what you want to do ?

On line 11, you're adding the same value (one past the end of the array, not what you want) over and over again. You want to add a[i] . Double check your for loop there too, count the number of elements that loop will step through.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.