Reading a 2D array from a file, compiles but doesnt work properly
Hello, im beginning to write a program to calculate the determinant of a 3x3 matrix - i need to read the elements of the matrix from an external file matrix.dat (contains 9 numbers, 3 rows, 3 columns seperated by spaces)
I have written the first part of the program - reading from the file: It compiles but doesnt work, i got it to print a value from the matrix to see if it read it but when i run the program it says 0.00000, implying a problem
where have i gone wrong? thanks so much!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*need to use an external function to computer det of a minor (2x2 matrix)*/
/*Need to read from a file named matrix.dat*/
/* Prototype your function here, also make sure you change all of these comments*/
int ReadFromFile(float fullmatrix[3][3], FILE *input);
int main(int argc, char* argv[])
{
FILE *input;
int i, j;
float fullmatrix[3][3];
const char inp_fn[]="matrix.dat";
/* Open files */
input = fopen(inp_fn, "r");
/* Check the pointers to files are not NULL, also check matrix has correct number of elements */
if( (input != (FILE*) NULL) )
{
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
fullmatrix[i][j]=ReadFromFile(fullmatrix, input);
}
fscanf(input, "\n");
}
fclose(input);
printf("%f", fullmatrix[2][2]);
return(0);
}
else
{
printf("*** Could not open input file, or there are not 9 elements in the matrix ***\n");
}
}
/*Write the external fucntion here, need a 2 dimensional array http://www.dfstermole.net/OAC/harray2.html*/
int ReadFromFile(float fullmatrix[3][3], FILE *input)
{
float element;
fscanf(input,"%i,", &element);
return (element);
}