1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with C programming lab

  1. Oct 10, 2009 #1
    1. The problem statement, all variables and given/known data
    Well the program has several functions but I just really need help with reading a text file and splitting the data in the file into a one-dimensional array and a two-dimensional array

    The text file is like this...
    21

    1110 1.5 5.4 6.0 9.8. 8.5 8.3 5.6 9.9
    1112 1.5 5.4 6.0 9.8. 8.5 8.3 5.6 9.9

    I have to somehow get the pin (1110 & 1112) into a 1-d array while putting the rest of the numbers into a 2-d array.

    This is what I have so far.

    /* =========================================================== */
    /* Reads data of a file and places the first column of info
    into a one dimensional array and the rest into a
    table.
    PRE: table = empty
    ary = empty
    POST:
    */
    int readTable (double scores[][MAX_COLS], double diverId[], int lineSize, char fileName[])
    {
    // Local Declarations
    FILE *fpScores;
    int r, c;

    // Statements
    fpScores = fopen( "scores.txt", "r" );

    int numOfDivers = 0;
    fscanf(fpScores, "%d", &numOfDivers);
    printf("The numbers of divers is %d.\n\n", numOfDivers);

    for (r = 0; r < MAX_ROWS; r++)

    printf("PIN DIF J1 J2 J3 J4 J5 J6 J7\n");
    printf("=== === == == == == == == ==\n");

    for (r = 0; r < MAX_ROWS; r++)
    {
    fscanf(fpScores, "%lf", &diverId[r]);
    printf("%.1lf ", diverId[r]);
    if( ( r + 1 ) % lineSize == 0 )
    printf( "\n");
    }

    printf("\n");

    ...end of function

    The problem is that the for loop goes through the file and makes every value have its own row like this:

    1111
    1.5
    5.4
    6.0
    etc..

    Any help with be appreciated. Thanks!
     
  2. jcsd
  3. Oct 10, 2009 #2
    Code (Text):

    [b]for (r = 0; r < MAX_ROWS; r++) // I assume this is mistakenly placed here (it shouldn't be here)[/b]

    printf("PIN DIF J1 J2 J3 J4 J5 J6 J7\n");
    printf("=== === == == == == == == ==\n");

    for (r = 0; r < MAX_ROWS; r++)
    {
    fscanf(fpScores, "%lf", &diverId[r]);
    printf("%.1lf ", diverId[r]);
    if( ( r + 1 ) % lineSize == 0 )
    printf( "\n");
    }
     
    I would run this code in a debugger and see why ( r + 1 ) % lineSize always returns 0.

    Personally though i would have actually used a for loop within a for loop for this..

    eg.
    Code (Text):

    for (r = 0; r < MAX_ROWS; r++)
    {
    for (l = 0; l < lineSize ; l++)
    {
    fscanf(fpScores, "%lf", &diverId[l]);
    printf("%.1lf ", diverId[l]);
    }
    printf("\n");
    }
     
    As the line is completely read, it will print a new line.
    It will then repeat for the next line.
    This is only a one dimension array though, so the array will get reused for each line (previous line data will be wiped from the array).
    I am not quite certain how you want the data stored within the array though..

    you could store it in a 2D array like diverId[r][l], it would still work the same. But i gather thats what scores[][] is for. You could get away with not using diverId[l] at all but it depends on the criteria of the program. I will leave that up to you :)
     
    Last edited: Oct 10, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with C programming lab
  1. C program help (Replies: 5)

  2. C++ program help! (Replies: 1)

  3. C program help needed (Replies: 3)

Loading...