开发者

Sparse matrix problem(there is a problem in my scan_matrix function)

I'm trying to write a matrix to sparse matrix conversion program. But, there is a problem in my scan_matrix function.I couldn't figure out where is the problem.According to Nemiver(debugging program) problem is in my fscanf call... My piece of code:

void scan_matrix(FILE *inp, struct sparse_m mysparse[], int *place_p)
{
    int matrix[9][9];
    int row,column,place=0;
    for(column=0;column<9;column++){
        for(row=0;row<9;row++){
            fscanf(inp, "%d", &matrix[row][column]);
        }
    }
    for(column=0;column<9;column++){
        for(row=0;row<9;row++){
            if( matrix[row][column] != 0 ) {
                mysparse[place++].row = row;
                mysparse[place++].column = column;
                mysparse[place++].value = matrix[row][column];
            }
        }
    }
    *place_p = place;
}

and my two dimensional matrix:

64  -16     0   -16     0     0     0     0     0
-16  64   -16     0   -16     0     0     0     0
0   -16    64     0     0   -16     0     0     0
-16   0     0    64   -16     0   -16     0     0
0   -16     0   -16    64   -16     0   -16     0
0     0   -16     0   -16    64     0     0   -16
0     0     0   -16     0     0    64   -16     0
0     0     0     0   -16     0   -16    64   -16
0     0     0     0     0   -16     0   -16    64

The output must be something like this;

(3, 0)  64
(4, 1)  -16
(5, 2)  -16
(6, 3)  -16
(7, 4)  64
(8, 5)  -16
(7, 6)  -16
(8, 7)  -16
(8, 8)  64

my full code;

#include <stdio.h>

struct sparse_m {
    int row,
        column,
        value;
};
void write_sparse(FILE *outp, struct sparse_m mysparse[], int c);
void scan_matrix(FILE *inp, struct sparse_m mysparse[],int *place_p);

int main(void)
{
    int a = 0;
    FILE *inp_txt;
    FILE *outp_txt;
    inp_txt = fopen("s_matrix.dat", "r");
    outp_txt = fopen("sparse.dat", "w");
    struct sparse_m mys[10];
    scan_matrix(inp_txt, mys,&a);
    write_sparse(outp_txt,mys,a);

    return(0);
}
void scan_matrix(FILE *inp, struct sparse_m mysparse[], int *place_p)
{
    int matrix[9][9];
    int row,column,place=0;
    for(column=0;column<9;column++){
        for(row=0;row<9;row++){
            fscanf(inp, "%d", &matrix[row][column]);
        }
    }
    for(column=0;column<9;column++){
        for(row=0;row<9;row++){
            if( matrix[row][column] != 0 ) {
                mysparse[place++].row = row;
                mysparse[place++].column = column;
                mysparse[place++].value = matrix[row][column];
            }
        }
    }
    *place_p = place;
}

void write_sparse(FILE *outp, struct sparse_m mysparse[], int c)
{
    int i;
    printf("%d---", c);
    for(i=0;i<c;i++){
        fprintf(outp, "(%d, 开发者_Go百科%d)  %d\n", mysparse[i].row,
                                        mysparse[i].column,
                                        mysparse[i].value);
    }
}


mysparse[place++].row = row;
mysparse[place++].column = column;
mysparse[place++].value = matrix[row][column];

You are incrementing place three times.


You've got your row/column loops the wrong way round. Instead of:

for(column=0;column<9;column++){
    for(row=0;row<9;row++){

you need:

for(row=0;row<9;row++){
    for(column=0;column<9;column++){

Your code was effectively transposing the matrix. In fact for the input you give this doesn't matter since the matrix is symmetric and so you get away with it for now.

Also see @Banthar's answer for another mistake!


The problem is here

mysparse[place++].row = row;
mysparse[place++].column = column;
mysparse[place++].value = matrix[row][column];

You're incrementing the myspase index you're writing to with every assignment above. Change it to the following:

mysparse[place].row = row;
mysparse[place].column = column;
mysparse[place].value = matrix[row][column];
place++;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜