A Sparse matrix is a matrix in which the maximum elements are zero.
Here in the given program we implement the sparse matrix by using Array .
Functions used to make a sparse matrix program are:-
1. Library functions
(a). printf();
(b). scanf();
(c) getch();
Header files are:-
1.#include<conio.h>
2.#include<stdio.h>
Data type used are-:
1.int
*********Program Start*********
// header files :- These files are necessary to use in your program to
call the library function like getch(); printf(); etc.
#include<conio.h>
#include<stdio.h>
// From here compiler start reading your program .
//drive code
void main()
{
// Data type declaration and initialization of variables
int arr[10][10],i,j,r,c,k=0,count=0,nm[10][10];
//To clear the previous screen
clrscr();
// Data input printf("\nenter size of row");
scanf("%d",&r);
printf("\nenter size of column");
scanf("%d",&c);
printf("\nenter data in the array");
// starting loop
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\nenter data-:%d",i);
scanf("%d",&arr[i][j]);
}
}
//Ending loop
//Starting loop for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(arr[i][j]!=0)
count++;
}
}
//Ending loop
//Starting loop
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(arr[i][j]!=0)
{ nm[0][k]=i;
nm[1][k]=j;
nm[2][k]=arr[i][j];
k++;
}
}
}
//Ending loop
printf("\ndisplay of the array:-\n");
//Starting of loop for(i=0;i<3;i++)
{
for(j=0;j<count;j++)
{
printf("%d\t",nm[i][j]);
}
printf("\n");
}
//Ending of loop
getch();
}
Very helpful....☺️☺️☺️☺️☺️
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletePost a Comment