Right, so I tried the validation code below and it's not really working....
Any ideas
Code:
/******************************************************************
***Program to find the determinant of a 3 x 3 matrix from a file***
******************************************************************/
/**********************************************
****Include all necessary standard libraries***
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/**********************************
****Declare the external function***
**********************************/
double CalcDet(double matrix[][]);
/***********************
****Start the program***
***********************/
int main(void)
{
/*Define variables*/
int i, j, tmp;
FILE* input;
double matrix[3][3], line1, line2, line3, det;
const char input_fn[]="matrix.dat";
/*Open file*/
input=fopen("matrix.dat","r");
/*Verify file*/
if (input==(FILE*) NULL)
{
printf("Cannot open required file!\n");
printf("Please ensure your matrix file\n");
printf("is in the correct directory\n");
exit(EXIT_FAILURE);
}
else
{
/*test code*/
while(fscanf(input, "%.1f %.1lf %.1lf %.1lf %.1lf %.1lf %.1lf .1%lf %.1lf", matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], matrix[1][1], matrix[1][2], matrix[2][0], matrix[2][1], matrix[2][2], &tmp) == 9);
{
/*Write matrix from matrix.dat to an array*/
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
fscanf(input, "%lf", &matrix[i][j]);
}
}
/*Close file*/
fclose(input);
/*Print the stored matrix and it's determinant*/
det = CalcDet(matrix);
printf("The matrix entered and it's determinant is\n");
printf("|%.1lf %.1lf %.1lf|\n", matrix[0][0], matrix[0][1], matrix[0][2]);
printf("|%.1lf %.1lf %.1lf| = %.6lf\n", matrix[1][0], matrix[1][1], matrix[1][2], det);
printf("|%.1lf %.1lf %.1lf|\n", matrix[2][0], matrix[2][1], matrix[2][2]);
}
}
return(0);
}
/***********************
****End of program******
***********************/
/*********************************
****Define external function, ****
****it returns the determinant****
*********************************/
double CalcDet(double matrix[3][3])
{
double det, line1, line2, line3;
line1 = ((matrix[0][0]) * ((matrix[1][1])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][1])));
line2 = ((matrix[0][1]) * ((matrix[1][0])*(matrix[2][2]) - (matrix[1][2])*(matrix[2][0])));
line3 = ((matrix[0][2]) * ((matrix[1][0])*(matrix[2][1]) - (matrix[1][1])*(matrix[2][0])));
det = line1 - line2 + line3;
return(det);
}