Problem when data is printed.

This is a discussion on Problem when data is printed. within the C Programming forums, part of the General Programming Boards category; This is what I have so far for code. I am trying to read the file.. that contains data that ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Problem when data is printed.

    This is what I have so far for code. I am trying to read the file.. that contains data that looks like this.

    Std# T1 T2 T3 T4 T5
    1234 54 65 44 44 78
    4561 78 78 87 87 87

    and so on. My problem is the idea is to set it up to be avaliable to recieve data for up to 40 students but right now I only have 15. When I go to print the Raw data, it prints a bunch of garbage and wierd numbers for all the other places in the array. I thought I had it so it would only read the data. Also my Average part gives me a floating exception point error.. I would appreciate anyones help or ideas. I have tried to comment as much as I could to make things easy to read.

    If you have any questions my email is Fuzzyman81@aol.com and your help would be greatly appreciated.

    Thank you..

    Ryan P



    /* Input: This program is divided into three parts.
    Part 1: p5.h : is the header of the program.
    Part 2: p5m.c : is the function MAIN of the program.
    Part 3: p5f.c : is the series of companion functions.

    The program will use data from the file p5.in
    Makefile is then used to compile the three together.
    */
    /* Output: The output for the program will be sent directly to the monitor.
    */


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

    #define MAX_ROWS 40
    #define MAX_COLS 5

    // Prototype Declarations

    int getStuData (int StdNum[], int table[MAX_ROWS][MAX_COLS],
    float StdAverage[MAX_ROWS]);
    void printRawData(int size, int StdNum[], int table[][MAX_COLS]);
    void testAverage (int table[][MAX_COLS], float testAve[], int size);
    void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[],
    float testAve[], int size);

    int main(void)
    {
    //Local Definitions

    int size;
    int table [MAX_ROWS][MAX_COLS];
    int StdNum [MAX_ROWS];
    float StdAverage [MAX_ROWS] = {0};
    float testAve [MAX_COLS] = {0};

    //Function Calls

    size=getStuData (StdNum, table, StdAverage);
    printRawData(size, StdNum, table);
    testAverage (table, testAve, size);
    printResults (table, StdNum, StdAverage, testAve, size);

    return 0;
    }
    //end of Main


    /*------------ getStuData-------------------
    This function reads the file p5.in
    and then stores the student numbers into a
    array.
    */

    int getStuData (int StdNum[MAX_ROWS], int table[MAX_ROWS][MAX_COLS],
    float StdAverage[MAX_ROWS])
    {
    // Local Definitions

    FILE *fp;
    int row=0;
    int col=0;
    int rowAve;
    int Okscan;

    if (!(fp = fopen ("p5.in", "r")))
    printf("Error opening File\a\a\n") , exit (100);

    while(row<MAX_ROWS)
    {
    rowAve=0;
    for(col=0; col<6; col++)
    {
    if (col==0)
    {
    Okscan=fscanf(fp, "%d", &StdNum[row]);
    if(!Okscan)
    break;
    }
    if (col!=0)
    {
    fscanf(fp, "%d", &table[row][col-1]);
    rowAve+=table[row][col-1];
    }
    }
    if(Okscan)
    {
    StdAverage[row]=(rowAve/5);
    row++;
    }
    else
    break;
    }
    fclose(fp);
    return row;
    }


    void printRawData(int size, int StdNum[], int table[][MAX_COLS])
    {
    //Local Definitions
    int row=0;
    int col=0;

    system("clear");

    printf("\n\t\t RAW DATA REPORT\n");
    printf("Student # Test1 Test2 Test3"
    " Test4 Test5\n");
    printf("---------------------------------------"
    " ----------------");
    for(row=0; row < size; row++)
    {
    printf("\n%7d", StdNum[row]);
    for(col = 0; col<MAX_COLS; col++)
    printf("%6d", table[row][col]);
    }
    printf("\n\n\nDepress <enter> key to continue: ");
    getc(stdin);
    fflush(stdin);
    }



    /*--------------testAverage--------------
    This function will average the students scores
    for each test individually.
    */

    void testAverage (int table[][MAX_COLS], float testAve[], int size)
    {
    //Local Definitions
    int row=0;
    int col=0;

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


    /*-------------printResults---------------
    This function will display the Average
    data report.
    */

    void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[],
    float testAve[], int size)
    {
    int row=0;
    int col=0;
    system("clear");
    printf("\t\t STATISTICAL REPORT");
    printf("\nStudent Test1 Test2 Test3"
    " Test4 Test5 Average");
    printf("\n---------------------------------------"
    " ----------------");

    for(row=0; row < size; row++)

    {
    printf("\n%7d", StdNum[row]);

    for(col = 0; col<MAX_COLS; col++)
    printf("%6d", table[row][col]);
    printf(" | %6.2f", StdAverage[row]);
    }

    printf("****************************************** **********\n");
    printf("Average: ");
    for(col = 0; col<MAX_COLS; col++)
    printf("%6.2f", testAve[col]);
    printf("\n\n\nDepress <enter> key to continue: ");
    getc(stdin);
    fflush(stdin);

    return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Help

    Someone please help if you have any idea.. I have been racking my brain all night... and all I have gotten is a headache.

    Ryan

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    Hi ryan i have a question why not use a linked list instead of arrays they are easier and alot more flexible to work with and u have no compile time imposed limit, you can have as many as u want cause linked list can grow on demand and they are easier if you need modify the data structure (add more data generally)later on and easier to reorganized if necessary.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    It's easy.. I am not familiar with linked lists... and for my wife to use the program, it is easier to set up her data to be used with arrays.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Older compilers may have trouble declaring large arrays within main(). I moved the arrays above main(). I also changed testAverage().
    Code:
    /* Input: This program is divided into three parts.
    Part 1: p5.h : is the header of the program. 
    Part 2: p5m.c : is the function MAIN of the program. 
    Part 3: p5f.c : is the series of companion functions. 
    
    The program will use data from the file p5.in 
    Makefile is then used to compile the three together. 
    */ 
    /* Output: The output for the program will be sent directly to the monitor. 
    */ 
    
    
    #include <stdio.h> 
    #include <stdlib.h>
    
    #define MAX_ROWS 40
    #define MAX_COLS 5
    
    // Prototype Declarations 
    
    int getStuData (int StdNum[], int table[MAX_ROWS][MAX_COLS], 
    float StdAverage[MAX_ROWS]); 
    void printRawData(int size, int StdNum[], int table[][MAX_COLS]); 
    void testAverage (int table[][MAX_COLS], float testAve[], int size); 
    void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[], 
    float testAve[], int size); 
    
    int table [MAX_ROWS][MAX_COLS];
    int StdNum [MAX_ROWS];
    float StdAverage [MAX_ROWS] = {0}; 
    float testAve [MAX_COLS] = {0}; 
    int main(void)
    { 
    //Local Definitions 
    
    int size; 
    
    //Function Calls 
    
    size=getStuData (StdNum, table, StdAverage); 
    printRawData(size, StdNum, table); 
    testAverage (table, testAve, size); 
    printResults (table, StdNum, StdAverage, testAve, size); 
    
    return 0; 
    } 
    //end of Main 
    
    
    /*------------ getStuData------------------- 
    This function reads the file p5.in 
    and then stores the student numbers into a 
    array. 
    */ 
    
    int getStuData (int StdNum[MAX_ROWS], int table[MAX_ROWS][MAX_COLS], 
    float StdAverage[MAX_ROWS]) 
    { 
    // Local Definitions 
    
    FILE *fp; 
    int row=0; 
    int col=0; 
    int rowAve; 
    int Okscan; 
    
    if (!(fp = fopen ("p5.in", "r")))
    printf("Error opening File\a\a\n") , exit (100);
    
    while(row<MAX_ROWS) 
    { 
    rowAve=0; 
    for(col=0; col<6; col++) 
    { 
    if (col==0) 
    { 
    Okscan=fscanf(fp, "%d", &StdNum[row]); 
    if(!Okscan) 
    break;
    } 
    if (col!=0) 
    { 
    fscanf(fp, "%d", &table[row][col-1]); 
    rowAve+=table[row][col-1]; 
    } 
    }
    if(Okscan)
    { 
    StdAverage[row]=(rowAve/5); 
    row++; 
    } 
    else 
    break; 
    } 
    fclose(fp);
    return row;
    } 
    
    
    void printRawData(int size, int StdNum[], int table[][MAX_COLS]) 
    { 
    //Local Definitions 
    int row=0; 
    int col=0; 
    
    system("clear");
    
    printf("\n\t\t RAW DATA REPORT\n"); 
    printf("Student # Test1 Test2 Test3" 
    " Test4 Test5\n"); 
    printf("---------------------------------------" 
    " ----------------"); 
    for(row=0; row < size; row++) 
    { 
    printf("\n%7d", StdNum[row]); 
    for(col = 0; col<MAX_COLS; col++) 
    printf("%6d", table[row][col]); 
    } 
    printf("\n\n\nDepress <enter> key to continue: "); 
    getc(stdin); 
    fflush(stdin); 
    } 
    
    
    
    /*--------------testAverage-------------- 
    This function will average the students scores 
    for each test individually. 
    */ 
    
    void testAverage (int table[][MAX_COLS], float testAve[], int size) 
    { 
    //Local Definitions 
    int row=0; 
    int col=0; 
    
    for(col = 0; col<MAX_COLS; col++)
    {
    for(row=0; row < size; row++)
    testAve[col] += table[row][col];
    testAve[col] /= size;
    } 
    } 
    
    
    /*-------------printResults--------------- 
    This function will display the Average 
    data report. 
    */ 
    
    void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[], 
    float testAve[], int size) 
    { 
    int row=0; 
    int col=0; 
    system("clear"); 
    printf("\t\t STATISTICAL REPORT");
    printf("\nStudent Test1 Test2 Test3" 
    " Test4 Test5 Average"); 
    printf("\n---------------------------------------" 
    " ----------------"); 
    
    for(row=0; row < size; row++) 
    
    { 
    printf("\n%7d", StdNum[row]); 
    
    for(col = 0; col<MAX_COLS; col++) 
    printf("%6d", table[row][col]); 
    printf(" | %6.2f", StdAverage[row]); 
    } 
    
    printf("\n ****************************************************\n");
    printf("Average: "); 
    for(col = 0; col<MAX_COLS; col++) 
    printf("%6.2f", testAve[col]); 
    printf("\n\n\nDepress <enter> key to continue: "); 
    getc(stdin); 
    fflush(stdin); 
    
    return; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  2. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 03:06 AM
  3. Problem passing data between functions
    By manutdfan in forum C Programming
    Replies: 9
    Last Post: 12-10-2006, 04:32 PM
  4. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  5. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM

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