user6807856
user6807856

Reputation:

Getting Transposition matrix considering sparse matrix(may something to do with sorting)

I want to get sparse matrix and print it but it does`t go well.

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

#define MAX_TERMS 10

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

typedef struct SparseMatrix {
    int rows;
    int cols;
    int terms;
    element data[MAX_TERMS];
} SparseMatrix;

void printSM(SparseMatrix temp);
SparseMatrix transpose(SparseMatrix a);

void main(){
    SparseMatrix A = {3,3,3, {{0, 1, 3},
                              {1, 0, 8},
                              {2, 2, 5}}};
    SparseMatrix B;
    B = transpose(A);
    printSM(A);
    printSM(B);
    getchar();
}

void printSM(SparseMatrix temp) {
    printf(">>Sparse Matrix<< \n");
    int r, c, i = 0;
    for (r = 0; r < temp.rows; r++) {
        for (c = 0; c < temp.cols; c++) {
            if (r == temp.data[i].row && c == temp.data[i].col) {
                printf("%3d", temp.data[i].value);
                i++;
            }
            else
            {
                printf("%3d", 0);
            }
        }
        printf("\n");
    }
}

is the given question and I made some code to deal with it

SparseMatrix transpose(SparseMatrix a){
    SparseMatrix b=a;
    for (int index = 0; index < a.terms; index++) {
        if (b.data[index].col != b.data[index].row) {
            b.data[index].col = a.data[index].row;
            b.data[index].row = a.data[index].col;
        }
    }
    return b;

It seems like ok. I thought it should be work well but that was a mistake.

The answer must be 0 8 0 3 0 0 0 0 5 But I only got 0 0 0 3 0 0 0 0 0

I tested by transpose function by using for loop and I knew I was totally did well I got b.data[0,1,2]={{1, 0, 3},{0, 1, 8},{2, 2, 5}} b.col=b.low=b.terms=3

I looked up my print function and I have noticed that the order should be important. For example, when I transpose these and get these (3,2, 4)>>>>>>>(2,3, 4) (3,1, 4)>>>>>>>(1,3, 4) that printSM function may ignore 2nd one because that the variable which indicates row already increased by 2 and could ignore the cases when cols=1 I know this printSM function is not recommendable and I want to change it to better one but I cannot touch this function because that is the given problem.....

So I am trying to insert selection sorting to my transpose function such that

for (int index = 0; index < b.terms - 1; index++) {
    int tmp=index;
    for (int indexp = index + 1; indexp < b.terms; indexp++) {
        if (b.data[tmp].col > b.data[indexp].col) {
            tmp = indexp;
        }
    }
            int ctmp = b.data[tmp].col;
            int rtmp = b.data[tmp].row;
            int vtmp = b.data[tmp].value;
            b.data[tmp].col = b.data[index].col;
            b.data[tmp].row = b.data[index].row;
            b.data[tmp].value = b.data[index].value;
            b.data[index].col = ctmp;
            b.data[index].row = rtmp;
            b.data[index].value = vtmp;
}

and.... I got the same answer 0 0 0 3 0 0 0 0 0

Oh no.... May I get your help please?

Upvotes: 0

Views: 70

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

A sparse matrix uses only a fraction of an otherwise huge memory requirement.

Yet you pass whole matrices to and from your functions. If you would truly be using large sparse matrices, you would run out of stack space.

You should change your function signatures to use pointers and use malloc to allocate only the memory required.

(This may not be the answer to your question, yet it is important to point this conceptual error out to you)

Upvotes: 1

Related Questions