9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit c38249593fa9ecfdd658270755bfbf91446c52a4
parent 2db29245fab67287158a884e1ee8938520b9a46e
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Tue, 30 Mar 2021 23:05:34 -0300

Update;

Diffstat:
Aguia03/ex45.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)
diff --git a/guia03/ex45.c b/guia03/ex45.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define N 2
+#define MATRIX_MAX 20
+
+double matrix_det(size_t n, double matrix[][n]);
+
+int main (void)
+{
+    double matrix[N][N];
+
+    double determinant;
+    double trace;
+
+    srand((unsigned int)time(NULL));
+
+    for(size_t i = 0; i < N; i++) {
+        /* This to make it fancy */    
+        putchar('(');
+        putchar(' ');
+
+        /* Initialize the matrix with random numbers in range 0 - MATRIX_MAX */
+        for(size_t j = 0; j < N; j++) {
+            matrix[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
+            printf("%5.1f ", matrix[i][j]);
+        }
+
+        /* This to make it fancy too*/    
+        putchar(')');
+        putchar('\n');
+
+        /* Calculates the matrix trace */
+        trace += matrix[i][i];
+    }
+
+
+    determinant = matrix_det(N, matrix);
+
+    printf("\nDeterminant = %2.1f.-\nTrace = %2.1f.-\n", determinant, trace);
+
+    return 0;
+}
+
+double matrix_det(size_t n, double matrix[][n])
+{
+    double det;
+    if(n == 2) {
+        det = ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
+    } else if (n == 3) {
+        det = (matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])) 
+            - (matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]))
+            - (matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]));
+    }
+    return det;
+}