Search code examples
csparse-matrix

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, %d)  %d\n", mysparse[i].row,
                                        mysparse[i].column,
                                        mysparse[i].value);
    }
}

Solution

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

    You are incrementing place three times.