9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/matrix_struct.c (3005B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <stdbool.h>
   4 #include <time.h>
   5 
   6 #define N 3
   7 #define M 3
   8 
   9 typedef struct {
  10     size_t rows, columns;
  11     double **array;
  12 } matrix_t;
  13 
  14 void m_create(size_t rows, size_t columns, matrix_t *matrix);
  15 void m_destroy(matrix_t *matrix);
  16 
  17 void m_load(matrix_t *matrix);
  18 void m_initrand(matrix_t *matrix);
  19 void m_print(matrix_t *matrix);
  20 void m_transpose(matrix_t *matrix, matrix_t *matrix_transpose);
  21 void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
  22 
  23 bool m_isSimetric(size_t r, size_t c, double matrix[r][c]);
  24 
  25 
  26 int main (void)
  27 {
  28     matrix_t matrix;
  29     matrix_t matrix2;
  30     matrix_t matrix_result;
  31     matrix_t matrix_transpose;
  32 
  33     m_create(2, 2, &matrix);
  34     m_create(2, 3, &matrix2);
  35     m_create(2, 3, &matrix_result);
  36     m_create(2, 2, &matrix_transpose);
  37 
  38     /*
  39     m_load(N, M, matrix);
  40     m_print(N, M, matrix);
  41 
  42     putchar('\n');
  43     
  44     m_transpose(&matrix, &matrix_transpose);
  45     m_print(&matrix_transpose);
  46 
  47     */
  48     m_initrand(&matrix);
  49     m_print(&matrix);
  50 
  51     putchar('\n');
  52 
  53     m_initrand(&matrix2);
  54     m_print(&matrix2);
  55 
  56     putchar('\n');
  57 
  58     m_initrand(&matrix2);
  59 
  60     m_multiply(&matrix, &matrix2, &matrix_result);
  61     m_print(&matrix_result);
  62 
  63     m_destroy(&matrix);
  64     m_destroy(&matrix2);
  65     m_destroy(&matrix_result);
  66     m_destroy(&matrix_transpose);
  67 
  68     return 0;
  69 }
  70 
  71 void m_print(matrix_t *matrix)
  72 {
  73     for(size_t i = 0; i < matrix->rows; i++) {
  74         putchar('(');
  75         putchar(' ');
  76         for(size_t j = 0; j < matrix->columns; j++) {
  77             printf("%6.2f ", matrix->array[i][j]);
  78         }
  79         putchar(')');
  80         putchar('\n');
  81     }
  82 }
  83 
  84 void m_initrand(matrix_t *matrix)
  85 {
  86     srand((unsigned int)time(NULL));
  87     
  88     for(size_t i = 0; i < matrix->rows; i++) {
  89         for(size_t j = 0; j < matrix->columns; j++) {
  90             matrix->array[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
  91         }
  92     }
  93 }
  94 
  95 void m_load(matrix_t *matrix)
  96 {
  97     char buf[20];
  98     for(size_t i = 0; i < matrix->rows; i++) {
  99         for(size_t j = 0; j < matrix->columns; j++) {
 100             fgets(buf, 20, stdin);
 101             matrix->array[i][j] = strtod(buf, NULL);
 102         }
 103     }
 104 }
 105 
 106 void m_transpose(matrix_t *matrix, matrix_t *matrix_transpose)
 107 {
 108     for(size_t i = 0; i < matrix->rows; i++) {
 109         for(size_t j = 0; j < matrix->columns; j++)
 110             matrix_transpose->array[j][i] = matrix->array[i][j];
 111 
 112     }
 113 }
 114 
 115 void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
 116 {
 117     if(matrixA->columns != matrixB->rows)
 118         return;
 119 
 120     double aux;
 121     for(size_t i = 0; i < matrixA->rows; i++) {
 122         for(size_t j = 0; j < matrixB->columns; j++) {
 123             for(size_t k = aux = 0; k < matrixA->columns; k++)
 124                 aux += ((matrixA->array[i][k])*(matrixB->array[k][j]));
 125             
 126             result->array[i][j] = aux;
 127         }    
 128     }
 129 }
 130 
 131 
 132 
 133 void m_create(size_t rows, size_t columns, matrix_t *matrix)
 134 {
 135     matrix->rows = rows;
 136     matrix->columns = columns;
 137 
 138     matrix->array = (double **)malloc((rows) * sizeof(double));
 139 
 140     for (size_t i = 0; i < rows; i++)
 141          matrix->array[i] = (double *)malloc(columns * sizeof(double));
 142 }
 143 
 144 void m_destroy(matrix_t *matrix)
 145 {
 146     for (size_t i = 0; i < matrix->rows; i++)
 147          free(matrix->array[i]);
 148 
 149     free(matrix->array);
 150 }