Thursday, February 2, 2012

Array [XVI] 2-D Array – Determinant & Saddle Point




The determinant of a matrix can be defined, for example using the Leibniz formula, for matrices with entries in any commutative ring. Briefly, a ring is a structure where addition, subtraction, and multiplication are defined. The commutativity requirement means that the product does not depend on the order of the two factors. There are various ways to define the determinant of a square matrix A, i.e. one with the same number of rows and columns. Perhaps the most natural way is expressed in terms of the columns of the matrix. If we write an n-by-n  matrix in terms of its column vectors. The determinant of a square matrix A is denoted by "det A" or | A |.


The determinant of a 2×2 matrix is found much like a pivot  operation. It is the product of the elements on the main diagonal minus the product of the elements off the main diagonal.
a    b   = ad - bc
c    d


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

#define MAX 10
  float detm(float a[][MAX],int S)
      {
           int i,j,k;
           float mult;
           float det=1;
           for(i=0;i<S;i++)
              {
                   for(j=0;j<S;j++)
                        {
                            printf("\n\t%f ",a[i][i]);
                            if(a[i][i]!=0)
                                {
                                      mult=a[j][i]/(float)a[i][i];
                                }
                            else
                                {
                                      return(0);
                                }
                            for(k=0;k<S;k++)
                                {
                                      if(i==j)
                                                {
                                                          break;
                                                }
                                      a[j][k]=a[j][k]-a[i][k]*mult;

                                }
                            }
                   }

           for(i=0;i<S;i++)
              {
                   det=det*a[i][i];
              }
           return det;
  }



  int chck(float a[][MAX],int S)
      {
           int i,j,k;
           float nz;
           for(i=0;i<S;i++)
              {
                   if(a[i][i]==0)
                        {
                            for(j=0;j<S;j++)
                                {
                                      if(a[i][j]!=0)
                                                {
                                                          k=j;
                                                          break;
                                                }
                                      if(j==(S))
                                                {
                                                          return 0;
                                                }
                                }
                            for(j=0;j<S;j++)
                                {
                                      a[j][i]=a[j][i]-a[j][k];
                                }
                        }
              }
           return 1;
      }
  void main()
      {
           float a[MAX][MAX],det;
           int i,j,size;
           clrscr();
           printf("\n\tEnter Size of Array [Below 10] : ");
           scanf("%d",&size);
           for(i=0;i<size;i++)
              {
                   for(j=0;j<size;j++)
                        {
                            printf("\tEnter Element at [%d,%d] :",i,j);
                            scanf("%f",&a[i][j]);
                        }
              }
           printf("\n\tArray : \n");
           for(i=0;i<size;i++)
              {
                   printf("\t");
                   for(j=0;j<size;j++)
                        {
                            printf("%5.2f :",a[i][j]);
                        }
                   printf("\n");
              }

           if(chck(a,size)==0)
              {
                   det=0;
              }
           else
              {
                   det=detm(a,size);
                   printf("\n\tDeterminent of Above Matrix is :  %f",det);
              }
      getch();
  }


Saddle Point - Given a RxC Matrix, where R rows and C columns, a Saddle-Point  is a number in the matrix that is smallest in its row and largest in its column.



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

  void main()
   {
      int a[10][10];
      int i,j,r=0,c=0,r1=0,max=0,c1=0,f=0,min;
      clrscr();
      printf("\n\tEnter No. of Row : ");
      scanf("%d",&r);
      printf("\n\tEnter No. of Column : ");
      scanf("%d",&c);
      for(i=0;i<r;i++)
           {
              printf("\n\tEnter %d elements for row %d   :  ",c,i+1);
              for(j=0;j<c;j++)
                   {

                        scanf("%d",&a[i][j]);
                        printf("\t\t\t\t\t");
                   }
           }
      printf("\n\tThe array->\n");
      for(i=0;i<r;i++)
           {
              printf("\n\t");
              for(j=0;j<c;j++)
                   {
                        printf("%3d ",a[i][j]);
                   }
           }
      for(i=0;i<r;i++)
           {
              max=a[i][0];
              for(j=0;j<c;j++)
                   {
                        if(a[i][j]<max)
                            {
                                max=a[i][j];
                                c1=j;
                            }
                   }
              for(j=0;j<r;j++)
                   {
                        if(a[j][c1]>max)
                            {
                                max=a[j][c1];
                                r1=j;
                            }
                   }
              printf("\n\t c1 %d r1 %d \n",c1,r1);
                        if(r1==i)
                            {
                                f=1;
                                printf("\n\t%d at %d   %d is saddle point",a[r1][c1],r1+1,c1+1);
                                break;
                            }
           }
      if(f==0)
           {
              printf("\n\tNo Saddle Point ");
           }
      getch();
  }


No comments: