Created
August 24, 2019 19:00
-
-
Save Rajssss/9e3679302fd9e0b101bcaaa1a6684028 to your computer and use it in GitHub Desktop.
Sparse Matrix Representation in 3 Column Method in C....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| // Structure Definations | |
| struct element { | |
| int i; | |
| int j; | |
| int data; | |
| }; | |
| struct sparse { | |
| int Row; | |
| int Col; | |
| int nonZero; | |
| struct element *newElement; | |
| }; | |
| void CreateSparse(struct sparse *s); | |
| void DisplaySparse(struct sparse s); | |
| int main(void) { | |
| struct sparse s; | |
| CreateSparse(&s); | |
| DisplaySparse(s); | |
| free(s.newElement); | |
| return 0; | |
| } | |
| void CreateSparse(struct sparse *s) { | |
| setbuf(stdout, 0); | |
| printf("Enter Dimension of Sparse matrix Row X Col and No. of non zero " | |
| "elements =>"); | |
| scanf("%d %d %d", &s->Row, &s->Col, &s->nonZero); | |
| s->newElement = (struct element *)malloc(s->nonZero * sizeof(struct element)); | |
| printf("Now enter all the non zero values followed by " | |
| "indices i X j =>"); | |
| for (int i = 0; i < (s->nonZero); i++) | |
| scanf("%d %d %d", &s->newElement[i].data, &s->newElement[i].i, | |
| &s->newElement[i].j); | |
| } | |
| void DisplaySparse(struct sparse s) { | |
| int k = 0; | |
| for (int i = 0; i < s.Row; i + +) { | |
| for (int j = 0; j < s.Col; j++) { | |
| if (i == s.newElement[k].i && j == s.newElement[k].j) { | |
| printf("%d ", s.newElement[k].data); | |
| k++; | |
| } else | |
| printf("0 "); | |
| } | |
| printf("\n"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment