9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 295b9667dc877082fcb85db9fa3b4f71c967ac32
parent 443d752c38696c6eb3d26cbd22f537b8c0b6dd41
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Wed, 23 Jun 2021 10:53:50 -0300

Update: added guia08 exercises

Diffstat:
Dguia08/a.out | 0
Aguia08/ex10.c | 42++++++++++++++++++++++++++++++++++++++++++
Aguia08/ex10_status_t.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Aguia08/ex18.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 139 insertions(+), 0 deletions(-)
diff --git a/guia08/a.out b/guia08/a.out
Binary files differ.
diff --git a/guia08/ex10.c b/guia08/ex10.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+double **identity_matrix(size_t dim);
+
+int main (void)
+{
+    double **m;
+    size_t len = 3;
+    m = identity_matrix(len);
+
+    for(size_t i = 0; i < len; i++) {
+        for(size_t j = 0; j < len; j++)
+            printf("%.0f ", m[i][j]);
+
+        putchar('\n');
+    }
+
+    return 0;
+}
+
+double **identity_matrix(size_t dim)
+{
+    double **m;
+    
+    /* Allocates memory for a squared matrix of dimension dim */
+    if(!(m = (double **)malloc(sizeof(double) * (dim * dim)))) return NULL;
+
+    /* Gets memory for every element of the matrix, if an error occurs, frees every previous allocated element and returns null, if no errors are enconuntered and both indexes are equal stores a 1 otherwise leaves it at zero */
+    for(size_t i = 0; i < dim; i++) {
+        if(!(m[i] = (double *)calloc(sizeof(double), dim))) {
+            for(size_t k = i; k--;) free(m[k]);
+            return NULL;
+        }
+        for(size_t j = 0; j < dim; j++) {
+            if(i == j) m[i][j] = 1;
+            else m[i][j] = 0;
+        }
+    }
+
+    return m;
+}
diff --git a/guia08/ex10_status_t.c b/guia08/ex10_status_t.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef enum {
+    OK,
+    ERROR_MEMORY_ALLOC,
+    ERROR_NULL_POINTER
+} status_t;
+
+status_t identity_matrix(size_t dim, double ***m);
+
+int main (void)
+{
+    double **m;
+    size_t len = 5;
+    if(!identity_matrix(len, &m));
+
+    for(size_t i = 0; i < len; i++) {
+        for(size_t j = 0; j < len; j++)
+            printf("%.0f ", m[i][j]);
+
+        putchar('\n');
+    }
+
+    return 0;
+}
+
+status_t identity_matrix(size_t dim, double ***m)
+{
+    if(!m) return ERROR_NULL_POINTER;
+
+    /* Allocates memory for a squared matrix of dimension dim */
+    if(!(*m = (double **)malloc(sizeof(double) * (dim * dim)))) return ERROR_MEMORY_ALLOC;
+
+    /* Gets memory for every element of the matrix, if an error occurs, frees every previous allocated element and returns ERROR_NULL_POINTER, if no errors are enconuntered and both indexes are equal stores a 1 otherwise leaves it at zero */
+    for(size_t i = 0; i < dim; i++) {
+        if(!((*m)[i] = (double *)calloc(sizeof(double), dim))) {
+            for(size_t k = i; k--;) free((*m)[k]);
+            return ERROR_MEMORY_ALLOC;
+        }
+        for(size_t j = 0; j < dim; j++) {
+            if(i == j) (*m)[i][j] = 1;
+            else (*m)[i][j] = 0;
+        }
+    }
+
+    return OK;
+}
diff --git a/guia08/ex18.c b/guia08/ex18.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define INIT_SIZE 10
+#define GROWTH_FACTOR .5
+
+typedef enum {
+    OK,
+    ERROR_ALLOC_MEMORY,
+    ERROR_NULL_POINTER
+} status_t;
+
+status_t fread_line(FILE *, char **);
+
+int main (void)
+{
+    char *str;
+
+    fread_line(stdin, &str);
+    printf("%s", str);
+
+    free(str);
+    return 0;
+}
+
+status_t fread_line(FILE *fp, char **dst)
+{
+    int aux;
+    size_t i, alloc_size;
+
+    if(!dst || !fp) return ERROR_NULL_POINTER;
+
+    alloc_size = INIT_SIZE;
+
+    if(!(*dst = (char *)calloc(alloc_size, sizeof(char)))) return ERROR_ALLOC_MEMORY;
+
+    for(i = 0; (aux = fgetc(fp)) != EOF; i++)
+    {
+        if(i == (alloc_size - 2))
+        {
+            alloc_size += (alloc_size * GROWTH_FACTOR);
+            if(!(*dst = (char *)realloc(*dst, alloc_size * sizeof(char)))) return ERROR_ALLOC_MEMORY;
+            /* Fills the new memory with zeros to avoid valgrind uninitialized warning */
+            for(size_t j = i; j < alloc_size; j++) (*dst)[j] = '\0';
+        }
+        (*dst)[i] = aux;
+    }
+    return OK;
+}