9511_workbook

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