Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MEMERR perror(""); exit(-1);
- double **mkMat(int n, int m) {
- double **arr = calloc(sizeof(double*), n);
- if (arr == NULL) {
- MEMERR
- }
- for (int i = 0; i < n; i++) {
- arr[i] = calloc(sizeof(double), m);
- if (arr[i] != NULL) {
- continue;
- }
- while (i--) {
- free(arr[i]);
- }
- free(arr);
- MEMERR
- }
- return arr;
- }
- void printMat(double *const *const arr, int n, int m) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- printf("%g ", arr[i][j]);
- }
- puts("");
- }
- }
- void freeMat(double **arr, int n) {
- for (int i = 0; i < n; i++) {
- free(arr[i]);
- }
- free(arr);
- }
- void readMat(double **arr, int n, int m) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- scanf("%lf", &arr[i][j]);
- }
- }
- }
- double **multMat(double **a, double **b, int n, int m, int k) {
- double **res = mkMat(n, k);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < k; j++) {
- double sum = 0;
- for (int it = 0; it < m; it++) {
- sum += a[i][it] * b[it][j];
- }
- res[i][j] = sum;
- }
- }
- return res;
- }
- int main(void) {
- int m, n, p;
- double **a = NULL, **b = NULL, **c = NULL;
- scanf("%d%d%d", &m, &n, &p);
- a = mkMat(m, n);
- b = mkMat(n, p);
- readMat(a, m, n);
- readMat(b, n, p);
- c = multMat(a, b, m, n, p);
- printMat(c, m, p);
- freeMat(a, m);
- freeMat(b, n);
- freeMat(c, m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement