0% found this document useful (0 votes)
4 views

Sparse Matric Program

The document contains a C program that accepts a matrix from the user and determines if it is sparse. If the matrix is sparse, it converts it into a triplet form, displaying the number of non-zero elements along with their row and column indices. If the matrix is not sparse, it simply indicates that the matrix is not sparse.

Uploaded by

cs7367266
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)
4 views

Sparse Matric Program

The document contains a C program that accepts a matrix from the user and determines if it is sparse. If the matrix is sparse, it converts it into a triplet form, displaying the number of non-zero elements along with their row and column indices. If the matrix is not sparse, it simply indicates that the matrix is not sparse.

Uploaded by

cs7367266
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/ 2

WAP to accept a matrix from user, find out matrix is sparse or not

and convert into triplex matrix.

#include <stdio.h>

int main() {
int a[10][10], triplet[100][3];
int r, c, i, j, nonZero = 0;

printf("Enter rows and columns: ");


scanf("%d %d", &r, &c);

printf("Enter matrix elements:\n");


for (i = 0; i < r; i++)
for (j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
if (a[i][j] != 0)
nonZero++;
}

if (nonZero <= (r * c) / 2) {
printf("\nMatrix is sparse.\nTriplet form:\n");
printf("%d %d %d\n", r, c, nonZero);

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
if (a[i][j] != 0)
printf("%d %d %d\n", i, j, a[i][j]);
} else {
printf("\nMatrix is not sparse.\n");
}

return 0;
}

You might also like