Tips for Reading and Splitting Data in C Programming Lab Homework

AI Thread Summary
The discussion focuses on reading a text file in C and splitting the data into a one-dimensional array for diver IDs and a two-dimensional array for scores. The provided code attempts to read the data but incorrectly places each value into separate rows instead of grouping them. Suggestions include using nested loops to read each line correctly and storing the data in the appropriate arrays. A key point is the need to ensure that the data structure used aligns with the program's requirements. Proper debugging and restructuring of the loop logic are essential for achieving the desired output.
vuongstran
Messages
1
Reaction score
0

Homework Statement


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!
 
Physics news on Phys.org
Code:
[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:
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 that's 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:
Back
Top