ARRAY Examples

C programme



     

Matrix Operations :





SCROLL  DOWN  FOR  ABOVE  PROGRAMS

Program :


/*  Program to demonstrate addition of two matrices.

Creation Date : 30 MAR 2012 11:35:00 PM

Author : HARISH */

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

void main()
{
 int a[3][3],b[3][3],c[3][3],i,j;
 clrscr();
 printf("\n\t Enter First Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("\n\t Enter Second Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   c[i][j]=a[i][j]+b[i][j];
  }
 }
 printf("\n\t Matrix Addition is : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t %d",c[i][j]);
  }
  printf("\n");
 }
 getch();
}

Output :


 Enter First Matrix : 2 1 2
 3 2 1
 2 3 3
 
 Enter Second Matrix : 2 1 0
 2 3 0
 1 1 2
 
 Matrix Addition is :
 
 4 2 2
 5 5 1
 3 4 5_

















     Subtraction of Matrices :


Program :


/*  Program to demonstrate subtraction of two matrices.

Creation Date : 30 MAR 2012 11:42:08 PM

Author : HARISH */

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

void main()
{
 int a[3][3],b[3][3],c[3][3],i,j;
 clrscr();
 printf("\n\t Enter First Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("\n\t Enter Second Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   c[i][j]=a[i][j]-b[i][j];
  }
 }
 printf("\n\t Matrix Subtraction is : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t %d",c[i][j]);
  }
  printf("\n");
 }
 getch();
}

Output :


 Enter First Matrix : 2 1 2
 3 2 1
 2 3 3
 
 Enter Second Matrix : 2 1 0
 2 3 0
 1 1 2
 
 Matrix Subtraction is :
 
 0 0 2
 1 -1 1
 1 2 1_


















      Multiplication of two Matrices :  

Program :


/*  Program to demonstrate multiplication of two matrices.

Creation Date : 30 MAR 2012 11:50:11 PM

Author : HARISH */

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

void main()
{
 int a[3][3],b[3][3],c[3][3],i,j,k;
 clrscr();
 printf("\n\t Enter First Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("\n\t Enter Second Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   c[i][j]=0;
   for(k=0;k<3;k++)
   {
   c[i][j]=c[i][j]+a[i][k]*b[k][j];
   }
  }
 }
 printf("\n\t Matrix Multiplication is : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t %d",c[i][j]);
  }
  printf("\n");
 }
 getch();
}

Output :


 Enter First Matrix : 1 2 1
 3 2 1
 1 2 1
 
 Enter Second Matrix : 3 3 3
 1 2 1
 1 1 1
 
 Matrix Multiplication is :
 
 6 8 6
 12 14 12
 6 8 6_




















     Transpose of Matrices :

Program :


/* Program to demonstrate transpose of matrix.

Creation Date : 30 MAR 2012 12:03:59 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j;
 clrscr();
 printf("\n\t Enter Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("\n\t Transpose of Matrix is : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t%d",a[j][i]);
  }
  printf("\n");
 }
 getch();
}

Output :


 Enter Matrix : 2 1 2
 3 2 1
 2 3 3
 
 Transpose of Matrix is :
 
 2 3 2
 1 2 3
 2 1 3_



















     Check Matrix is Lower Triangular or Not :  

Program :


/* Program to check lower triangular matrix or not.

Creation Date : 30 MAR 2012 01:14:54 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j,flg=0;
 clrscr();
 printf("\n\t Enter 3*3 Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 } 
 for(i=0;i<3;i++)
 { 
  for(j=0;j<3;j++)
  {
   if(a[i]<a[j] && a[i][j]==0)
   {
    flg=flg+1;
   }
  }
 }
 if(flg==3)
    printf("\n\n Lower triangular matrix !");
 else
    printf("\n\n Not lower triangular matrix !");
 getch();
}

Output :


 Enter 3*3 Matrix : 
 1 0 0
 1 1 0
 2 2 3
 
 Lower triangular matrix !_


















     Check Matrix is Upper Triangular or Not :

Program :


/* Program to check upper triangular matrix or not.

Creation Date : 30 MAR 2012 01:14:54 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j,flg=0;
 clrscr();
 printf("\n\t Enter 3*3 Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 } 
 for(i=0;i<3;i++)
 { 
  for(j=0;j<3;j++)
  {
   if(a[i]>a[j] && a[i][j]==0)
   {
    flg=flg+1;
   }
  }
 }
 if(flg==3)
    printf("\n\n Upper triangular matrix !");
 else
    printf("\n\n Not Upper triangular matrix !");
 getch();
}

Output :


 Enter 3*3 Matrix : 
 1 2 3
 0 1 1
 0 0 3
 
 Upper triangular matrix !_

















     Check Matrix is UNITY/Identity or Not :

Program :


/* Program to check unit/identity matrix or not.

Creation Date : 30 MAR 2012 08:28:08 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j,flg=0,flg2=0,flg3=0;
 clrscr();
 printf("\n\t Enter 3*3 Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 } 
 for(i=0;i<3;i++)
 { 
  for(j=0;j<3;j++)
  {
   if(a[i]<a[j] && a[i][j]==0)
   {
    flg=flg+1;
   }
   if(a[i]>a[j] && a[i][j]==0)
   {
    flg2=flg2+1;
   }
   if(a[i][j]==1)
   {
    flg3=flg3+1;
   }
  }
 }
 if(flg==3 && flg2==3 && flg3==3)
    printf("\n\n Unit/Identity matrix !");
 else
    printf("\n\n Not Unit/Identity matrix !");
 getch();
}


Output :


 Enter 3*3 Matrix : 
 1 0 0
 0 1 0
 0 0 1
 
 Unit/Identity matrix !_




















     Check Matrix is ZERO/NULL Matrix or Not :

Program :


/* Program to check zero/NULL matrix or not.

Creation Date : 30 MAR 2012 02:09:04 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j,flg=0;
 clrscr();
 printf("\n\t Enter 3*3 Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 } 
 for(i=0;i<3;i++)
 { 
  for(j=0;j<3;j++)
  {
   if(a[i][j]==0)
   {
    flg=flg+1;
   }
  }
 }
 if(flg==9)
    printf("\n\n Zero/Null matrix !");
 else
    printf("\n\n Not Zero/Null matrix !");
 getch();
}

Output :


 Enter 3*3 Matrix : 
 0 0 0
 0 0 0
 0 0 0
 
 Zero/Null matrix !_

















     Check Matrix is Diagonal Matrix or Not :

Program :


/* Program to check diagonal matrix or not.

Creation Date : 30 MAR 2012 01:14:54 PM

Author : HARISH */

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

void main()
{
 int a[3][3],i,j,flg=0,flg2=0;
 clrscr();
 printf("\n\t Enter 3*3 Matrix : ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 } 
 for(i=0;i<3;i++)
 { 
  for(j=0;j<3;j++)
  {
   if(a[i]<a[j] && a[i][j]==0)
   {
    flg=flg+1;
   }
   if(a[i]>a[j] && a[i][j]==0)
   {
    flg2=flg2+1;
   }
  }
 }
 if(flg==3 && flg2==3)
    printf("\n\n Diagonal matrix !");
 else
    printf("\n\n Not Diagonal matrix !");
 getch();
}

Output :


 Enter 3*3 Matrix : 
 2 0 0
 0 5 0
 0 0 1
 
 Diagonal matrix !_












No comments: