C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,482 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




Sparse Matrix

 

Sparse Matrix, Need help reading in sparse matrix

riskyricky

6 Dec, 2008 - 03:28 PM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 6

I'm trying to read in a sparse matrix for a .txt file. The size of the matrix is 20 x 20 always. I need to read in the sparse matrix and store it into a regular matrix 20 x 20. I can open and read in data, but do not know how to reference the sparse into a regular matrix.

sparse.txt file
20,20
0,2-8
0,7-3
1,2-1
2,18-7
3,16-2
5,12-9
5,15-12
8,7-3
9,3-6
10,0-19
10,12-5
13,17-3
14,3-18
15,12-3

Need to read in and convert to normal matrix. Any help greatly appreciated. Thanks

CODE

int read_sparse_matrix(int read_matrix[20][20], FILE* infile){
    do{
    printf("Enter the file name of First Matrix\n>>");
    scanf("%s", &file);
    printf("Opening file %s...\n", file);
    if((infile = fopen(file, "r")) == NULL){
            printf("Error Opening File. Try Agian\n\n");
    }
    }while(infile == NULL);
    x = fgetc(infile);
    while(x != '\n'){
        x = fgetc(infile);
    }
    x = fgetc(infile);
    for(i=0;i<20;i++){
        for(j=0;j<20;j++){
            if(x == ' ' || x == '\n'){
                x = fgetc(infile);
            }
            if(x != ' ' && x != '\n'){
                A[i][j] = ((int)x)-48; //Assigns matrix A to data.
            }
            x = fgetc(infile);
        }
    }
    return 0;
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 04:02AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month