@Fuch
bmstu student

Как транспонировать прямоугольную матрицу?

#include "stdio.h"
#include "stdlib.h"

double **input_matrix(double **A, int l, int c) {
    A = (double **) malloc(l * sizeof(double *));
    for (int i = 0; i < l; ++i) {
        A[i] = (double *) malloc(c * sizeof(double));
        for (int j = 0; j < c; ++j) {
            double x;
            scanf("%lf", &x);
            A[i][j] = x;
        }
    }
    return A;
}

double **trans_matrix(double **A, int l, int c) {
    double **B = (double **) malloc(c * sizeof(double *));
    for (int i = 0; i < c; ++i) {
        B[i] = (double *) malloc(l * sizeof(double));
    }
    for (int i = 0; i < c; ++i) {
        for (int j = 0; j < l; ++j) {
            B[j][i] = A[i][j];
        }
    }
    return B;

}

void print_matrix(double **A, int l, int c) {
    for (int i = 0; i < l; ++i) {
        for (int j = 0; j < c; ++j) {
            printf("%lf ", A[i][j]);
        }
        printf("\n");
    }
}

void free_matrix(double **A, int l) {
    for (int i = 0; i < l; ++i) {
        free(A[i]);
    }
    free(A);
}

int main() {
    int l, c;
    printf("Enter the number of line: ");
    scanf("%d", &l);
    printf("Enter the number of column: ");
    scanf("%d", &c);
    double **A = input_matrix(A, l, c);
    A = trans_matrix(A, l, c);
    print_matrix(A, l, c);
    free_matrix(A, l);

    return 0;
}


Квадратная матрица транспонируется, а прямоугольная нет. Можете помочь?
  • Вопрос задан
  • 127 просмотров
Пригласить эксперта
Ответы на вопрос 1
mayton2019
@mayton2019
Bigdata Engineer
На входе - матрица размром (l,c) на выходе после транспонирования размеры перевернутся и будет (c,l).

UPD1: Ты аллоцировал матрицу А. Потом матрицу Б. И в конце удалил память только одной матрицы. Это небрежность?

UPD2: Как-то так должно быть. Лишние параметры поудалял. Добавил матрицу Б. Где-то перевернул индексы.

#include "stdio.h"
#include "stdlib.h"

double **input_matrix(int l, int c) {
    double **A = (double **) malloc(l * sizeof(double *));
    for (int i = 0; i < l; ++i) {
        A[i] = (double *) malloc(c * sizeof(double));
        for (int j = 0; j < c; ++j) {
            double x;
            scanf("%lf", &x);
            A[i][j] = x;
        }
    }
    return A;
}

double **trans_matrix(double **A, int l, int c) {
    double **B = (double **) malloc(c * sizeof(double *));
    for (int i = 0; i < c; ++i) {
        B[i] = (double *) malloc(l * sizeof(double));
    }
    for (int i = 0; i < c; ++i) {
        for (int j = 0; j < l; ++j) {
            B[i][j] = A[j][i];
        }
    }
    return B;
}

void print_matrix(double **A, int l, int c) {
    for (int i = 0; i < l; ++i) {
        for (int j = 0; j < c; ++j) {
            printf("%lf ", A[i][j]);
        }
        printf("\n");
    }
}

void free_matrix(double **A, int l) {
    for (int i = 0; i < l; ++i) {
        free(A[i]);
    }
    free(A);
}

int main() {
    int l, c;
    printf("Enter the number of lines: ");
    scanf("%d", &l);
    printf("Enter the number of column: ");
    scanf("%d", &c);
    
    double **A = input_matrix(l, c);
    printf("Matrix A\n");
    print_matrix(A, l, c);

    double **B = trans_matrix(A, l, c);
    printf("Matrix B\n");
    print_matrix(B, c, l);

    free_matrix(A, l);
    free_matrix(B, c);
    return 0;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы