/*Serial No.130 [swami109.cpp]*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows,cols,tot=0;
int mat[5][5];
clrscr();
printf("enter the no of rows= ");
scanf("%d",&rows);
printf("enter the no of columns= ");
scanf("%d",&cols);
printf("\nEnter the matrix elements:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("\nEntered matrix is:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("\t%d",mat[i][j]);
}
printf("\n");
}
printf("\nSparse matrix is:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(mat[i][j]!=0)
tot++;
}
}
printf("\t%d\t%d\t%d\n",rows,cols,tot);
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(mat[i][j]!=0)
printf("\t%d\t%d\t%d\n",i+1,j+1,mat[i][j]);
}
}
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...