Page

Programe to Sparse Matrix in C/C++

//*******************************************************************
// SPARSE MATRIX
//********************************************************************
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<graphics.h>
void insert(int [][100],int,int);
void display(int [][100],int ,int);
void check(int [][100],int,int);
void main()
{
int gd,gm;
int a[100][100],ch,n,m,k;
char cho;

s:
clrscr();
printf("\t\t\t_________________________________\n\n");
printf("\t\t\t********SPARSE MATRIX *********");
printf("\n\n\t\t\t_________________________________");
printf("\n\n\t\t\t*******ENTER THE CHOICE*********");
printf("\n\t\t\t________________________________");
printf("\n\t\t\t INTER THE MATRIX      [1]");
printf("\n\t\t\t CHECKING  THE MATRIX  [2]");
printf("\n\t\t\t DISPLAY               [3]");
printf("\n\t\t\t EXIT                  [4]\n\n");
printf("\t\t\t\t.......");
fflush(stdin);
scanf("%d",&ch);


clrscr();

switch(ch)
{

case 1:
clrscr();
k=1;
printf("\n\n\t\t\t_____________________________________");
printf("\n\n\t\t\t***INSERTION PART*****\n");
printf("\t\t\t________________________________________\n\n");

printf("\n\n\t\t\tENTER THE SIZE OF MATRIX\n\n");
printf("\t\t\t\tROW:=");
scanf("%d",&n);
printf("\t\t\t\tCOLLUMN:=");
scanf("%d",&m);

insert(a,n,m);           //call the insert function
goto s;



case 2:
clrscr();
printf("\n\n\t\t\t_____________________________________");
printf("\n\n\t\t\t\t***MATRIX CHACKING PART*****\n");
printf("\t\t\t________________________________________\n\n");

 if(k==1)
{
check(a,n,m);              //call the check function
}
else
{
printf("\n\n\t\t\tPLEASE INSERT THE MATRIX");
}

getch();
goto s;

case 3:
clrscr();
printf("\n\n\t\t\t\t___________________________________");
printf("\n\n\t\t\t\t\t******DISPLAY PART***********");
printf("\n\t\t______________________________________\n\n");
if(k==1)
{

display(a,n,m); //call the display function
}
else
{

printf("\n\n\t\t\tPLEASE INSERT THE MATRIX");
}
getch();
goto s;

case 4:
clrscr();

printf("\n\n\t\t\t____________________________________");
printf("\n\n\t\t\t\t***EXIT PART*****\n");
printf("\n\n\t\t\t\t__________________________________");
getch();
exit(0);



//*********************************************************************
//                               DEFAULT
//*********************************************************************
default:
clrscr();
printf("\n\n\n\t\t\t U R ENTER THE WRONG CHOICE");
delay(1000);
goto s;
}
 //***************END OF THE SWITCH STATEMENT***************
 //*********************************************************




getch();
}








//************************************************
//  ****INSERTION  function***
//*************************************************



void insert (int a[][100],int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("\n\n\t\t\tENTER THE VALUE of [%d][%d]:==",i,j);
scanf("%d",&a[i][j]);
}
}

      printf("\n\n\t\t\t*******MATRIX IS FULL*********");
      delay(1000);
}





//****************************************************************
//      DISPLAY   function
//****************************************************************/*



void display(int a[][100],int n,int m)
{
int i,j,k;
printf("\n\t\t\tROW:=%d\n\n\t\t\tCOLUMN:=%d\n",n,m);
printf("\n\n\n\t\t\tMATRIX IS ......\n\n\t\t");

for(k=0;k<m;k++)
{
printf("\tcolumn %d",k);
}

printf("\n\n\n");

for(i=0;i<n;i++)
{
printf("\trow %d\t",i);
for(j=0;j<m;j++)
{
printf("\t%d\t",a[i][j]);
}

printf("\n\n\n");

}

}





//*************************************************************
//     ******** CHECKING function *******
//***************************************************************/*

void check(int a[][100],int n ,int m)
{

int i,j,ut=0,lt=0,d=0,ire=0,k=0,re=0,ir=0;



for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i][j]==0)
{
lt=1;
break;
}
}
}


if(lt==1)
{
for(i=0;i<n;i++)
{
for(j=i;j<m;j++)
{
if(a[i][j]==0)
{
ut=1;
break;
}
}
}
}

 if((ut==1)&&(lt==1))
 {
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
{
if(a[i][j]==0)
{
d=1;
break;
}
}
else if(i!=j)
{
if(a[i][j]!=0)
{
d=1;
break;
}
}
}
}
}

if(lt==0)
{
re=1;
printf("\t\t\tmatrix is lower traingler");
}
else if(ut==0)
{
re=1;
printf("\t\t\tmatrix is upper triangular");
}
else if(d==0)
{
re=1;
printf("\t\t\tmatrix is daigonal");
}
else
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]==0)
{
ire++;
}
}
}

if(ir>1)
{
ire=1;
printf("matrix is iregular");
}
else
{
printf("matrix is not sparse matrix");
}
}
}



No comments:

Post a Comment