Thread: function receive the array as a pointers

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    function receive the array as a pointers


    Hello:

    My question is why my average are all 43.6
    How to fix it?
    Thank

    #include<stdio.h>
    #include<stdlib.h>

    #define ROW 3
    #define COL 6

    void openFile(int [][COL]);
    void average (int [][COL], double *);
    void report(int [][COL], double);

    int main(void)
    {
    int table[ROW][COL];
    double avg;

    openFile(table);
    average (table, &avg);
    report(table, avg);

    return 0;
    }
    /**************************** OPEN FILE *************/

    void openFile(int table[][COL])
    {
    FILE *fp;

    int row=0;

    if(!(fp=fopen("ss.txt", "r")))
    {
    printf("\nCANNOT opening file ss.txt\n");
    exit(EXIT_FAILURE);
    }

    while(fscanf(fp, "%d%d%d%d%d%d", &table[row][0], &table[row][1], &table[row][2],
    &table[row][3], &table[row][4], &table[row][5]
    )==6)

    {row++;}

    fclose(fp);
    }

    /******* average *********/

    void average(int table[][COL], double *avg)
    {
    int row, col;

    for(row=0; row<ROW; row++)
    {
    double tot=0;

    for(col=1; col<COL; col++)
    tot += *(*(table + row)+col);

    *avg = tot / 5.0;
    }
    return;
    }

    /**************REPORT **************/

    void report(int table[][COL], double avg)
    {
    int row, col;

    for(row=0; row<ROW; row++)
    {
    printf("%4d", *(*(table+row)+0));

    for(col=1; col<COL; col++)
    {
    printf("%10d", *(*(table + row)+col));
    }
    printf("%10.2lf\n", avg);
    }

    return;
    }

    1111 92 87 100 10 20 43.60
    2222 50 63 48 36 10 43.60
    3333 20 55 57 47 25 43.60

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    It is to do with the way you are calling these three functions from main().
    Code:
    openFile(table);            // You read the data in this func.
    average (table, &avg); // You perform the averages calculation
    report(table, avg);       // You print out the report on screen
    You have evaluated three averages, each time storing the data in double avg. So the first time you evaluate the average, avg is equal to a number. But you overwrite that value with the next iteration of the loop.
    The reason that you have the same data three times is that you are trashing the data stored in avg each time you evaluate avg. You then pass the final value that was evaluated to report().

    To fix this program you could do one of a couple of things. Have the average function only evaluate the average of one line of data every time. Then loop between average(); and report(); i.e. you print out the data to screen every time you evaluate one line of data.

    or perhaps make avg an array. Store the results in a different element as you loop through your calculations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM