【问题标题】:add sparse matrix in C在C中添加稀疏矩阵
【发布时间】:2021-11-18 08:51:03
【问题描述】:

如何正确添加每个稀疏矩阵? 我有两个稀疏矩阵 A 和 B,, 但是我写了关于add_matrix的代码它不能正常工作

我认为 printf("0"); 的代码毁了一切 那么我该如何编写函数add_matrix

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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

typedef struct SparseMatrix {
    element* data;
    int rows;
    int cols;
    int terms;
} SparseMatrix;


void matrix_transpose(SparseMatrix a, SparseMatrix *b) 
{
    for (int r = 0; r < (a.terms); r++) {
            b->data[r].row = a.data[r].col;
            b->data[r].col = a.data[r].row;
            b->data[r].value = a.data[r].value;
    }
}

void add_matrix(SparseMatrix a, SparseMatrix b, SparseMatrix* c)
{
    // this part
    for (int i = 0; i < a.terms; i++) {
        c->data[i].value = a.data[i].value + b.data[i].value;
    }
}
    


void print_matrix(SparseMatrix a)
{
    int n = a.rows;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            bool check = false;
            for (int k = 0; k < a.terms; k++)
            {
                if (a.data[k].row == i && a.data[k].col == j)
                {
                    printf("%2d", a.data[k].value);
                    check = true;
                }
            }
            if (!check)
                printf(" 0");
        }
        printf("\n");
    }
    printf("\n");
}

int main(void)
{
    SparseMatrix A, B, C;

    int n, num; //n*n matrix, num of values

    scanf("%d %d", &n, &num);

    A.cols = n;
    A.rows = n;
    A.terms = num;
    A.data = (element*)malloc(sizeof(element) * num);
    B.rows = n;
    B.cols = n;
    B.terms = num;
    B.data = (element*)malloc(sizeof(element) * num);
    C.rows = n;
    C.cols = n;
    C.data = (element*)malloc(sizeof(element) * num);  
    C.terms = num;
    for (int i = 0; i < num; i++)
    {
        scanf("%d %d %d", &A.data[i].row, &A.data[i].col, &A.data[i].value);
    }

    print_matrix(A);

    matrix_transpose(A, &B);

    print_matrix(B);

    add_matrix(A, B, &C);

    print_matrix(C);
    free(A.data);
    free(B.data);
    free(C.data);
    return 0;
}

输入

3 3 0 0 3 1 2 5 2 0 7

想要的输出

6 0 7 0 0 5 7 5 0

我的代码输出

6 0 0 0 0 0 0 0 0 0 0

我调试了什么

A和B的行,col不变。

【问题讨论】:

  • “不起作用”从来都不是一个好的问题描述。请给出准确的输入、预期结果和实际结果。还要描述您进行了哪些调试,以及根据该调试您发现哪里出错了。
  • 测试用例 #1 输入:3 3 0 0 3 1 2 5 2 0 7 输出:6 0 7 0 0 5 7 5 0 我所做的是重写 add_matrix 函数。除了add_matrix 一切都是正确的,但老实说我不知道​​该怎么做才能做到最好
  • 请不要将此类信息放入 cmets。 Edit 更新它的帖子。此外,您仍然只提供了一个输出。这是预期的还是实际的输出?请同时提供。那么调试呢?
  • 我编辑了帖子。没事吧?
  • 您可能误解了“稀疏”一词或这些结构所代表的含义。在 add 函数中,哪里考虑了矩阵元素的实际位置?

标签: c algorithm matrix sparse-matrix


【解决方案1】:

函数add_matrx没有考虑矩阵元素的实际位置:

void add_matrix(SparseMatrix a, SparseMatrix b, SparseMatrix* c)
{
    for (int i = 0; i < a.terms; i++) {
    //              ^^^^^^^^^^^
    // Why are you assuming that both the matrices had the same
    // amount of non-zero values? Are you sure that the resulting matrix
    // will have exactly the same number of non-zeroes (in the same places)?

        c->data[i].value = a.data[i].value + b.data[i].value;
        // That may be right IF both a.data[i] and b.data[i] have the same
        // row and column, otherwise you are summing the wrong elements.

        // What are c->data[i].row and c->data[i].col now?
        // Those are not initialized, leading to undefined behavior, e.g. I
        // obtained this result running your program:
        // 61014 0 0
        // 0 0 0
        // 0 0 0
    }
}

我建议在从流中提取矩阵元素的值之后和使用矩阵之前按行和按列对矩阵元素进行排序,以使所有算法更容易(并且更高效)。

【讨论】:

    猜你喜欢
    • 2018-08-07
    • 2017-12-16
    • 1970-01-01
    • 2017-06-15
    • 2018-04-26
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 2017-06-15
    相关资源
    最近更新 更多