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;
}