如何使用 C 中的结构获得正确的转置矩阵?

问题描述 投票:0回答:1

我制作了这个程序来获得矩阵

a
和矩阵
b
,这是矩阵
a
的转置矩阵。

问题是我无法得到准确的转置矩阵。

函数我查了好几遍,也没找到哪里错了

编译时没有错误

#include <stdio.h>

typedef struct
{
    int row;
    int col;
    int value;
} term;

我声明了我在整个程序中使用的结构 term

void transpose(term a[], term b[])
{
    int n, currentb;
    n = a[0].value; // Total number of elements  
    b[0].row = a[0].col; // Number of rows in b = Number of columns in a 
    b[0].col = a[0].row; // Number of columns in b = Number of rows in a
    b[0].value = n;

    if (n > 0) // A nonzero matrix
    {
        currentb = 1;
        for (int i = 0; i < a[0].col; i++) // Starting from the 0th column
        {
            for (int j = 1; j <= n; j++) // Find the element from the current column
            {
                if (a[j].col == i)
                {
                    // Add the elements in the current column to b.
                    b[currentb].row = a[j].col;
                    b[currentb].col = a[j].row;
                    b[currentb].value = a[j].value;
                    currentb++;
                }
            }
        }
    }
}

一开始我以为上面的函数写错了

所以,逻辑我看了好几遍都找不到错误的地方

int main()
{
    term a[3] = { {1,2,3}, {4,5,6}, {7,8,9} }
    term b[3];

    // print term a
    printf("original matrix\n");
    for (int i = 0; i < 3; i++)
    {
        printf(" %d %d %d\n", a[i].row, a[i].col, a[i].value);
    }
    printf("\n\n");
    
    // call the transpose function
    void transpose(term a[], term b[]);
    
    // print term b
    printf("transpose matrix\n");
    for (int i = 0; i < 3; i++)
    {
        printf(" %d %d %d\n", b[i].row, b[i].col, b[i].value);
    }

    return 0;
}

接下来,我回顾了调用转置函数的行。

因为输出结果是这样的:

original matrix
 1 2 3
 4 5 6
 7 8 9

transpose matrix
 -858993460 -858993460 -858993460
 -858993460 -858993460 -858993460
 -858993460 -858993460 -858993460

我已经翻了10多页的题了,还没找到和我一样错误结果是用结构体求转置矩阵的题

如果代码没有问题,结果应该是这样的

original matrix
 1 2 3
 4 5 6
 7 8 9
    
transpose matrix
 1 4 7
 2 5 8
 3 6 9

请检查我的代码,如果您能告诉我是否有任何问题,我将不胜感激。

c matrix structure transpose
1个回答
0
投票

表示矩阵的方式非常不寻常:您使用

term
结构的数组,每个结构都有一个
row
col
value
字段。转置这些结构没有多大意义,代码也很混乱,因为
row
col
都不代表行数或列数。

然而,主要问题是您根本没有调用

transpose
void transpose(term a[], term b[]);
只是一个函数声明,而不是调用。你应该写:

  // call the transpose function
  void transpose(a, b);

这里是表示为数组数组的 3x3 矩阵的修改版本:

#include <stdio.h>

void transpose(int a[3][3], int b[3][3]) {
    if (a == b) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < i; j++) {
                int temp = a[j][i];
                a[j][i] = a[i][j];
                a[i][j] = temp;
            }
        }
    } else {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                a[j][i] = b[i][j];
            }
        }
    }
}

int main() {
    int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} }
    int b[3][3];

    // print term a
    printf("original matrix\n");
    for (int i = 0; i < 3; i++) {
        printf(" %d %d %d\n", a[i][0], a[i][1], a[i][2]);
    }
    printf("\n");
    
    // call the transpose function
    void transpose(a, b);
    
    // print term b
    printf("transpose matrix\n");
    for (int i = 0; i < 3; i++) {
        printf(" %d %d %d\n", b[i][0], b[i][1], b[i][2]);
    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.