problem with mulitdimensional array as function arguements

This is a discussion on problem with mulitdimensional array as function arguements within the C Programming forums, part of the General Programming Boards category; This is the problem statement for this assignment: As warm-up, write a program that displays the sum and average of ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    problem with mulitdimensional array as function arguements

    This is the problem statement for this assignment:

    As warm-up, write a program that displays the sum and average of both the rows and columns of a
    rectangular double array named table. Do not use a global: declare table in main. Define table’s
    dimensions statically using the preprocessor, defaulting to 5 × 3. Your solution must include three
    functions: readTable, sumAveRow, and sumAveColumns.
    I suggest this signature for sumAveRow: sumAveRow(int r, double t[][COLS], double* sum, double*
    average).

    here is my code so far:
    Code:
    #include <stdio.h>
    
    #define COLS 3
    #define ROWS 5
    
    void sumAveRow(int r, double t[][COLS], double* sum, double* average);
    void sumAveCol(int c, double t[][COLS], double* sum, double* average);
    void readTable(int r, int c, double t[][COLS]);
    
    int main() {
    
            double table[ROWS][COLS];
            double sum, average;
            /* get inputs and store in table */
    
            readTable(ROWS, COLS, table[][COLS]);
            sumAveRow(ROWS, table[], &sum, &average);
            return (0);
    }
    
    void readTable(int r, int c, double t[][COLS]) {
    
            int i = 0;
            int j = 0;
            double data = 0;
    
            while ( i > ROWS) {
                    printf("For row %d,", i);
                    while ( j < COLS ) {
                            printf("    enter a double: ");
                            scanf("%lf", &data);
                            t[i][j] = data;
                            j = j + 1;
                    }
                    i = i + 1;
                    j = 0;
            }
    }
    
    void sumAveRow(int r, double t[][COLS], double* sum, double* average) {
    
            int i = 0;
            int j = 0;
            sum = 0;
            average = 0;
            for (i = 0; i < r; i++) {
                    *sum = t[i][j];
                    }
            *average = (*sum / r);
            printf("Row %d:  Sum = %.2lf; Average = %.2lf", i, *sum, *average);
    }
    I know it's not complete, but my real concern is that I keep getting syntax errors in the function calls for readTable and sumAveRow:
    error: syntax error before ']' token. What can I do to fix this?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For starters, you'd want to call your functions like this.
    Code:
            readTable(ROWS, COLS, table);
            sumAveRow(ROWS, table, &sum, &average);
    Then,
    Code:
            while ( i < r) {
                    printf("For row %d,", i);
                    while ( j < c ) {
    And
    Code:
     *sum = 0;
            *average = 0;
    [edit]Then, I think you'd have better luck with a function like this:
    Code:
    void sumAveRow(int r, int c, double t[][COLS], double* sum, double* average)
    {
       int i;
       *sum = 0;
       *average = 0;
       for ( i = 0; i < c; i++ )
       {
          *sum += t[r][i];
       }
       *average = *sum / c;
       printf("Row %d:  Sum = %.2lf; Average = %.2lf", r, *sum, *average);
    }
    Which of course means you'd call the function like this.
    Code:
       sumAveRow(0, COLS, table, &sum, &average);
    Last edited by Dave_Sinkula; 12-07-2005 at 10:57 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Problem with array of function pointers
    By silk.odyssey in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2004, 07:14 PM
  5. Replies: 6
    Last Post: 10-21-2003, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21