0

So I find the number of lines in an unknown matrix and then want to use this number to scan through the matrix the correct number of times and display the matrix at the end. I need to find the dimension as i want to go on to find the determinant but this is my code so far.

Anyway the problem is that the "dim" integer doesn't seem to transfer as it prints out a bunch of crazy numbers

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(int argc, char* argv[])
{
    FILE       *input;
    int        i, j, temp, dim;  
    float      fullmatrix[dim][dim];
    const char inp_fn[]="matrix.dat";

/*Open File*/
input = fopen(inp_fn, "r");
dim = 0;

while (EOF != (temp = fgetc(input)))
{
    if (temp=='\n')
    {
        ++dim;
    }
}

if( (input != (FILE*) NULL) )
{
    for(i=0; i<=dim; i++)
    {
        for(j=0; j<=dim; j++)
        {
            fscanf(input, "%f", &fullmatrix[i][j]);
            printf("%f ", fullmatrix[i][j]);
        }
        printf("\n");
    }
    fclose(input);
}
else
{
    printf("Could not open file!\n");
}

return(0);
}

I'm pretty new to this so i'm probably being stupid.

Carterini
  • 165
  • 7
  • 1
    How would it know what the dimensions of the matrix are, before dim has any value? ..... – Bart Nov 02 '13 at 18:10
  • This is what I think i'm confusing, how can i get it to do the While part first to increase the dim, THEN do the rest? It seems to do it all at once. – Carterini Nov 02 '13 at 18:12
  • `int i, j, temp, dim; float fullmatrix[dim][dim];` No, the size of the matrix will not automatically synchronize itself with future values of `dim` – n. m. could be an AI Nov 02 '13 at 18:14
  • @Carterini simply create fullmatrix once you have determined the value of dim. Not at the very beginning. – Bart Nov 02 '13 at 18:18
  • In addition, you are *counting* the items first, which is correct (and at that point you know how large the input is going to be), but you are counting *bytes* where you should count *floats*. After that you continue to read input from the last position. Use `fseek` or `rewind` to re-start at the beginning. Furthermore, the array structure suggests you are storing `dim*dim` items, not `dim` as you are counting now. – Jongware Nov 02 '13 at 18:19
  • Also, `for(i=0; i<=dim; i++)` -- no. Even with an adjusted value for `dim`, that's one too many. (Edit) Hmmm - you didn't mention a fairly crucial point: your input file consists of one float per line. Please adjust last comment to fit. – Jongware Nov 02 '13 at 18:20
  • I moved it and it still doesn't work :( – Carterini Nov 02 '13 at 18:21
  • @Jongware He is counting the number of lines of the input file. For his problem the number of floats per row is equal to the number of lines. He is dealing with a `n x n` matrix. – PhillipD Nov 02 '13 at 18:22
  • Yeah, I noticed that. Doesn't really help the OP left that out in his description, given the number of *other*, more pressing issues. – Jongware Nov 02 '13 at 18:23
  • Sorry I thought the fact that i'm doing the determinant would be enough to say it is n x n. Cheers for all your help guys, code is still ballsing up though. I feel so stupid. – Carterini Nov 02 '13 at 18:27
  • Okay, some hints to get you started (apart from the other issues). Your syntax `float fullmatrix[dim][dim]` is invalid under C. Change the declaration to: `float * fullmatrix;`, and where you know the size, insert `fullmatrix = malloc(dim*dim*sizeof float);`. – Jongware Nov 02 '13 at 18:42
  • @Jongware is `fullmatrix[dim][dim]` really invalid? – PhillipD Nov 02 '13 at 18:47
  • I just looked up the rewind and fseek things you mentioned earlier jongware and have implemented then and it's now working. Thanks so much. – Carterini Nov 02 '13 at 18:48
  • 1
    @PhillipD: [VLA is a C99/gcc extension](http://stackoverflow.com/questions/14548753/passing-a-multidimensional-variable-length-array-to-a-function) – Jongware Nov 02 '13 at 18:51
  • @Jongware Thanks for the information. Using `malloc()` as you suggest should be the best way to create the matrix. – PhillipD Nov 02 '13 at 19:01

1 Answers1

0

I found this code to work...but read the comments \\ and note the comment on variable length arrays (VLA).

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(int argc, char* argv[])
{
    FILE       *input;
    int        i, j, temp, dim;  

    const char inp_fn[]="matrix.dat";

/*Open File*/
input = fopen(inp_fn, "r");
dim = 0;

while (EOF != (temp = fgetc(input)))
{
    if (temp=='\n')
    {
        ++dim;
    }
}

float fullmatrix[dim][dim];  // define fullmatrix after dim is known

// we close and reopen the file. We could also move back to the beginning of the file.
fclose(input);

printf("%d\n", dim);

input = fopen(inp_fn, "r");

// Probably not the safest way...we just assume that the file has not change after we 
// determined dim
for (i=0; i<dim; i++) 
 {  
    for(j=0; j<dim; j++)
    {
       fscanf(input, "%f", &fullmatrix[i][j]);
        printf("%f ", fullmatrix[i][j]);
    }
    printf("\n");
}

fclose(input);


return(0);
}

Example output:

3
1.000000 2.000000 3.000000 
4.000000 5.000000 6.000000 
7.000000 8.000000 9.000000 
PhillipD
  • 1,797
  • 1
  • 13
  • 23