1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 2 6 #define MATRIX_MAX 20 7 8 double matrix_det(size_t n, double matrix[][n]); 9 10 int main (void) 11 { 12 double matrix[N][N]; 13 14 double determinant; 15 double trace; 16 17 srand((unsigned int)time(NULL)); 18 19 for(size_t i = 0; i < N; i++) { 20 /* This to make it fancy */ 21 putchar('('); 22 putchar(' '); 23 24 /* Initialize the matrix with random numbers in range 0 - MATRIX_MAX */ 25 for(size_t j = 0; j < N; j++) { 26 matrix[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20; 27 printf("%10f ", matrix[i][j]); 28 } 29 30 /* This to make it fancy too*/ 31 putchar(')'); 32 putchar('\n'); 33 34 /* Calculates the matrix trace */ 35 trace += matrix[i][i]; 36 } 37 38 39 determinant = matrix_det(N, matrix); 40 41 printf("\nDeterminant = %10f.-\nTrace = %10f.-\n", determinant, trace); 42 43 return 0; 44 } 45 46 double matrix_det(size_t n, double matrix[][n]) 47 { 48 double det; 49 if(n == 2) 50 return ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0])); 51 else if (n == 3) 52 return (matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])) 53 - (matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])) 54 - (matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0])); 55 }