## Write a program to enter a sparse matrix and display it (the sparse matrix must be stored in vector form)

/*
PROGRAM:
——–
Write a program to enter a sparse matrix and display it
(the sparse matrix must be stored in vector form)
*/
#include<stdio.h>
#include<conio.h>

void main()
{
int no,r[6],c[6],n[6],i,j,k;
clrscr();

for(i=0,k=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“Enter the value of Row-%d & Column-%d: “,
i+1,j+1);
scanf(“%d”,&no);
if(no!=0)
{
r[k]=i;
c[k]=j;
n[k]=no;
k++;
}
}
printf(“\n”);
}

printf(“\n\nThe Vector Form is: \n\n”);
for(i=0;i<k;i++)
{
printf(“%5d %5d %5d\n”,r[i]+1,c[i]+1,n[i]);
}
printf(“\n\nCreated Sparse Matrix from Vector Form:\n”);
for(i=0,k=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(r[k]==i&&c[k]==j)
{
printf(“%3d”,n[k]);
k++;
}
else
{
printf(“%3d”,0);
}

}
printf(“\n”);
}

getch();
}
/*
OUTPUT:
——-
Enter the value of Row-1 & Column-1: 0
Enter the value of Row-1 & Column-2: 0
Enter the value of Row-1 & Column-3: 0
Enter the value of Row-1 & Column-4: 5

Enter the value of Row-2 & Column-1: 0
Enter the value of Row-2 & Column-2: 2
Enter the value of Row-2 & Column-3: 0
Enter the value of Row-2 & Column-4: 0

Enter the value of Row-3 & Column-1: 0
Enter the value of Row-3 & Column-2: 0
Enter the value of Row-3 & Column-3: 0
Enter the value of Row-3 & Column-4: 4

Enter the value of Row-4 & Column-1: 0
Enter the value of Row-4 & Column-2: 0
Enter the value of Row-4 & Column-3: 0
Enter the value of Row-4 & Column-4: 1

The Vector Form is:

1 4 5
2 2 2
3 4 4
4 4 1
Created Sparse Matrix from Vector Form:
0 0 0 5
0 2 0 0
0 0 0 4
0 0 0 1
*/