Also, you told me to fix that while loop to a for loop. At the moment it looks like this:
while (inFile >> matrix[numRows][0]) { printf ("%d\t", matrix[numRows][0]); for (j = 1; j < numCols; j++) { inFile >> matrix[numRows][j]; printf ("%d\t", matrix[numRows][j]); } int counter;
But I'm really struggling to get a grasp on what you mean... Could you possible show me how I should change it? Thank you in advance!
I thought you said earlier that you had gotten rid of this line? Check if you use it. If not, get rid of it.
int counter;
Yep, the line below declares and initializes in one statement, so you're good on the initialization part.
counters[2][2] = {0};
As for turning the for loop into a while loop, look carefully at the above code you've posted (I assume you realize you did not post the entire while loop):
while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}
int counter;
Recall that you have already changed numRows into i everywhere. That means you can use numRows now for something else and you need to put i somewhere in the loop below. Let's look at a for-loop:
for (int variable1 = 0; variable1 < variable2; variable1++)
Let's look at your data:
5
1 0 0
2 0 0
3 0 0
4 1 2
5 1 2
How many times are you going through the loop in this data? That needs to go in the for loop. Look at the for loop layout carefully.
When will you read in that 5 from the data file? Before the for loop or inside the for loop? variable2 clearly needs to have something in it for the loop to make sense.
What should the variable names for variable1 and variable2 be? Look at your code, particularly the matrix index variables.
As for this line:
inFile >> matrix[numRows][0];
decide where it needs to go and whether it still needs to be there. Hint: look at the inner for loop you have already. Can it be changed so you no longer need the line above?
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}
One last thing. You changed all of your numRows variables from the last thread into i variables in this thread when you knew you were changing from a while loop to a for loop. Be very careful not to get confused between the two versions. There are only a few lines you need to change. I've given you lots of hints. Give it a try. You'll undoubtedly, as JRM mentioned, get some errors and have to play around with the code more. He's correct. It's inevitable and it's not a bad thing. Save an original copy of your code so you can go back to it, then experiment!