KEMBAR78
Sparce Matrix | PDF | Algorithms | Computational Science
0% found this document useful (0 votes)
9 views1 page

Sparce Matrix

The document contains C code for displaying a sparse matrix representation and its transpose. It prompts the user to input the dimensions and elements of a matrix, then constructs a sparse representation and computes its transpose. Finally, it prints both the original sparse matrix and the transposed sparse matrix.

Uploaded by

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

Sparce Matrix

The document contains C code for displaying a sparse matrix representation and its transpose. It prompts the user to input the dimensions and elements of a matrix, then constructs a sparse representation and computes its transpose. Finally, it prints both the original sparse matrix and the transposed sparse matrix.

Uploaded by

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

#include <stdio.

h> // Display sparse representation


printf("\nSparse Matrix Representation
int main() { (row col value):\n");
int m, n, i, j, count = 0; for (i = 0; i <= count; i++) {
printf("%d\t%d\t%d\n", sparse[i][0],
printf("Enter rows and columns of matrix: sparse[i][1], sparse[i][2]);
"); }
scanf("%d%d", &m, &n);
// Transpose sparse matrix
int mat[m][n]; int transpose[count + 1][3];
transpose[0][0] = sparse[0][1];
printf("Enter elements of the matrix:\n"); transpose[0][1] = sparse[0][0];
for (i = 0; i < m; i++) { transpose[0][2] = sparse[0][2];
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]); k = 1;
if (mat[i][j] != 0) { for (j = 0; j < n; j++) {
count++; for (i = 1; i <= count; i++) {
} if (sparse[i][1] == j) {
} transpose[k][0] = sparse[i][1];
} transpose[k][1] = sparse[i][0];
transpose[k][2] = sparse[i][2];
// Sparse representation (count+1 rows, 3 k++;
cols) }
int sparse[count + 1][3]; }
sparse[0][0] = m; }
sparse[0][1] = n;
sparse[0][2] = count; // Display transpose sparse matrix
printf("\nTranspose Sparse Matrix
int k = 1; Representation (row col value):\n");
for (i = 0; i < m; i++) { for (i = 0; i <= count; i++) {
for (j = 0; j < n; j++) { printf("%d\t%d\t%d\n", transpose[i][0],
if (mat[i][j] != 0) { transpose[i][1], transpose[i][2]);
sparse[k][0] = i; }
sparse[k][1] = j;
sparse[k][2] = mat[i][j]; return 0;
k++; }
}
}
}

You might also like