简体   繁体   English

C中稀疏矩阵的转置

[英]Transpose of sparse matrix in C

I've been trying to write a program that displays the sparse matrix and also finds the transpose of the matrix, but while transposing only the elements of the first row of the original matrix are getting transposed and all the other elements from other rows are getting ignored.我一直在尝试编写一个程序来显示稀疏矩阵并找到矩阵的转置,但是在转置时只有原始矩阵的第一行的元素被转置,而其他行的所有其他元素正在转置忽略。 I need some help.我需要帮助。

Here's the code I've written这是我写的代码

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

struct Element{
    int i;
    int j;
    int x;
};

struct Sparse{
    int m;
    int n;
    int num;
    struct Element *ele;
};

void create(struct Sparse *s){
    printf("Enter the dimensions ");
    scanf("%d%d",&s->m, &s->n );
    printf("Number of non-zero elements");
    scanf("%d",&s->num);
    s-> ele= (struct Element*) malloc(s->num * sizeof(struct Element));
    printf("Enter all non-zero elements\n");
    for (int i = 0; i< s->num; i++)
    {
        scanf("%d%d%d",&s->ele[i].i,&s->ele[i].j,&s->ele[i].x);
    }
}
void display(struct Sparse s){
    int i,j,k=0;
    for (i = 0; i < s.m; i++)
    {
        for (j = 0; j < s.n; j++)
        {
            if(i==s.ele[k].i && j== s.ele[k].j)
                printf("%d ",s.ele[k++].x);
            else
                printf("0 ");
        }
        printf(" \n");
    }
}
void createTranspose(struct Sparse *t, struct Sparse s){
    t->m = s.n;
    t->n = s.m;
    t->num = s.num;
    t-> ele= (struct Element*) malloc(t->num * sizeof(struct Element));
    printf("Enter all non-zero elements\n");
    for (int i = 0; i< t->num; i++)
    {
        t->ele[i].i= s.ele[i].j;
        t->ele[i].j= s.ele[i].i;
        t->ele[i].x= s.ele[i].x;
    }
}

int main(){
    struct Sparse s, t;
    create(&s);
    display(s);
    createTranspose(&t,s);
    printf("Transpose of the matrix is \n");
    display(t);
    return 0;
}

Output输出

Enter the dimensions 6 6
Number of non-zero elements6
Enter all non-zero elements
0 0 1
0 2 2
0 4 3
2 3 4
4 1 5
5 5 6
1 0 2 0 3 0
0 0 0 0 0 0
0 0 0 4 0 0
0 0 0 0 0 0
0 5 0 0 0 0
0 0 0 0 0 6
Enter all non-zero elements
Transpose of the matrix is
1 0 0 0 0 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
3 0 0 0 0 0
0 0 0 0 0 0

Some help to get a proper output would be highly appreciated.获得适当输出的一些帮助将不胜感激。

Your display function assumes that the sparse elements are in row-major order, but after simple transpose they're now in column-major order.您的显示函数假定稀疏元素按优先顺序排列,但经过简单转置后,它们现在按列优先顺序排列。

Either you need to re-sort the sparse elements that they retain the row-major ordering, or you need an inner loop in your display routine:您需要重新排序它们保留行优先顺序的稀疏元素,或者您需要在显示例程中使用内部循环:

    for (k = 0; k < s.num; k++) {
        if (i == s.ele[k].i && j == s.ele[k].j) {
             printf("%d ", s.ele[k].x);
             break;
        }
    }

    // not found among the sparse elements
    if (k == s.num) {
        printf("0 ");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM