9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit ad72aba8f2238b090628806bc6fe0a8818b38a80
parent d2d3e493ce04c0cebaea5ce793adcbdeadbb9cd4
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Fri,  2 Apr 2021 21:53:32 -0300

Update: added guia03 last exercises;

Diffstat:
Mguia03/ex48.c | 16----------------
Aguia03/ex49.c | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 16 deletions(-)
diff --git a/guia03/ex48.c b/guia03/ex48.c
@@ -34,18 +34,7 @@ int main (void)
     m_create(2, 2, &matrix);
     m_create(2, 3, &matrix2);
     m_create(2, 3, &matrix_result);
-    m_create(2, 2, &matrix_transpose);
 
-    /*
-    m_load(N, M, matrix);
-    m_print(N, M, matrix);
-
-    putchar('\n');
-    
-    m_transpose(&matrix, &matrix_transpose);
-    m_print(&matrix_transpose);
-
-    */
     m_initrand(&matrix);
     m_print(&matrix);
 
@@ -56,15 +45,12 @@ int main (void)
 
     putchar('\n');
 
-    m_initrand(&matrix2);
-
     m_multiply(&matrix, &matrix2, &matrix_result);
     m_print(&matrix_result);
 
     m_destroy(&matrix);
     m_destroy(&matrix2);
     m_destroy(&matrix_result);
-    m_destroy(&matrix_transpose);
 
     return 0;
 }
@@ -129,8 +115,6 @@ void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
     }
 }
 
-
-
 void m_create(size_t rows, size_t columns, matrix_t *matrix)
 {
     matrix->rows = rows;
diff --git a/guia03/ex49.c b/guia03/ex49.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define ARRAYS 3
+#define LARGE_OF_ARRAYS 4
+
+typedef struct {
+    size_t n, m;
+    int **a;
+} array_t;
+
+int arr_find_max(array_t *array);
+void arr_create(size_t n, size_t m, array_t *array);
+void arr_initran(array_t *array);
+void arr_print(array_t *array);
+void arr_destroy(array_t *array);
+
+int main (void)
+{
+    array_t array;
+
+    arr_create(ARRAYS, LARGE_OF_ARRAYS, &array);
+
+    arr_initran(&array);
+
+    arr_print(&array);
+
+    printf("\nThe maximum value is: %d\n", arr_find_max(&array));
+
+    arr_destroy(&array);
+    return 0;
+}
+
+
+void arr_create(size_t n, size_t m, array_t *array)
+{
+    array->n = n; 
+    array->m = m;
+
+    array->a = (int **)malloc(n * sizeof(int *));
+
+    for(size_t i = 0; i < n; i++)
+        *(array->a + i) = (int *)malloc(m * sizeof(int));
+}
+
+void arr_destroy(array_t *array)
+{
+    for(size_t i = 0; i < array->n; i++)
+        free(*(array->a + i));
+
+    free(array->a);
+}
+
+void arr_initran(array_t *array)
+{
+    srand((unsigned int)time(NULL));
+    
+    for(size_t i = 0; i < array->n; i++)
+        for(size_t j = 0; j < array->m; j++)
+            array->a[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
+        
+}
+
+void arr_print(array_t *array)
+{
+    for(size_t i = 0; i < array->n; i++) {
+        for(size_t j = 0; j < array->m; j++)
+            printf("%2d ", array->a[i][j]);
+        
+        putchar('\n');
+    }
+}
+
+int arr_find_max(array_t *array)
+{
+    int max = 0;
+    for(size_t i = 0; i < array->n; i++)
+        for(size_t j = 0; j < array->m; j++)
+            if(array->a[i][j] > max)
+                max = array->a[i][j];
+    
+    return max;
+}