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 Matrices. Show all posts
Showing posts with label Matrices. Show all posts

Friday, September 9, 2011

Row Average, column Average and Matrix Average of a Random Matrix


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

int mat[max][max];

void main()
{
int i,j;
int row_sum[max];
int col_sum[max];
int mat_sum = 0;
clrscr();
randomize();
printf("\n\n RANDOM MATRIX \n\n");
for(i=0;i<max;i++)
{
 row_sum[i] = 0;
 printf("\n");
 for(j=0;j<max;j++)
 {
  if(i==0)
   col_sum[j] = 0;
  mat[i][j] = random(10);
  col_sum[j] += mat[i][j];
  row_sum[i] += mat[i][j];
  mat_sum += mat[i][j];
  printf(" %d",mat[i][j]);
 }
}

getch();

printf("\n\n Row Sums");
for(i =0; i<max; i++)
 printf("\n Row %d = %d",i+1,row_sum[i]);

printf("\n\n Column Sums");
for(i =0; i<max; i++)
 printf("\n Column %d = %d",i+1,col_sum[i]);

printf("\n\n Matrix Sum = %d",mat_sum);
getch();
}

Monday, January 10, 2011

Multiplication of Matrices

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


 void in_mat(int[max][max],int,int);
 void disp_mat(int[max][max],int,int);
 void mul_mat(int[max][max],int[max][max],int[max][max],int,int,int);


 void main()
  {
   int x[max][max],y[max][max],z[max][max],a,b,c,d;
   clrscr();
   do
   {
   cout<<"\n\n Enter the numbers of rows n columns of matrix #1 : ";
   cin>>a>>b;
   cout<<"\n\n Enter the numbers of rows n columns of matrix #2 : ";
   cin>>c>>d;
   if ( b != c )
    cout<<"\n\n Matrices can't be multiplied .. !! ";
   }while( b != c );
   cout<<"\n\n Now, The Matrices can be multiplied .. !! ";
   getch();


   cout<<"\n\n\n\n Enter the elements for the following matrices \n";
   cout<<"\n\n Enter for the Matrix #1 \n";
   in_mat(x,a,b);
   cout<<"\n\n Enter for the Matrix #2 \n";
   in_mat(y,c,d);
   clrscr();
   cout<<"\n\n The following matrices are : ";
   cout<<"\n\n\t\t Matrix #1 :- ";
   disp_mat(x,a,b);
   cout<<"\n\n\t\t Matrix #2 :- ";
   disp_mat(y,c,d);
   mul_mat(x,y,z,a,d,b);
   cout<<"\n\n\t The Product of the above two matrices are \n";
   disp_mat(z,a,d);
   getch();
  }


 void in_mat(int mat[max][max],int row,int col)
  {
   int i,j;
   for(i=0;i<row;i++)
    {
     cout<<"\n Enter its elements for Row #"<<i+1<<" : ";
     for(j=0;j<col;j++)
       cin>>mat[i][j];
    }
  }


 void disp_mat(int mat[max][max],int row,int col)
  {
   int i,j;
   for(i=0;i<row;i++)
    {
     cout<<"\n\t Row #"<<i+1<<" :";
     for(j=0;j<col;j++)
      cout<<"  "<<mat[i][j];
    }
  }


 void mul_mat(int mat1[max][max],int mat2[max][max],int mat3[max][max],int r1,int c2,int r2c1)
  {
   int i,j,k;
   for(i=0;i<r1;i++)
    {
     for(j=0;j<c2;j++)
      {
       mat3[i][j] = 0;
       for(k=0;k<r2c1;k++)
mat3[i][j] += mat1[i][k] * mat2[k][j];
      }
    }
  }