矩阵倒置

矩阵转置

#include 
#include 
#pragma warning(disable:4996)

int main() {

    FILE * fin;
    FILE * fout;
    fin = fopen("matrixIn.txt", "r");
    fout = fopen("matrixOut.txt", "w");

    int a, b;

    fscanf(fin, "%d%d", &a, &b);
    fgetc(fin);

    int * A = (int *)malloc(sizeof(int)*a*b);

    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            fscanf(fin, "%d", A+(b*i)+j);
        }
    }

    for (int j = 0; j < b; j++) {
        for (int i = 0; i < a; i++) {
            fprintf(fout, "%d ", *(A + (b*i) + j));
        }
        fprintf(fout, "\n");
    }

    fclose(fin);
    fclose(fout);
    free(A);

    return 0;
}

你可能感兴趣的:(C语言)