fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef MATRIZ_H
  5. #define MATRIZ_H
  6.  
  7. #include <stdbool.h>
  8.  
  9. typedef struct Matriz Matriz;
  10.  
  11. Matriz* matriz_create(int rows, int cols);
  12. void matriz_destroy(Matriz* matriz);
  13. void matriz_print(Matriz* matriz);
  14. double matriz_get(Matriz* matriz, int row, int col);
  15. void matriz_set(Matriz* matriz, int row, int col, double value);
  16. int matriz_rows(Matriz* matriz);
  17. int matriz_cols(Matriz* matriz);
  18. bool matriz_equals(Matriz* matriz1, Matriz* matriz2);
  19.  
  20. // New functions
  21. Matriz* matriz_add(Matriz* matriz1, Matriz* matriz2);
  22. Matriz* matriz_sub(Matriz* matriz1, Matriz* matriz2);
  23. Matriz* matriz_scalar_mul(Matriz* matriz, double scalar);
  24. Matriz* matriz_transpose(Matriz* matriz);
  25. bool matriz_is_symmetric(Matriz* matriz);
  26.  
  27. #endif
  28.  
  29. struct Matriz {
  30. double** array;
  31. int rows;
  32. int cols;
  33. };
  34.  
  35. Matriz* matriz_create(int rows, int cols) {
  36. Matriz* matriz = malloc(sizeof(Matriz));
  37.  
  38. matriz->array = malloc(sizeof(double*) * rows);
  39. for (int i = 0; i < rows; i++) {
  40. matriz->array[i] = malloc(sizeof(double) * cols);
  41. }
  42. matriz->rows = rows;
  43. matriz->cols = cols;
  44.  
  45. return matriz;
  46. }
  47.  
  48. void matriz_destroy(Matriz* matriz) {
  49. for (int i = 0; i < matriz->rows; i++) {
  50. free(matriz->array[i]);
  51. }
  52. free(matriz->array);
  53. free(matriz);
  54. }
  55.  
  56. void matriz_print(Matriz* matriz) {
  57. for (int i = 0; i < matriz->rows; i++) {
  58. for (int j = 0; j < matriz->cols; j++) {
  59. printf("%.4f ", matriz->array[i][j]);
  60. }
  61. printf("\n");
  62. }
  63. }
  64.  
  65. double matriz_get(Matriz* matriz, int row, int col) {
  66. return matriz->array[row][col];
  67. }
  68.  
  69. void matriz_set(Matriz* matriz, int row, int col, double value) {
  70. matriz->array[row][col] = value;
  71. }
  72.  
  73. int matriz_rows(Matriz* matriz) {
  74. return matriz->rows;
  75. }
  76.  
  77. int matriz_cols(Matriz* matriz) {
  78. return matriz->cols;
  79. }
  80.  
  81. bool matriz_equals(Matriz* matriz1, Matriz* matriz2) {
  82. if (matriz1->rows != matriz2->rows || matriz1->cols != matriz2->cols) {
  83. return false;
  84. }
  85.  
  86. for (int i = 0; i < matriz1->rows; i++) {
  87. for (int j = 0; j < matriz1->cols; j++) {
  88. if (matriz1->array[i][j] != matriz2->array[i][j]) {
  89. return false;
  90. }
  91. }
  92. }
  93.  
  94. return true;
  95. }
  96.  
  97.  
  98. int main(void) {
  99. /* code */
  100. return 0;
  101. }
  102.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty