我编了这个程序,得到了矩阵a和矩阵b,这是矩阵a的转置矩阵.

问题是我不能得到一个准确的换位矩阵.

我判断了几次功能,但都找不到哪一部分错了.

编译时没有出现错误.

#include <stdio.h>

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

我声明了我在整个程序中使用的 struct 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页的问题,但我还没有找到像我这样使用 struct 来获得换位矩阵的错误结果的问题.

如果代码没有问题,结果应该如下所示.

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

请判断我的代码,如果你能告诉我有什么问题,我将不胜感激.

推荐答案

表示矩阵的方式不适合于赋值:您使用包含term个 struct 的数组,每个 struct 都有rowcolvalue个字段.这种表示形式对于稀疏矩阵是有效的,并且这种表示形式的转置很容易:只需交换每个termcolrow字段,但您需要将数组长度传递给transpose函数,因为它不能根据参数类型、值或内容确定元素的数量.

然而,主要问题是您根本没有调用transpose:void transpose(term a[], term b[]);只是一个函数声明,而不是调用.你应该写道:

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

发布的示例表明您正在处理用数组数组表示的简单矩阵.以下是表示为数组数组的3x3矩阵的修改版本:

#include <stdio.h>

void transpose(int a[3][3], int b[3][3]) {
    if (a == b) {
        // transposing the matrix in place:
        // swap the upper and lower triangles
        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 {
        // source and destination are distinct
        // copy the elements in transposed order
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                a[i][j] = b[j][i];
            }
        }
    }
}

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
    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;
}

C++相关问答推荐

如何从C中的公钥字符串创建EVP_PKEY

使用sd-设备列举设备导致seg错误

gcc已编译的可执行文件TSB是否同时暗示最低有效字节和最低有效位?

以c格式打印时间戳

使用错误的命令执行程序

对于C中给定数组中的每个查询,如何正确编码以输出给定索引范围(1到N)中所有数字的总和?

在Apple Silicon上编译x86的Fortran/C程序

为什么双精度d=flt_max+flt_max;在c语言中得到inf的结果

在C语言中,指针指向一个数组

C语言中MPI发送接收字符串时出现的分段错误

C语言中奇怪的输出打印数组

如何在C宏定义中包含双引号?

在吉陀罗中,_2_1_和CONCAT11是什么意思?

try 查找带有指针的数组的最小值和最大值

STM32 FATFS用户手册(Um1721)中的代码正确吗?

浮动目标文件,数据段

未为同一文件中的函数执行DirectFunctionCall

使用复合文字数组初始化的指针数组

在 C/C++ 中原子按位与字节的最佳方法?

如何在 C 中的 Postgres 函数的表中 for 循环