Followers

Showing posts with label MATRIX. Show all posts
Showing posts with label MATRIX. Show all posts

Monday, March 12, 2012

SUM OF DIAGONAL ELEMENTS OF A SQUARE MATRIX

#include<iostream.h>
#include<conio.h>
int x[10][10], n, i, j, b=0 ;

void inpt(int a[10][10] , int n)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<n ; j++)
      cin>>a[i][j];
  cout<<"the matrix :\n";
  for(i=0 ; i<n ; i++)
  {
    cout<<"\n";
    for(j=0 ; j<n ; j++)
      cout<<a[i][j]<<" ";
  }
  cout<<"\n";
}

void otpt(int a[10][10] , int n)
{
  for(i=0 ; i<n/2 ; i++)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      b=b+a[i][j];
      else
      continue;
    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      b=b+a[i][j];
      else
      continue;
    }
  }
  cout<<"\nSum of diagonal elements = "<<b;
}

void main()
{
  clrscr();
  cout<<"enter the number of rows or columns : ";
  cin>>n;
  cout<<"enter a matrix : ";
  inpt(x,n);
  otpt(x,n);
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)


DIAGONAL ELEMENTS IN A SQUARE MATRIX


#include<iostream.h>
#include<conio.h>
int x[10][10], n, i, j;

void inpt(int a[10][10] , int n)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<n ; j++)
      cin>>a[i][j];
  cout<<"The matrix :\n";
  for(i=0 ; i<n ; i++)
  {
    cout<<"\n";
    for(j=0 ; j<n ; j++)
      cout<<a[i][j]<<"  ";
  }
  cout<<"\n";
}

void otpt(int a[10][10] , int n)
{
  for(i=0 ; i<n/2 ; i++)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      cout<<a[i][j]<<" , ";

    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      {
      cout<<a[i][j];
      if(i==n/2)
        cout<<" .";
      else
        cout<<" , ";
      }
    }
  }

}

void main()
{
  clrscr();
  cout<<"Enter the number of rows or columns : ";
  cin>>n;
  cout<<"Enter a matrix : ";
  inpt(x,n);
  cout<<"Diagonal elements : ";
  otpt(x,n);
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

Saturday, December 3, 2011

DOUBLE OF A MATRIX

#include<iostream.h>
#include<conio.h>

void main()
{
 int x[10][10], y[10][10], n, m, i, j;
 cout<<"\nEnter the number of rows :";
 cin>>n;
 cout<<"Enter the number of columns :";
 cin>>m;
 cout<<"Enter the matrix X :\n";
 for(i=0 ; i<n ; i++)
 {
   for(j=0 ; j<m ; j++)
     cin>>x[i][j];
 }

 cout<<"The matrix X :\n";
 for(i=0 ; i<n ; i++)
 {
   cout<<"\n";
   for(j=0 ; j<m ; j++)
     cout<<" "<<x[i][j];
 }


 for(i=0 ; i<n ; i++)
 {
   for(j=0 ; j<m ; j++)
     y[i][j]= 2*x[i][j];
 }
 cout<<"\nThe double of the given matrix : ";

 for(i=0 ; i<n ; i++)
 {
   cout<<"\n";
   for(j=0 ; j<m ; j++)
     cout<<" "<<y[i][j];
 }

 getch();
}
.................................................................
OUTPUT:(click to enlarge the image)

SUM OF TWO MATRICES : SIMPLE

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
 int x[10][10], y[10][10], z[10][10] , n, m, i, j;
 char dec;
 cout<<"The number of rows and columns shoud be equal in both the matrices for any \narithmentic operation ";
 do{
    cout<<"\nEnter the number of rows :";
    cin>>n;
    cout<<"Enter the number of columns :";
    cin>>m;
    cout<<"Enter the matrix X :\n";
    for(i=0 ; i<n ; i++)
    {
      for(j=0 ; j<m ; j++)
      cin>>x[i][j];
    }

    cout<<"The matrix X :\n";
    for(i=0 ; i<n ; i++)
    {
      cout<<"\n";
      for(j=0 ; j<m ; j++)
      cout<<" "<<x[i][j];
    }
    cout<<"\nIs the matrix correct? (Y/N) : ";
    cin>>dec;
 }while(dec=='N'|| dec=='n');
do{
   cout<<"\nEnter the matrix Y :\n";
   for(i=0 ; i<n ; i++)
   {
     for(j=0 ; j<m ; j++)
       cin>>y[i][j];
   }

   cout<<"The matrix Y :\n";
   for(i=0 ; i<n ; i++)
   {
     cout<<"\n";
     for(j=0 ; j<m ; j++)
       cout<<" "<<y[i][j];
   }
   cout<<"\nIs the matrix correct? (Y/N) : ";
      cin>>dec;

}while(dec=='N'|| dec=='n');

 for(i=0 ; i<n ; i++)
 {
   for(j=0 ; j<m ; j++)
     z[i][j]= x[i][j] + y[i][j];
 }
 cout<<"\nThe sum of the two given matrices : ";

 for(i=0 ; i<n ; i++)
 {
   cout<<"\n";
   for(j=0 ; j<m ; j++)
     cout<<" "<<z[i][j];
 }

 getch();
}
.................................................................
OUTPUT:(click to enlarge the image)


SUM OF TWO MATRICES : SPECIAL DESIGN

#include<iostream.h>
#include<conio.h>
int n, m , x[3][3], y[3][3], z[3][3], i, j, k, l;

void inpt(int a[3][3] , int n , int m)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<m ; j++)
      cin>>a[i][j];
  cout<<"the matrix :\n";
  for(i=0 ; i<n ; i++)
  {
    cout<<"\n";
    for(j=0 ; j<m ; j++)
      cout<<a[i][j];
  }
  cout<<"\n";
}

void sum(int x[3][3], int y[3][3] , int z[3][3], int n, int m)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<m ; j++)
      z[i][j]= x[i][j] + y[i][j];
}

void otpt(int x[3][3], int y[3][3] , int z[3][3], int n, int m)
{
  for(i=0 ; i<n ; i++)
  {
    if(i==1)
    {
      for(j=1 ; j<=1 ; j++)
      cout<<" "<<x[i][j]<<" +";
      for(k=1 ; k<=1 ; k++)
      cout<<" "<<y[i][k]<<" =";
      for(l=1 ; l<=1 ; l++)
      cout<<" "<<z[i][l];
    }

    else
    {
      for(j=0 ; j<3 ; j++)
      {
      if(j==1)
        continue;
      cout<<x[i][j]<<" ";
      }

      for(k=0 ; k<3 ; k++)
      {
      if(k==1)
        continue;
      cout<<y[i][k]<<" ";
      }

      for(l=0 ; l<3 ; l++)
      {
      if(l==1)
        continue;
      cout<<z[i][l]<<" ";
      }
    }
    cout<<"\n";
  }
}

void main()
{
  clrscr();
  cout<<"Enter the number of rows : ";
  cin>>n;
  cout<<"Enter the number of columns : ";
  cin>>m;
  cout<<"Enter the 1st matrix :\n";
  inpt(x,n,m);
  cout<<"Enter the 2nd matrix :\n";
  inpt(y,n,m);
  sum(x,y,z,n,m);
  cout<<"\nThe sum of the given matrices :\n";
  otpt(x,y,z,n,m);
  getch();
}
.................................................................
OUTPUT:(CLICK TO ENLARGE)