commit b45e895d5ac7c1df20b94a8a607f26ec3969c33d
parent 493116077c77d7ce4a3085e8968df1f248cd9e44
Author: klewer-martin <martin.cachari@gmail.com>
Date: Thu, 1 Apr 2021 18:11:17 -0300
Update: added ex47.c
Diffstat:
2 files changed, 74 insertions(+), 0 deletions(-)
diff --git a/guia03/ex46.c b/guia03/ex46.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <time.h>
#define N 3
diff --git a/guia03/ex47.c b/guia03/ex47.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <time.h>
+
+#define N 3
+#define M 4
+
+void m_load(size_t r, size_t c, double matrix[r][c]);
+void m_initrand(size_t r, size_t c, double matrix[r][c]);
+void m_print(size_t r, size_t c, double matrix[r][c]);
+void m_transpose(size_t r, size_t c, double matrix[r][c], double matrix_t[c][r]);
+bool m_simetric(size_t r, size_t c, double matrix[r][c]);
+
+int main (void)
+{
+ double matrix[N][M];
+ double matrix_transpose[M][N];
+
+ m_load(N, M, matrix);
+ m_print(N, M, matrix);
+ putchar('\n');
+
+ m_transpose(N, M, matrix, matrix_transpose);
+ m_print(M, N, matrix_transpose);
+
+
+ return 0;
+}
+
+void m_load(size_t r, size_t c, double matrix[r][c])
+{
+ char buf[20];
+ for(size_t i = 0; i < r; i++) {
+ for(size_t j = 0; j < c; j++) {
+ fgets(buf, 20, stdin);
+ matrix[i][j] = strtod(buf, NULL); /* value from stdin */
+ }
+ }
+}
+
+void m_initrand(size_t r, size_t c, double matrix[r][c])
+{
+ srand((unsigned int)time(NULL));
+
+ for(size_t i = 0; i < N; i++) {
+ for(size_t j = 0; j < M; j++) {
+ matrix[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
+ }
+ }
+}
+
+void m_print(size_t r, size_t c, double matrix[r][c])
+{
+ for(size_t i = 0; i < r; i++) {
+ putchar('(');
+ putchar(' ');
+ for(size_t j = 0; j < c; j++) {
+ printf("%6.2f ", matrix[i][j]);
+ }
+ putchar(')');
+ putchar('\n');
+ }
+}
+
+void m_transpose(size_t r, size_t c, double matrix[r][c], double matrix_t[c][r])
+{
+ for(size_t i = 0; i < r; i++) {
+ for(size_t j = 0; j < c; j++)
+ matrix_t[j][i] = matrix[i][j];
+
+ }
+}