如何用malloc转换矩阵并打印它

如何用malloc转换矩阵并打印它,c,C,我希望我的代码扫描行和列的值,然后打印矩阵(我不在乎矩阵中的值)和转置矩阵 我的代码有什么问题 守则: #include <stdio.h> #include <string.h> void main() { int rows1, colums1, i, j; int**matrix1, **transpose_matrix1; printf_s("enter rows and colums\n"); scanf_s("%d%d", &

我希望我的代码扫描行和列的值,然后打印矩阵(我不在乎矩阵中的值)和转置矩阵

我的代码有什么问题

守则:

#include <stdio.h>
#include <string.h>

void main()
{
    int rows1, colums1, i, j;
    int**matrix1, **transpose_matrix1;

    printf_s("enter rows and colums\n");
    scanf_s("%d%d", &rows1, &colums1);

    matrix1 = malloc(sizeof(int*)*rows1);
    for (i = 0; i < rows1; i++)
        matrix1[i] = malloc(sizeof(int)*colums1);

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            matrix1[i][j] = i+j;
    puts("matrix: ");
    for (i = 0; i < rows1; i++)
    {
        for (j = 0; j < colums1; j++)
        {
            printf_s("%d ", matrix1[i][j]);
        }
        printf_s("\n");
    }

    transpose_matrix1 = malloc(sizeof(int*)*colums1);
    for (i = 0; i < colums1; i++)
        transpose_matrix1[i] = malloc(sizeof(int)*rows1);

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            transpose_matrix1[j][i] = matrix1[i][j];

    puts("transpose matrix:");
    for (i = 0; i < rows1; i++)
    {
        for (j = 0; j < colums1; j++)
        {
            printf_s("%d ", transpose_matrix1[i][j]);
        }
        printf_s("\n");
    }
}
输出:

enter rows and colums
matrix:
transposematrix:
0 1 -33686019
1 2 -33686019

在转置矩阵中,行数是正规矩阵的列数,列数等于正规矩阵的行数。因此,
i
应该从0到
列1
j
从0到
行1

for (i = 0; i < colums1; i++)
    for (j = 0; j < rows1; j++)
        transpose_matrix1[i][j] = matrix1[j][i];

puts("transpose matrix:");
for (i = 0; i < colums1; i++)
{
    for (j = 0; j < rows1; j++)
    {
        printf("%d ", transpose_matrix1[i][j]);
    }
    printf("\n");
}
(i=0;i 对于(j=0;j当您初始化转置矩阵时,您可以像

transpose_matrix1[j][i] = matrix1[i][j];
注意在
转置矩阵1[j][i]
中使用
j
i
的顺序


然后在打印时使用
转置矩阵1[i][j]
。请注意,
j
i
的顺序已更改,即使围绕这两个的循环相同。在两个循环中,
j
i
需要使用相同的顺序。

您对什么是列和什么是行有点困惑 以下是一个固定版本:

void print_matrix(int ** matrix, int rows, int colums){
    int i,j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < colums; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int transpose_rows1, transpose_colums1, rows1, colums1, i, j;
    int**matrix1, **transpose_matrix1;

    printf("enter rows and colums\n");
    scanf("%d%d", &rows1, &colums1);

    matrix1 = (int**)malloc(sizeof(int*)*rows1);
    for (i = 0; i < rows1; i++)
        matrix1[i] = (int*)malloc(sizeof(int)*colums1);

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            matrix1[i][j] = i+j;
    puts("matrix: ");
    print_matrix(matrix1, rows1, colums1);

    transpose_rows1 = colums1;
    transpose_colums1 = rows1;

    transpose_matrix1 = (malloc(sizeof(int*) * transpose_rows1));
    for (i = 0; i < transpose_rows1; i++)
        transpose_matrix1[i] = (malloc(sizeof(int) * transpose_colums1));

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            transpose_matrix1[j][i] = matrix1[i][j];

    puts("transpose matrix:");
    print_matrix(transpose_matrix1, transpose_rows1, transpose_colums1);

    return 0;
}
void打印矩阵(整数**矩阵,整数行,整数列){
int i,j;
对于(i=0;i
为了避免将来混淆自己,请尝试使用具有确切含义的函数和名称

创建一个函数来打印数组要比编写两次代码然后再仔细考虑现在的行和列更容易


而且,如果变量名colums1始终是矩阵的一列,而不是一列一行,那么就更容易了。因此,只需为第二个数组列和行创建一个新变量,并使用正确的数据即可。

您应该只根据所面临的实际问题提出实际的、可回答的问题。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。现在你的问题对我来说似乎是个离题的问题。你可以在这里找到更多的信息:@john这个问题不是最好的,但是现在可以回答它。如果矩阵是正方形,那么它的构造是对称的。转置矩阵将与原始矩阵相同,这是有意的吗?
void print_matrix(int ** matrix, int rows, int colums){
    int i,j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < colums; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int transpose_rows1, transpose_colums1, rows1, colums1, i, j;
    int**matrix1, **transpose_matrix1;

    printf("enter rows and colums\n");
    scanf("%d%d", &rows1, &colums1);

    matrix1 = (int**)malloc(sizeof(int*)*rows1);
    for (i = 0; i < rows1; i++)
        matrix1[i] = (int*)malloc(sizeof(int)*colums1);

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            matrix1[i][j] = i+j;
    puts("matrix: ");
    print_matrix(matrix1, rows1, colums1);

    transpose_rows1 = colums1;
    transpose_colums1 = rows1;

    transpose_matrix1 = (malloc(sizeof(int*) * transpose_rows1));
    for (i = 0; i < transpose_rows1; i++)
        transpose_matrix1[i] = (malloc(sizeof(int) * transpose_colums1));

    for (i = 0; i < rows1; i++)
        for (j = 0; j < colums1; j++)
            transpose_matrix1[j][i] = matrix1[i][j];

    puts("transpose matrix:");
    print_matrix(transpose_matrix1, transpose_rows1, transpose_colums1);

    return 0;
}