The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)
Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Monday, June 11, 2012

Finding Norm of Matrix


#include<conio.h>
#include<stdio.h>
#include<math.h>

int main()
  {
    int i,j,m,n;
    int mat[10][10];
    double s,nm;
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    s = 0;
    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);    
       
      for(j=0;j<n;j++)
      {
        scanf("%d",&mat[i][j]);    
        s += pow(mat[i][j],2);                  
      }              
    }
    nm = sqrt(s);
    printf("\n\nThe Norm of this matrix : %lf",nm);  
    getch();
    return 0;
  }

Saturday, June 9, 2012

Finding the Saddle Point of Matrix


#include<conio.h>
#include<stdio.h>
#include<math.h>

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);            
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
   
    for(i=0;i<m;i++)
    {
      min = 0;        
      for(j=1;j<n;j++)
      {
        if(mat[i][min] > mat[i][j])
         min = j;            
      }    
      for(j=0;j<m;j++)
      {
        if(mat[j][min] > mat[i][min])
         break;                            
      }
      printf("\n Row %d : ",i+1);
      if(j == m)
       printf("Saddle point exists at Column %d",min+1);
      else
       printf("Saddle point doesn't exists");    
    }    
    getch();
    return 0;
  }

Wednesday, June 6, 2012

Finding Skew-Symmetric Matrix


#include<conio.h>
#include<stdio.h>
#include<math.h>

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    do
    {
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    }while(m!=n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);          
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
 
    for(i=1;i<m;i++)
    {        
     for(j=0;j<i;j++)
     {
       if(mat[i][j] != -mat[j][i])
        break;            
     }  
    }  
 
    if(i == m && j+1 == i)
     printf("\n\nIt is a Skew-Symmetric Matrix");
    else
     printf("\n\nIt is not a Skew-Symmetric Matrix");
    getch();
    return 0;
  }

Monday, June 4, 2012

Finding Symmetric Matrix


#include<conio.h>
#include<stdio.h>
#include<math.h>

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    do
    {
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    }while(m!=n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);          
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
 
    for(i=1;i<m;i++)
    {        
     for(j=0;j<i;j++)
     {
       if(mat[i][j] != mat[j][i])
        break;            
     }  
    }  
 
    if(i == m && j+1 == i)
     printf("\n\nIt is a Symmetric Matrix");
    else
     printf("\n\nIt is not a Symmetric Matrix");
    getch();
    return 0;
  }

Thursday, May 3, 2012

Longest Common Sub-sequence 2


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
char a[10],b[10];
int count[10][10],dir[10][10];

void backtrack(int i, int j)
{
if(i!=0 && j!=0)
  {
if(dir[i][j] == 3)
    {
backtrack(i-1,j-1);
printf("%c",a[i-1]);        
    }     
else if(dir[i][j] == 2)
backtrack(i-1,j);
else
backtrack(i,j-1);
  }  
}

int main()
{
                int i,j,k,count1,count2;
                printf("\n Enter 1st string : ");
gets(a);
fflush(stdin);
printf("\n Enter 2nd string : ");
gets(b);
                count1 = strlen(a);
    count2 = strlen(b);

                for(i=1;i<=count1;i++)
                count[i][0]=0;
                for(i=1;i<=count2;i++)
                count[0][i]=0;
                for(i=1;i<=count1;i++)
                {
                                for(j=1;j<=count2;j++)
                                {
                                                if(a[i-1]==b[j-1])
                                                {
                                                                count[i][j]=count[i-1][j-1]+1;
                                                                dir[i][j]=3;
                                                }
                                                else if(count[i][j-1]>=count[i-1][j])
                                                {
                                                                count[i][j]=count[i][j-1];
                                                                dir[i][j]=1;
                                                }
                                                else
                                                {
                                                                count[i][j]=count[i-1][j];
                                                                dir[i][j]=2;
                                                }
                                }
                }
                char temp;
                i=0;
                while(i<count2)
                {
                printf("%c\t",b[i]);
                i++;
    }
                printf("\n");
for(i=1;i<=count1;i++)
                {
printf("%c",a[i-1]);
                                for(j=1;j<=count2;j++)
                                {
                                                if(dir[i][j]==1)
                                                temp='-';
                                                else if(dir[i][j]==2)
                                                temp='|';
                                                else if(dir[i][j]==3)
                                                temp='\\';
                                                printf("%c%d\t",temp,count[i][j]);

                                }
                                printf("\n");
                }             
printf("The LCS is : ");
backtrack(count2,count1);          
getch();
return(0);

}

Monday, April 16, 2012

Longest Common Sub-sequence


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20

int c[max+1][max+1];
char b[max+1][max+1];
char x[max],y[max];
int m,n;

void showLCS(int i,int j)
{
  if(i!=0 && j!=0)
  {
    if(b[i][j] == '\\')
    {
      showLCS(i-1,j-1);    
      printf("%c",x[i-1]);        
    }    

    else if(b[i][j] == '|')
     showLCS(i-1,j);

    else
     showLCS(i,j-1);
  }  
}

int main()
{
 int i,j;  

 printf("\n Enter 1st string : ");
 gets(x);

 printf("\n Enter 2nd string : ");
 gets(y);

 m = strlen(x);
 n = strlen(y);

 for(i=1;i<=m;i++)
 {
  c[i][0] = 0;
  b[i][0] = ' ';
 }

 for(j=0;j<=n;j++)
 {
  c[0][j] = 0;
  b[0][j] = ' ';
 }

 for(i=1;i<=m;i++)
 {
  for(j=1;j<=n;j++)
  {
    if(x[i-1] == y[j-1])
    {
      c[i][j] = c[i-1][j-1]+1;
      b[i][j] = '\\';          
    }


    else
    {
      if(c[i-1][j] >= c[i][j-1])
      {
        c[i][j] = c[i-1][j];
        b[i][j] = '|';
      }

      else
      {
        c[i][j] = c[i][j-1];
        b[i][j] = '_';
      }      
    }
  }
 }

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

 getch();
 printf("\n\n The LCS of both strings : ");
 showLCS(m,n);
 getch();
 return 0;  
}

Sunday, April 8, 2012

Strassen's Multiplication


#include<stdio.h>
#include<conio.h>

int main()
{
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;
  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }
  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }
  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);  
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;
   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }
   getch();
   return 0;
}

Sunday, September 4, 2011

Various Matrix Operations


#include<conio.h>
#include<iostream.h>
#define max 3

class matrix
{
 private :
  int mx[max][max];
  int det;
  int tmx[max][max];
  float imx[max][max];
  int cf[max][max];
  int adj[max][max];
 public :
  void inp_dat();
  int find_mdet(int,int);
  void find_trns(int);
  void pro_dat();
};

int i,j,k;

void matrix :: inp_dat()
{
 for(i=1;i<=max;i++)
 {
  cout<<"\n Enter the elements for Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cin>>mx[i][j];
 }
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cout<<" "<<mx[i][j];
 }
}

void matrix :: find_trns(int choice)
{
 for(int i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(int j=1;j<=max;j++)
  {
  if(choice == 1)
   {
   tmx[i][j] = mx[j][i];
   cout<<" "<<tmx[i][j];
   }
  else
   {
   adj[i][j] = cf[j][i];
   cout<<" "<<adj[i][j];
   }
  }
 }
}

int matrix :: find_mdet(int r,int c)
{
 int ar[4],res;
 k = 0;
 for(i=1;i<=max;i++)
 {
  for(j=1;j<=max;j++)
  {
   if(i != r && j != c)
   {
    ar[k] = mx[i][j];
    k++;
   }
  }
 }
 res = ar[0]*ar[3]-ar[1]*ar[2];
 return res;
}

void matrix :: pro_dat()
{
 int prod = 1;
 det = 0;
 for(i=1;i<=max;i++)
 {
  det += prod*mx[1][i]*find_mdet(1,i);
  prod *= -1;
 }
 cout<<"\n\n Its Determinant is : "<<det;
 cout<<"\n\n Its Transpose is : \n\n ";
 find_trns(1);
 cout<<"\n\n Its co-factor is : \n";
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
  {
  cf[i][j] = prod*find_mdet(i,j);
  cout<<" "<<cf[i][j];
  prod *= -1;
  }
 }
 cout<<"\n\n Its Adjoint is : \n ";
 find_trns(2);
 if(det == 0)
  cout<<"\n\n It's Inverse doesn't exists as its Determinant is Zero.";
 else
 {
  cout<<"\n\nIts Inverse is : \n";
  for(i=1;i<=max;i++)
  {
   cout<<"\n Row #"<<i<<" :";
   for(j=1;j<=max;j++)
   {
   imx[i][j] = (float) adj[i][j]/det;
   cout<<" "<<imx[i][j];
   }
  }
 }
}

void main()
{
clrscr();
matrix mat;
mat.inp_dat();
mat.pro_dat();
getch();
}