9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex46.c (1503B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <stdbool.h>
   4 #include <time.h>
   5 
   6 #define N 3
   7 #define M 4
   8 
   9 void m_load(size_t r, size_t c, double matrix[r][c]);
  10 void m_initrand(size_t r, size_t c, double matrix[r][c]);
  11 void m_print(size_t r, size_t c, double matrix[r][c]);
  12 void m_transpose(size_t r, size_t c, double matrix[r][c], double matrix_t[c][r]);
  13 
  14 int main (void)
  15 {
  16     double matrix[N][M];
  17     double matrix_transpose[M][N];
  18 
  19 //    m_initrand(N, M, matrix);
  20     m_load(N, M, matrix);
  21     m_print(N, M, matrix);
  22     putchar('\n');
  23     
  24     m_transpose(N, M, matrix, matrix_transpose);
  25     m_print(M, N, matrix_transpose);
  26 
  27 
  28     return 0;
  29 }
  30 
  31 void m_load(size_t r, size_t c, double matrix[r][c])
  32 {
  33     char buf[20];
  34     for(size_t i = 0; i < r; i++) {
  35         for(size_t j = 0; j < c; j++) {
  36             fgets(buf, 20, stdin);
  37             matrix[i][j] = strtod(buf, NULL);    /*    value from stdin    */ 
  38         }
  39     }
  40 }
  41 
  42 void m_initrand(size_t r, size_t c, double matrix[r][c])
  43 {
  44     srand((unsigned int)time(NULL));
  45 
  46     for(size_t i = 0; i < N; i++) {
  47         for(size_t j = 0; j < M; j++) {
  48             matrix[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
  49         }
  50     }
  51 }
  52 
  53 void m_print(size_t r, size_t c, double matrix[r][c])
  54 {
  55     for(size_t i = 0; i < r; i++) {
  56         putchar('(');
  57         putchar(' ');
  58         for(size_t j = 0; j < c; j++) {
  59             printf("%6.2f ", matrix[i][j]);
  60         }
  61         putchar(')');
  62         putchar('\n');
  63     }
  64 }
  65 
  66 void m_transpose(size_t r, size_t c, double matrix[r][c], double matrix_t[c][r])
  67 {
  68     for(size_t i = 0; i < r; i++) {
  69         for(size_t j = 0; j < c; j++)
  70             matrix_t[j][i] = matrix[i][j];
  71 
  72     }
  73 }