how to generate sparse matrix in c

Answers were Sorted based on User's Feedback



how to generate sparse matrix in c..

Answer / aman celly

//PROGRAM TO CREATE A REPRESENTATION OF SPARSE MATRIX AND
PERFORM OPERATIONS

#include<stdio.h>
#include<conio.h>
void implement(int[][10],int,int,int[][10]);
void add(int[][10],int,int,int[][10],int[][10]);
void sub(int[][10],int,int,int[][10],int[][10]);
void transpose(int[][10],int,int[][10]);
void main()
{
int a[10][10];
int b[10][10];
int c[10][10];
int d[10][10];
int e[50][10];
int row,col,k,l,ch;
char che;
clrscr();
printf("enter the no. of rows\t:");
scanf("%d",&row);
printf("\n enter the no. of coloms\t:");
scanf("%d",&col);
printf("\n enter the elements of sparse matrix \t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("\n element at [%d][%d]\t",k,l);
scanf("%d",&a[k][l]);
}
}
printf("sparse matrix is \n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%d\t",a[k][l]);
}
printf("\n");
}
do
{
printf("\n CYNET MENU\n");
printf("1:REPRESENTATION\n");
printf("2:ADDITION\n");
printf("3:SUBTRACTION\n");
printf("4:TRANSPOSE\n");
printf("5:EXIT\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
implement(a,row,col,b);
break;
case 2:

printf("\n enter the elements of second sparse matrix \t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("\n element at [%d][%d]\t",k,l);
scanf("%d",&c[k][l]);
}
}
printf("sparse matrix is \n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%d\t",c[k][l]);
}
printf("\n");
}
implement(a,row,col,b);
implement(c,row,col,d);
add(b,row,col,d,e);
break;
case 3:
printf("\n enter the elements of second sparse matrix \t:");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("\n element at [%d][%d]\t",k,l);
scanf("%d",&c[k][l]);
}
}
printf("sparse matrix is \n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%d\t",c[k][l]);
}
printf("\n");
}
implement(a,row,col,b);
implement(c,row,col,d);
sub(b,row,col,d,e);
break;
case 4:
implement(a,row,col,b);
transpose(b,col,c);
break;

case 5:
exit();
default:
printf("you entered wrong choice\n");
}
printf("do you want to continue(y\Y):");
che=getche();
//clrscr();
}while(che=='y'||che=='Y');
getch();
}
void implement(int a[][10],int row,int col,int b[][10])
{
int g=1,nz=0,k,l;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{
nz=nz+1;
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=nz;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{
b[g][0]=k;
b[g][1]=l;
b[g][2]=a[k][l];
g++;
}
}
}
printf("implementation of sparse matrix is\n");
for(k=0;k<g;k++)
{
for(l=0;l<3;l++)
{
printf("%d\t",b[k][l]);
}
printf("\n");
}
}
void add(int b[][10],int row,int col,int d[][10],int e[]
[10])
{
int p1=1,p2=1,i=1;
int k,l;
if(b[0][0]!=d[0][0])
{
printf("addition is not possible\n");
}
else
{
while(p1<=b[0][2]&&p2<=d[0][2])
{
if(b[p1][0]==d[p2][0])
{
if(b[p1][1]<d[p2][1])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][1]>d[p2][1])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
else if(b[p1][1]==d[p2][1])
{
e[i][0]=d[p1][0];
e[i][1]=d[p1][1];
e[i][2]=b[p1][2]+d[p2][2];
if(e[i][2]!=0)
{
i++;
}
p1++;
p2++;
}

}
else if(b[p1][0]<d[p2][0])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][0]>d[p2][0])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
if(p1!=b[0][2])
{
while(p1<=b[0][2])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
}
else if(p2!=d[0][2])
{
while(p2<=d[0][2])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
e[0][0]=row;
e[0][1]=col;
e[0][2]=i-1;


printf("matrix after addition\n");
for(k=0;k<i;k++)
{
for(l=0;l<3;l++)
{
printf("%d\t",e[k][l]);
}
printf("\n");
} }
}

void sub(int b[][10],int row,int col,int d[][10],int e[]
[10])

{
int p1=1,p2=1,i=1;
int k,l;
if(b[0][0]!=d[0][0])
{
printf("subtraction is not possible\n");
}
else
{
while(p1<=b[0][2]&&p2<=d[0][2])
{
if(b[p1][0]==d[p2][0])
{


if(b[p1][1]<d[p2][1])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][1]>d[p2][1])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
else if(b[p1][1]==d[p2][1])
{
e[i][0]=d[p1][0];
e[i][1]=d[p1][1];
e[i][2]=b[p1][2]-d[p2][2];
if(e[i][2]!=0)
{
i++;
}
p1++;
p2++;
}

}
else if(b[p1][0]<d[p2][0])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][0]>d[p2][0])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
if(p1!=b[0][2])
{
while(p1<=b[0][2])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
}
else if(p2!=d[0][2])
{
while(p2<=d[0][2])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}

e[0][0]=row;
e[0][1]=col;
e[0][2]=i-1;


printf("matrix after subtraction\n");
for(k=0;k<=i;k++)
{
for(l=0;l<3;l++)
{
printf("%d\t",e[k][l]);
}
printf("\n");
} }
}

void transpose(int b[][10],int col,int c[][10])
{
int i,j,k,temp;

for(i=0;i<=b[0][2];i++)
{
c[i][0]=b[i][1];
c[i][1]=b[i][0];
c[i][2]=b[i][2];
}
for(i=1;i<=b[0][2];i++)
{
for(j=i+1;j<=b[0][2];j++)
{
if(c[i][0]>c[j][0])
{
for(k=0;k<3;k++)
{
temp=c[i][k];
c[i][k]=c[j][k];
c[j][k]=temp;
}
}

}
}
printf("transpose of matrix is\n");
for(i=0;i<=c[0][2];i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

Is This Answer Correct ?    107 Yes 41 No

how to generate sparse matrix in c..

Answer / d. prashant

/* Program of sparse matrix for 3-tuple method using array*/

#include
#define srow 50
#define mrow 20
#define mcolumn 20

main()
{
int mat[mrow][mcolumn],sparse[srow][3];
int i,j,nzero=0,mr,mc,sr,s;
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);

for(i=0;i for(j=0;j {
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
printf("Entered matrix is : \n");
for(i=0;i {
for(j=0;j {
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
nzero++;
}
printf("\n");
}

sr=nzero+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=nzero;
s=1;

for(i=0;i for(j=0;j {
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}
printf("Sparse matrix is :\n");
for(i=0;i {
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}/*End of main()*/

Is This Answer Correct ?    57 Yes 50 No

how to generate sparse matrix in c..

Answer / it career point

//program to 3-tuple representation
#include<stdio.>
#include<conio.h>
void main()
{
int a[5][5],row,columns,i,j;
printf(Ënter the order of the matrix.(5*5)");
scanf("%d%d",&row,&columns);
printf("Enter the element of the matrix\n");
for(i=0;i<row;i++)
for(j=0;j<columns;j++)
{
scanf("%d", &a[i][j]);
}
printf("3-tuple representation");
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
if(a[i][j]!=0)
{
printf("%d %d %d", (i+1),(j+1),a[i][j]);
}
}

getch();
}

Is This Answer Correct ?    41 Yes 42 No

Post New Answer

More C Interview Questions

Find the output? void main() {float a=2.0; printf("\nSize of a ::%d",sizeof(a)); printf("\nSize of 2.0 ::%d",sizeof(2.0));}

11 Answers   IBM, TCS,


int array[]={1,2,3,4,5,6,7,8}; #define SIZE (sizeof(array)/sizeof(int)) main() { if(-1<=SIZE) printf("1"); else printf("2"); }

2 Answers   Vector,


Which type of language is c?

0 Answers  


What is the easiest sorting method to use?

0 Answers  


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL

0 Answers  






main() { int x=2, y=4 if ((x==2||y==4) x++ y++ if (y==4+1) { x=x+y; } y++; printf("The values of x and y are %d and %d."x,y); } What is the output?

5 Answers   TCS,


int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above

0 Answers  


What is C language ?

0 Answers   Jekson,


Explain about the constants which help in debugging?

0 Answers  


How are structure passing and returning implemented?

0 Answers  


given the piece of code int a[50]; int *pa; pa=a; to access the 6th element of the array which of the following is incorrect? a.*(a+5) b.a[5] c.pa[5] d.*(*pa + 5)

6 Answers   amu, TCS,


What math functions are available for integers? For floating point?

0 Answers  


Categories