Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Tuesday, March 22, 2011

Program For Finding The Saddle Point Of A Matrix with Example


Program

#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 3

void main()
{
   void read(int a[][COL],int,int);
   void dis(int a[][COL],int,int);
   int sadd_pt(int a[][COL],int,int,int *,int*);
   int i,a[3][3],m=0,n=0;
   clrscr();
   read(a,ROW,COL);
   printf("\nThe matrix is \n");
   dis(a,ROW,COL);
   i=sadd_pt(a,3,3,&m,&n);
   printf("The saddle point is %d &its position is row : %d col :
%d\n",
      i,m+1,n+1);
   getch();
}
void read(int c[][3] ,int i ,int k)
{
    int j,l;
   printf("Enter the array \n");
   for(j=0;j<i;j++)
       for(l=0;l<k;l++)
              scanf("%d",&c[j][l]);
   fflush(stdin);
}
void dis(int d[][3],int i,int k)
{
   int j,l;
   for(j=0;j<i;j++)
   {
      for(l=0;l<k;l++)
            printf("\t%d",d[j][l]);
      printf("\n");
   }
}
int sadd_pt(int mat[][3],int k ,int l,int *row,int *col)
{
   int min=32767,i=0,j,m,n,p=0;
   while(i<k)
   {
      min=32767;
      m=i;
      p=0;
      for(j=0;j<l;j++)
      {
         if(mat[i][j]<min)
         {
            min=mat[i][j];
            n=j;
         }
      }
      for(j=0;j<k;j++)
             if(min>=mat[j][n])
                    p++;

      if(p==3)
      {
            *row=m;
            *col=n;
            return(min);
      }
      i++;
   }
   printf("No saddle point exists\n");
   getch();
   exit(0);
}

Example

Input
Enter the array
20 30 40
56 78 45
1 2 3
Output
The matrix is
20 30 40
56 78 45
1 2 3
The saddle point is 45 and its position is row 2, column 3

Program for Transpose Of A Matrix with Example -2


Alternative Version of the Program

This is another version of the transpose program. Here a separate matrix is used to hold the result of transposition.

#include<stdio.h>
#include<conio.h>
#define ROW 3 #define COL 3
void main() {
   void read(int a[][COL],int,int);
   void dis(int a[][COL],int,int);
   void trans(int a[][COL],int b[][COL],int,int);
   int a[3][3],b[3][3],i,j;
   clrscr();
   read(a,ROW,COL);
   printf("\nThe matrix is \n");
   dis(a,ROW,COL);
   trans(a,b,ROW,COL);
   printf("The tranpose of the matrix is\n");
   dis(b,ROW,COL);
   getch();
}
void read(int c[3][3] ,int i ,int k)
{
   int j,l;
   printf("Enter the array \n");
   for(j=0;j<i;j++)
       for(l=0;l<k;l++)
              scanf("%d",&c[j][l]);
   fflush(stdin);
}
void dis(int d[3][3 ],int i,int k)
{
   int j,l;
   for(j=0;j<i;j++)
   {
      for(l=0;l<k;l++)
            printf("%d ",d[j][l]);
      printf("\n");
   }
}
void trans(int mat[][3],int tr_mat[][3], int k ,int l)
{
   int i,j;
   for(i=0;i<k;i++)
   for(j=0;j<l;j++)
   {
          tr_mat[i][j]=mat[j][i];
   }
}

Example

Input
Enter the array 1
2
3
4
5
6
7
8
9
Output
The matrix is
1 2 3
4 5 6
7 8 9
The transpose of the matrix is
1 4 7
2 5 8
3 6 9

Program for Transpose Of A Matrix with Example


Program

#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 3

void main()
{
   void read(int a[][COL],int,int);
   void dis(int a[][COL],int,int);
   void trans(int a[][COL],int,int);
   int a[3][3];
   clrscr();
   read(a,ROW,COL);
   printf("\nThe matrix is \n");
   dis(a,ROW,COL);
   trans(a,ROW,COL);
   printf("The tranpose of the matrix is\n");
   dis(a,ROW,COL);
   getch();
}
void read(int c[3][3] ,int i ,int k)
{
    int j,l;
   printf("Enter the array \n");
   for(j=0;j<i;j++)
      for(l=0;l<k;l++)
             scanf("%d",&c[j][l]);
   fflush(stdin);
}
void dis(int d[3][3 ],int i,int k)
{
   int j,l;
   for(j=0;j<i;j++)
   {
      for(l=0;l<k;l++)
             printf("%d ",d[j][l]);
      printf("\n");
   }
}
void trans(int mat[][3],int k ,int l)
{
   int i,j,temp;
   for(i=0;i<k;i++)
       for(j=i+1;j<l;j++)
      {
         temp=mat[i][j];
         mat[i][j]=mat[j][i];
         mat[j][i]=temp;
      }
}
Example
Input
Enter the array
12
13
14
15
16
17
18
19
11
Output
The matrix is
12 13 14
15 16 17
18 19 11
The transpose of the matrix is
12 15 18
13 16 19
14 17 11