Sparse Matrix
Sparse Matrix
#include<stdio.h>
int main() {
int S[10][10],m,n,i,k=0,size=0;
printf("Enter number of rows in the matrix : ");
scanf("%d",&m);
printf("Enter number of columns in the matrix : ");
scanf("%d",&n);
printf("Enter elements in the matrix : ");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&S[i][j]);
printf("The matrix is \n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf(" %d ",S[i][j]);
if (S[i][j] != 0)
size++;
printf("\n");
}
int M[3][size];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (S[i][j] != 0) {
M[0][k] = i;
M[1][k] = j;
M[2][k] = S[i][j];
k++;
}
printf("Triplet representation of the matrix is \n");
for (int i=0; i<3; i++) {
for (int j=0; j<size; j++)
printf(" %d ", M[i][j]);
printf("\n");
}
return 0;
Output:
Enter number of rows in the matrix : 4
Enter number of columns in the matrix : 5
Enter elements in the matrix :
00304
00570
00000
02600
The matrix is
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Triplet representation of the matrix is
0 0 1 1 3 3
2 4 2 3 1 2
3 4 5 7 2 6
Obtain tripplet representation for the given sparse matrix.
C program to find transpose of given matrix:
https://www.scaler.com/topics/transpose-of-a-matrix-in-c/
https://www.thecrazyprogrammer.com/2014/02/c-program-for-findin
g-transpose-of-a-sparse-matrix.html
#include<stdio.h>
#include<stdlib.h>
#define max 5
int main() {
int matrix[max][max];
int spmatrix[max][3];
int transposematrix[max][3];
int i, j, k, row, col;
printf("Enter the order of sparse matrix\n");
scanf("%d %d", &row, &col);
printf("Enter the element of the sparse matrix\n");
for(i=0; i<row; i++)
for(j=0; j<col; j++)
scanf("%d",&matrix[i][j]);
k=1;
for(i=0; i<row; i++)
for(j=0; j<col; j++)
if(matrix[i][j]!=0) {
spmatrix[k][0]=i;
spmatrix[k][1]=j;
spmatrix[k][2]=matrix[i][j];
k++;
}
spmatrix[0][0]=row;
spmatrix[0][1]=col;
spmatrix[0][2]=k-1;
printf("ELEMENTS OF THE SPARSE MATRIX\n");
for(i=0; i<=spmatrix[0][2]; i++) {
for(j=0;j<3;j++)
printf("%d\t", spmatrix[i][j]);
printf("\n");
}
transposematrix[0][0]=spmatrix[0][1];
transposematrix[0][1]=spmatrix[0][0];
transposematrix[0][2]=spmatrix[0][2];
k=1;
for(i=0; i<spmatrix[0][1]; i++)
for(j=0;j<=spmatrix[0][2];j++)
if(spmatrix[j][1]==i) {
transposematrix[k][0]=spmatrix[j][1];
transposematrix[k][1]=spmatrix[j][0];
transposematrix[k][2]=spmatrix[j][2];
k++;
}
printf("Transpose of the sparse matrix\n");
for(i=0; i<=transposematrix[0][2]; i++) {
for(j=0;j<3;j++)
printf("%d\t", transposematrix[i][j]);
printf("\n");
}
C program to transpose sparse matrix:
#include <stdio.h>
int main() {
int m, n, i, j;
// Requesting the dimensions of the matrix from the user
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d%d", &m, &n);
int A[m][n], Transposed[n][m];
// Capturing the matrix elements from the user
printf("Enter the elements of the matrix:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("A[%d][%d] = ", i+1, j+1);
scanf("%d", &A[i][j]);
}
}
// Calculating the transpose
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
Transposed[j][i] = A[i][j];
}
}
// Displaying the transpose
printf("\nTranspose of the matrix is:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
printf("%d ", Transposed[i][j]);
}
printf("\n");
}
return 0;
}
Enter the number of rows and columns of the matrix: 4 5
Enter the elements of the matrix:
A[1][1] = 0
A[1][2] = 0
A[1][3] = 3
A[1][4] = 0
A[1][5] = 4
A[2][1] = 0
A[2][2] = 0
A[2][3] = 5
A[2][4] = 7
A[2][5] = 0
A[3][1] = 0
A[3][2] = 0
A[3][3] = 0
A[3][4] = 0
A[3][5] = 0
A[4][1] = 0
A[4][3] = 6
A[4][4] = 0
A[4][5] = 0
https://youtu.be/WOmvvwXDOns?si=jk7rCAIhlx1BNy6i
C program to transpose sparse matrix:
#include<stdio.h>
#include<stdlib.h>
#define max 5
int main() {
int matrix[max][max];
int spmatrix[max][3];
int transposematrix[max][3];
int i, j, k, row, col;
printf("Enter the order of sparse matrix\n");
scanf("%d %d", &row, &col);
printf("Enter the element of the sparse matrix\n");
for(i=0; i<row; i++)
for(j=0; j<col; j++)
scanf("%d", &matrix[i][j]);
k=1;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(matrix[i][j]!=0) {
spmatrix[k][0]=i;
spmatrix[k][1]=j;
spmatrix[k][2]=matrix[i][j];
k++;
}
spmatrix[0][0]=row;
spmatrix[0][1]=col;
spmatrix[0][2]=k-1;
printf("ELEMENTS OF THE SPARSE MATRIX\n");
for(i=0; i<=spmatrix[0][2]; i++) {
for(j=0;j<3;j++)
printf("%d\t", spmatrix[i][j]);
printf("\n");
}
transposematrix[0][0]=spmatrix[0][1];
transposematrix[0][1]=spmatrix[0][0];
transposematrix[0][2]=spmatrix[0][2];
k=1;
for(i=0; i<spmatrix[0][1]; i++)
for(j=0; j<=spmatrix[0][2]; j++)
if(spmatrix[j][1]==i) {
transposematrix[k][0]=spmatrix[j][1];
transposematrix[k][1]=spmatrix[j][0];
transposematrix[k][2]=spmatrix[j][2];
k++;
}
printf("Transpose of the sparse matrix\n");
for(i=0; i<=transposematrix[0][2]; i++) {
for(j=0;j<3;j++)
printf("%d\t", transposematrix[i][j]);
printf("\n");
}
return 0;
}
https://youtu.be/3gvPxtE0rD8?si=oF3EUU7myO_BYPto
https://youtu.be/x70zNUIHR0k?si=1KtU308qSrdR8Dl- => How to
multiply two sparse matrices