Tips for Reading and Splitting Data in C Programming Lab Homework

In summary, the conversation discusses how to read a text file and split the data into a one-dimensional array and a two-dimensional array. The code provided shows an attempt at achieving this, but there is a problem with the for loop that results in each value having its own row. A suggestion is made to use a for loop within a for loop to solve this issue. It is also mentioned that the data can be stored in a 2D array or a different approach can be taken depending on the program's criteria.
  • #1
vuongstran
1
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
  • #2
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:
  • #3


I would suggest breaking down the problem into smaller, more manageable steps. First, focus on reading the text file and storing the data in a one-dimensional array. This can be achieved by using a loop to read each line of the file and store it in the array. Then, you can move on to splitting the data into a two-dimensional array. This can be done by using a loop to read each line of the file and then splitting the data into columns, which can then be stored in the two-dimensional array. You may also need to use additional variables to keep track of the number of rows and columns in the array.

Additionally, I would recommend using more descriptive variable names to make your code easier to understand and follow. For example, instead of using "r" and "c" for row and column, you could use "row" and "column" to make the code more readable.

Lastly, it may be helpful to use comments in your code to explain what each section is doing. This can make it easier for others (and even yourself) to understand and troubleshoot the code.

I hope this helps and good luck with your lab!
 

FAQ: Tips for Reading and Splitting Data in C Programming Lab Homework

What is C programming?

C programming is a high-level programming language that was developed in the 1970s. It is a powerful and widely used language for developing operating systems, databases, and other complex software applications.

What is the purpose of a C programming lab?

The purpose of a C programming lab is to provide hands-on experience and practice with coding in the C language. It allows students to apply the concepts learned in lectures and gain a better understanding of the language through practical application.

How can I prepare for a C programming lab?

To prepare for a C programming lab, it is important to review the lecture material and understand the basic syntax and concepts of the language. It can also be helpful to practice coding exercises and familiarize yourself with the tools and software used in the lab.

What kind of assignments can I expect in a C programming lab?

In a C programming lab, you can expect assignments that involve writing code to solve problems and create programs. These assignments may range from simple exercises to more complex projects that require knowledge of different C language features.

What resources are available for help with C programming labs?

There are many resources available for help with C programming labs, including textbooks, online tutorials, and forums where you can ask for assistance. Your instructor or teaching assistant may also be available for guidance and feedback on your assignments.

Similar threads

Back
Top