#include<stdio.h>
#include<conio.h>
# define MAX 50
struct triplet
{
int row;
int col;
int element;
}spmatrix[MAX];
void main()
{
clrscr();
int i, j, k=0, n, row, col;
printf("Array representation of Sparse Matrix\n\n");
printf("Enter Matrix Dimension:\n");
scanf("%d%d",&row,&col);
printf("\nEnter number of non-zero elements in sparse matrix: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element row, element column and its value\n");
scanf("%d%d%d",&spmatrix[i].row,&spmatrix[i].col,&spmatrix[i].element);
}
printf("\nThe Sparse Matrix is:\n");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
{
if((spmatrix[k].row==(i+1))&&(spmatrix[k].col==(j+1)))
{
printf("\t%d",spmatrix[k].element);
k++;
}
else
{
printf("\t0");
}
}
}
getch();
}
#include<conio.h>
# define MAX 50
struct triplet
{
int row;
int col;
int element;
}spmatrix[MAX];
void main()
{
clrscr();
int i, j, k=0, n, row, col;
printf("Array representation of Sparse Matrix\n\n");
printf("Enter Matrix Dimension:\n");
scanf("%d%d",&row,&col);
printf("\nEnter number of non-zero elements in sparse matrix: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element row, element column and its value\n");
scanf("%d%d%d",&spmatrix[i].row,&spmatrix[i].col,&spmatrix[i].element);
}
printf("\nThe Sparse Matrix is:\n");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
{
if((spmatrix[k].row==(i+1))&&(spmatrix[k].col==(j+1)))
{
printf("\t%d",spmatrix[k].element);
k++;
}
else
{
printf("\t0");
}
}
}
getch();
}