Tips for Reading and Splitting Data in C Programming Lab Homework

Click For Summary
SUMMARY

The discussion focuses on reading a text file in C and splitting its data into a one-dimensional array for diver IDs and a two-dimensional array for scores. The provided code snippet demonstrates an initial attempt to read the data, but it incorrectly places each value into separate rows instead of organizing them into the intended arrays. The suggested solution includes using nested loops to correctly read and store the data, ensuring that the first column is captured in a one-dimensional array while the remaining values populate a two-dimensional array.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with file handling in C using the FILE structure
  • Knowledge of arrays, specifically one-dimensional and two-dimensional arrays
  • Experience with loops and conditional statements in C
NEXT STEPS
  • Learn about C file I/O operations, specifically using fopen, fscanf, and fclose
  • Explore multi-dimensional arrays in C and their memory allocation
  • Investigate debugging techniques in C to troubleshoot array indexing issues
  • Study best practices for reading structured data from files in C programming
USEFUL FOR

C programmers, computer science students, and anyone working on data processing tasks in C who need to efficiently read and manipulate structured data from text files.

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:

Similar threads

Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K