Tuesday, June 30, 2015

Working with Sparse Matrix

Posted By: BackBenchers World - Tuesday, June 30, 2015

Share

& Comment

#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();
}

About BackBenchers World

Techism is an online Publication that complies Bizarre, Odd, Strange, Out of box facts about the stuff going around in the world which you may find hard to believe and understand. The Main Purpose of this site is to bring reality with a taste of entertainment

0 comments:

Post a Comment

Copyright © 2013 TechDotHunter™ is a registered trademark.

Designed by Templateism. Hosted on Blogger Platform.