Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views7 pages

Sparse

Uploaded by

Ashwin Harikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Sparse

Uploaded by

Ashwin Harikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM: SPARSE MATRIX

#include <stdio.h>
void sparse(int A[10][10], int m, int n, int S[10][10]);
void add_sparse(int S1[10][10], int S2[10][10], int S3[10][10]);
void transpose(int S[10][10], int T[10][10]);
int main() {
int i, j, A[10][10], B[10][10], m, n, choice;
int S1[10][10], S2[10][10], S3[10][10];

do {
printf("\nMenu:\n");
printf("1. Enter matrices\n");
printf("2. Add sparse matrices\n");
printf("3. Find transpose of first matrix\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &m, &n);

printf("Enter the first matrix:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter the second matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &B[i][j]);
}
}

sparse(A, m, n, S1);
sparse(B, m, n, S2);
break;

case 2:
add_sparse(S1, S2, S3);
printf("\nSum of the matrices (Triplet Representation):\n");
for (i = 0; i <= S3[0][2]; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", S3[i][j]);
}
printf("\n");
}
break;
case 3:
transpose(S1, S3);
printf("\nTranspose of the first sparse matrix:\n");
for (i = 0; i <= S3[0][2]; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", S3[i][j]);
}
printf("\n");
}
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
void sparse(int A[10][10], int m, int n, int S[10][10]) {
int i, j, k = 1;
S[0][0] = m;
S[0][1] = n;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] != 0) {
S[k][0] = i;
S[k][1] = j;
S[k][2] = A[i][j];
k++;
}
}
}
S[0][2] = k - 1;
printf("\nTriplet representation :\n");
for (i = 0; i <= S[0][2]; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", S[i][j]);
}
printf("\n");
}
}
void add_sparse(int S1[10][10], int S2[10][10], int S3[10][10]) {
int i = 1, j = 1, k = 1;
S3[0][0] = S1[0][0];
S3[0][1] = S1[0][1];
S3[0][2]=0;
while (i <= S1[0][2] && j <= S2[0][2]) {
if (S1[i][0] < S2[j][0] || (S1[i][0] == S2[j][0] && S1[i][1] < S2[j][1])) {
S3[k][0] = S1[i][0];
S3[k][1] = S1[i][1];
S3[k][2] = S1[i][2];
i++;
k++;
} else if (S1[i][0] > S2[j][0] || (S1[i][0] == S2[j][0] && S1[i][1] > S2[j][1])) {
S3[k][0] = S2[j][0];
S3[k][1] = S2[j][1];
S3[k][2] = S2[j][2];
j++;
k++;
} else {
S3[k][0] = S1[i][0];
S3[k][1] = S1[i][1];
S3[k][2] = S1[i][2] + S2[j][2];
i++;
j++;
k++;
}
}
while (i <= S1[0][2]) {
S3[k][0] = S1[i][0];
S3[k][1] = S1[i][1];
S3[k][2] = S1[i][2];
i++;
k++;
}
while (j <= S2[0][2]) {
S3[k][0] = S2[j][0];
S3[k][1] = S2[j][1];
S3[k][2] = S2[j][2];
j++;
k++;
}
S3[0][2] = k-1;
}

void transpose(int S[10][10], int T[10][10]) {


int i, j, k = 1;
T[0][0] = S[0][1];
T[0][1] = S[0][0];
T[0][2] = S[0][2];
for (i = 0; i < S[0][1]; i++) {
for (j = 1; j <= S[0][2]; j++) {
if (S[j][1] == i) {
T[k][0] = S[j][1];
T[k][1] = S[j][0];
T[k][2] = S[j][2];
k++;
}
}
}
}
OUTPUT
ubuntu@ubuntu-H81M-S:~/AshwinHarikumar-10$ ./a.out
Menu:
1. Enter matrices
2. Add sparse matrices
3. Find transpose of first matrix
4. Exit
Enter your choice: 1
Enter the number of rows and columns of the matrices: 2
2
Enter the first matrix:
0
0
2
5
Enter the second matrix:
0
0
3
6

Triplet representation :
2 2 2
1 0 2
1 1 5

Triplet representation :
2 2 2
1 0 3
1 1 6
Menu:
1. Enter matrices
2. Add sparse matrices
3. Find transpose of first matrix
4. Exit
Enter your choice: 2

Sum of the matrices (Triplet Representation):


2 2 2
1 0 5
1 1 11

Menu:
1. Enter matrices
2. Add sparse matrices
3. Find transpose of first matrix
4. Exit
Enter your choice: 3

Transpose of the first sparse matrix:


2 2 2
0 1 2
1 1 5

Menu:
1. Enter matrices
2. Add sparse matrices
3. Find transpose of first matrix
4. Exit
Enter your choice: 4
Exiting...

You might also like