0

I'm trying to store 3 sets of 5 double numbers from user input. I need to store the information in a 3 x 5 array and compute the average of each set of five values.

I can't figure out how to fix two errors.

First error: hw9.c:27:2 error: incompatible type for argument 1 of 'set_average' set_average (array[ROW][COL]); ^

Second error: hw9.c:8:6: note: expected 'double (*)[5]' but argument is of type 'double' void set_average(double array[ROW][COL]);

Thanks for any help and suggestions.

#include <stdio.h>
#define ROW 3
#define COL 5
void set_average(double array[ROW][COL]);
void all_average(double array[ROW][COL]);
void find_largest(double array[ROW][COL]);
int main(void)
{
    double array[ROW][COL];
    int i, j;

    printf("Enter three sets of five double numbers.\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 5; j++)
        {
            printf("Enter elements until done.\n");
            printf("Enter %d%d: ",i+1,j+1);
            scanf("%le", &array[i][j]);
        }   
    printf("Done entering numbers.\n");
    printf("Now it's time to compute the average of each set of five values\n");

    set_average (array[ROW][COL]);

    return 0;
}

void set_average(double array[ROW][COL])
{
    int r;      //row
    int c;
    double sum;
    double avg; //average
        for (r = 0; r < ROW; r++)
            for (c = 0; c < COL; c++)
            {
                sum += array[r][c];
            }
    avg = sum / 5;
    printf("The average is %le\n", avg);
}

3 Answers3

0

You are calling function as

set_average (array[ROW][COL]);

instead of

set_average (array);

Function is defined as void set_average(double array[ROW][COL]) so it expects an array of ROW. Each has an array of COL items which are doubles. Error was reported because function was called with just one double as argument.

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
0

you need to pass full array to the function

set_average (array);

As you are passing

set_average (array[ROW][COL]);

it will send only single value i.e array[3][5]

In set_average function you have not initialize value of double sum; . Initialize sum with zero, otherwise you will get wrong Output.

Himanshu
  • 4,327
  • 16
  • 31
  • 39
0
try this:
------------------
#include <stdio.h>
#define ROW 3
#define COL 5

void set_average(double array[][COL]);

int main(void)
{
        double array[ROW][COL];
        int i, j;

        printf("Enter three sets of five double numbers.\n");
        for (i = 0; i < 3; i++)
                for (j = 0; j < 5; j++)
                {
                        printf("Enter elements until done.\n");
                        printf("Enter %d%d: ",i+1,j+1);
                        scanf("%le", &array[i][j]);
                }
        printf("Done entering numbers.\n");
        printf("Now it's time to compute the average of each set of five values\n");

        set_average( array );

        return 0;
}

void set_average( double array[ ][5] )
{
        int r;      //row
        int c;
        double sum;
        double avg; //average
        for (r = 0; r < ROW; r++)
                for (c = 0; c < COL; c++)
                {
                        sum += array[r][c];
                }
        avg = sum / 5;
        printf("The average is %le\n", avg);
}
rabi shaw
  • 441
  • 1
  • 3
  • 14