Thread: inputing from file to multi-dimensional array

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    inputing from file to multi-dimensional array

    I am having trouble with finding the problem with program. It compiles but produces no output. If you have any ideas PLEASE let me know.

    Here is the C code:

    /* inputs data from file and calculates high, low, and avg score

    Written by: Joshua Kenney
    Date: 3.5.02

    */

    #include <stdio.h>

    #define MAX_COLS 6
    #define MAX_ROWS 6

    void getScores(int table[][MAX_COLS]);
    void calculateScores(int table[][MAX_COLS], int columnAvg MAX_COLS]);
    void printScores(int table[][MAX_COLS], int columnAvg[MAX_COLS]);

    int main(void)
    {
    /* local variables */

    int table[MAX_ROWS][MAX_COLS];
    int columnAvg[MAX_COLS] = {0};


    getScores(table);
    calculateScores(table, columnAvg);
    printScores(table, columnAvg);


    return 0;
    }

    /*getScores
    input scores from file and put them in two-dimensional array */

    void getScores(int table[MAX_ROWS][MAX_COLS])
    {
    FILE *scores;
    int row;
    int col;

    scores = fopen("scores.DAT", "r");

    if (!scores)
    {
    printf("Could not open input file\a\n");

    }
    else
    {
    for (row = 0; row < MAX_ROWS; row++)
    for(col = 0; MAX_COLS; col++)
    {
    fscanf(scores,"%d", table[row][col]);

    }
    }

    fclose(scores);

    return;
    }

    /*calculateScores
    sort and calculate the high, low, and avg score */

    void calculateScores(int table[][MAX_COLS], int columnAvg[MAX_COLS])
    {
    int row;
    int col;

    for (col = 0; col < MAX_COLS; col++)
    {
    for (row = 0; row < MAX_ROWS; row++)
    columnAvg[col] += table [row][col];
    columnAvg[col] /= MAX_ROWS;
    }
    }

    /*printScores
    print the table of scores */

    void printScores(int table[][MAX_COLS], int columnAvg[MAX_COLS])
    {
    int row;
    int col;

    printf("Student/t");
    for (row = 0; row < MAX_ROWS; row++)
    {
    printf("Quiz%d/t", row);

    }

    for (row = 0; row < MAX_ROWS; row++);
    {
    for (col = 0; col < MAX_COLS; col++);
    printf("%6d", table[row][col]);
    }

    printf("----------------------------\n");
    for(col = 0; col < MAX_COLS; col++)
    printf("%6.2f", columnAvg[col]);

    return;
    }




    And here is the .DAT file:

    1234 52 7 100 78 34
    2134 90 36 90 77 30
    3124 100 45 20 90 70

  2. #2
    Unregistered
    Guest
    There are two areas that I would look at:
    1.

    scores = fopen("scores.DAT", "r");

    if (!scores)
    {


    In all my years in teaching C programming I would not do this. It is not that simple in C

    Try:

    if ((scores = fopen("scores.dat","r")) == NULL)
    {

    }
    else
    ....

    2. Since you are using functions while don't you pass the file as parameters to the functions. This could be another problem as well.

    Otherwise, you code looks ok. (I do see some syntax errors but you probably just typed it in to fast, right???


    Mr. C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM