ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
how to generate sparse matrix in c
 Question Submitted By :: Chandrakala_13
I also faced this Question!!     Rank Answer Posted By  
 
  Re: how to generate sparse matrix in c
Answer
# 1
//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 ?    13 Yes 4 No
Aman Celly
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
Give me basis knowledge of c , c++...  4
What is a class?  2
#include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } ME5
hello friends what do u mean by BUS ERROR i got this error while i am doing my program in DATA STRUCTURES Wipro2
we have to use realloc only after malloc or calloc ? or we can use initially with out depending on whether we are using malloc or calloc in our program ?  1
Why doesn't C have nested functions?  2
6. Which of the Following is not defined in string.h? A)strspn() B)strerror() C)memchr() D)strod() Accenture1
read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50 TCS2
what is the use of pointers  5
what is the output of the following program? main() { int c[]={2,8,3,4,4,6,7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf("%d",*c); ++q; } for(j=0;j<5;j++) { printf("%d",*p); ++p; } }  4
what is the difference between structural,object based,object orientd programming languages? PanTerra1
What is the memory allocated by the following definition ? int (*x)(); ADITI2
what is the diference between pointer to the function and function to the pointer?  2
write a function which accept two numbers from main() and interchange them using pointers?  3
Program to write some contents into a file using file operations with proper error messages.  1
disadvantages of realloc ? HCL1
Difference Between embedded software and soft ware? Bosch1
What is the main differences between C and Embedded C?  2
Which of the following about automatic variables within a function is correct ? a.its type must be declared before using the variable b.they are local c.they are not initialised to zero d.they are global. TCS3
write the program for maximum of the following numbers? 122,198,290,71,143,325,98  4
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com