Wednesday, 19 October 2011

"C program for Matrix operation"

#include<stdio.h>
#include<conio.h>
void addition();
void substraction();
void multiplication();
void magic_sqr();
void transpose();
void saddle_pt();
int a[10][10],b[10][10],d[10][10],rsum[20],csum[20];
int r1,r2,c1,c2,m,n,i,j,d1,d2,ar,ac,val,flag=0,rowvalue,colvalue;
void main()
{
  int c;
  char ch;
  do
  {
    clrscr();
    printf("\n\n\t MENU");
    printf("\n\n\t 1.Addition");
    printf("\n\n\t 2.Subtraction");
    printf("\n\n\t 3.Multiplication");
    printf("\n\n\t 4.Transpose");
    printf("\n\n\t 5.Saddle Point");
    printf("\n\n\t 6.Magic Square");
    printf("\n\n\t 7.Exit");
    printf("\n\n\t Enter your choice(1,2,3,4,5,6,7):");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
            addition();
        break;
        case 2:
            substraction();
        break;
        case 3:
            multiplication();
        break;
        case 4:
            transpose();
        break;
        case 5:
            saddle_pt();
        break;
        case 6:
            magic_sqr();
        break;
        case 7:
            exit(0);
        break;
        default:
        printf("\n invalid choice:");
    }
    printf("\n\n\t Do you want to continue in same menu(y/n):");
    c=getch();
  }
  while(c=='y'||c=='Y');
  getch();
}

void addition()
{
   int i,j;
   int d[10][10];
   printf("\n\n\t Enter the size of matrix:");
   scanf("%d%d",&m,&n);
   printf("\n\n\t Enter the elements of first matrix:");
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
       scanf("%d",&a[i][j]);
    }
   }
   printf("\n\n\t Enter the elements of second matrix:");
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
        scanf("%d",&b[i][j]);
    }
   }
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
        d[i][j]=a[i][j]+b[i][j];
    }
   }
   printf("\n\n\t The addition of matrix is:");
   for(i=0;i<m;i++)
   {
    printf("\n\n\t");
    for(j=0;j<n;j++)
    {
        printf("%d\t",d[i][j]);
    }
   }
}

void substraction()
{
  int i,j;
  int d[10][10];
  printf("\n\n\t Enter the size of matrix:");
  scanf("%d%d",&m,&n);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        scanf("%d",&a[i][j]);
    }
  }
  printf("\n\n\t Enter the elements of second matrix:");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        scanf("%d",&b[i][j]);
    }
  }
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        d[i][j]=a[i][j]-b[i][j];
    }
  }
  printf("\n\n\t The subtraction of matrix is:");
  for(i=0;i<m;i++)
  {
     printf("\n\n\t");
    for(j=0;j<n;j++)
    {
    printf("%d\t",d[i][j]);
    }
  }
}

void multiplication()
{
  int i,j,k,r1,r2,c1,c2;
  int d[10][10];
  printf("\n\n\t Enter the size of first matrix:");
  scanf("%d%d",&r1,&c1);
  printf("\n\n\t Enter the size of second matrix:");
  scanf("%d%d",&r2,&c2);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c1;j++)
    {
        scanf("%d",&a[i][j]);
    }
  }
  printf("\n\n\t Enter the elements of second matrix:");
  for(i=0;i<r2;i++)
  {
    for(j=0;j<c2;j++)
    {
        scanf("%d",&b[i][j]);
    }
  }
  if(c1==r2)
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c2;j++)
    {
        d[i][j]=0;
        for(k=0;k<c2;k++)
        {
            d[i][j]=d[i][j]+(a[i][k]*b[k][j]);
        }
    }
  }
  printf("\n\n\t The multiplication is:");
  for(i=0;i<r1;i++)
  {
    printf("\n\n\t");
    for(j=0;j<c2;j++)
    {
        printf("%d\t",d[i][j]);
    }
  }
}

void transpose()
{
  int i,j,m,n;
  int d[10][10];
  printf("\n\n\t Enter the size of matrix:");
  scanf("%d%d",&m,&n);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<m;i++)
 {
    for(j=0;j<n;j++)
    {
        scanf("\t%d",&a[i][j]);
    }
 }
 for(i=1;i<m;i++)
 {
    for(j=0;j<i;j++)
    {
        d[i][j]=a[i][j];
        a[i][j]=a[j][i];
        a[j][i]=d[i][j];
    }
 }
 printf("\n\n\t The transepose is:");
 for(i=0;i<m;i++)
 {
    printf("\n\n\t");
    for(j=0;j<m;j++)
    {
        printf("%d\t",a[i][j]);
    }
 }
}

void saddle_pt()
{
  int i,j,small,large,rsmall,clarge,r1,c1,flag=0;
  printf("\n\n\t Enter size of matrix:");
  scanf("%d%d",&r1,&c1);
  printf("\n\n\t Enter the elements of matrix:");
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c1;j++)
    {
        scanf("\t%d",&a[i][j]);
    }
  }
  for(i=0;i<r1;i++)
  {
    rowvalue=a[i][0];
    rsmall=0;
    for(j=1;j<c1;j++)
    {
        if(rowvalue>a[i][j])
        {
            rowvalue=a[i][j];
            rsmall=j;
        }
    }
    colvalue=a[0][rsmall];
    clarge=0;
    for(j=1;j<r1;j++)
    {
        if(colvalue<a[j][clarge])
        {
            colvalue=a[j][rsmall];
            clarge=j;
        }
    }
    if(i==clarge)
    {
        flag=1;
        printf("\n Saddle point is present at(%d ,%d):",i,rsmall);
        printf("\n The element is=%d",a[i][rsmall]);
    }
  }
  if(flag==0)
  {
    printf("\n Saddle point is not present:");
  }
}

void magic_sqr()
{
   printf("\n\n\t Enter size of matrix:");
   scanf("%d%d",&r1,&c1);
   printf("\n\n\t Enter the elements of matrix:");
   for(i=0;i<r1;i++)
   {
    for(j=0;j<c1;j++)
    {
        scanf("%d",&a[i][j]);
    }
    }
   if(r1==c1)
   {
    for(i=0;i<c1;i++)
    {
        for(j=0;j<c1;j++)
        {
            rsum[i]=rsum[i]+a[i][j];
        }
    }
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            csum[i]=csum[i]+a[i][j] ;
        }
    }
   }
   for(i=0;i<r1;i++)
   {
    for(j=0;j<r1;j++)
    {
        if(i==j)
        d1=d1+a[i][j];
    }
   }
   for(i=0;i<r1;i++)
   {
    for(j=r1-1;j>=0;j--)
    {
        if(i==j)
        {
            d2=d2+a[i][j];
        }
    }
   }
   ar=rsum[0];              //
   for(i=0;i<r1;i++)
   {
    if(ar!=rsum[i])
    flag=1;
   }
   ac=csum[0];
   for(i=0;i<r1;i++)
   {
    if(ac!=csum[i])
    flag=1;
   }
   if(d1==d2&& ar==ac &&flag==0&&d1==ac)
   {
    printf("\n\n\t Matrix is magic square:");
   }
   else
   {
    printf("\n\t Not Magic square:");
   }
}

/*
OUTPUT:-

     MENU

     1.Addition

     2.Subtraction

     3.Multiplication

     4.Transpose

     5.Saddle Point

     6.Magic Square

     7.Exit

     Enter your choice(1,2,3,4,5,6,7):1


     Enter the size of matrix:2 3


     Enter the elements of first matrix:1
2 3 4 5 6


     Enter the elements of second matrix:1 2 3 4 5 6


     The addition of matrix is:

    2       4       6

    8       10      12



    Enter your choice(1,2,3,4,5,6,7):2


     Enter the size of matrix:2 2


     Enter the elements of first matrix:1 2 3 4


     Enter the elements of second matrix:1 2 3 4


     The subtraction of matrix is:

    0       0

    0       0






     Enter your choice(1,2,3,4,5,6,7):3


     Enter the size of first matrix:2 2


     Enter the size of second matrix:1 2 3 4


     Enter the elements of first matrix:1 2 3 4


     Enter the elements of second matrix:

     The multiplication is:

        7   10

        15  22




     Enter your choice(1,2,3,4,5,6,7):4


     Enter the size of matrix:2 2


     Enter the elements of first matrix:1 2 3 4


     The transepose is:

    1       3

    2       4


     Enter your choice(1,2,3,4,5,6,7):5


     Enter size of matrix:2 2


     Enter the elements of matrix:1 2 3 4

 Saddle point is present at(1 ,0):
 The element is=3



     Enter your choice(1,2,3,4,5,6,7):6


     Enter size of matrix:2 2


     Enter the elements of matrix:1 1 1 1


     Matrix is magic square:



*/







                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               





































                                                                               
                                                                               
                                                                               
                                                                               
                                              

                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                          







                                                                               











































































                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               






   

2 comments:

Few Linux App & commands which is going to used to Build AOSP(Android Open Source Project) Source Code

1. sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32n...