8.14 – Matrix programs to practice Part-4

by subbu on November 7, 2013

Specification: Accept a square matrix and print whether the matrix is an identity matrix

Show Logic

Identity Matrix: In a square matrix if all the diagonal elements are 1’s and left all the elements are 0’s then the matrix is called identity matrix.

Identity matrix

Logic:

If any diagonal element is not equal to 1 or any non diagonal element is not a zero then the matrix is not an identity matrix otherwise it is an identity matrix.
When i==j then a[i][j] would be a diagonal element, which must be 1 to become an identity matrix.
When i!=j then a[i][j] would be a non-diagonal element, which must be 0 to become an identity matrix


flag=1;
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   if((i==j&&a[i][j]!=1)||(i!=j&&a[i][j]!=0)) /* checking for non-identity matrix */
   {
     flag=0;
     break;
   }

If the flag is unchanged (1) then the matrix is an identity matrix, if the flag is assigned with 0 then the matrix is not an identity matrix.

if(flag)
   printf("Identity matrix");
else
   printf("Not an identity matrix");

Program:

#include<stdio.h>
#include<math.h>
int main()
{
int a[50][50],n,i,j,flag;
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
 for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);
flag=1;
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   if((i==j&&a[i][j]!=1)||(i!=j&&a[i][j]!=0)) /* checking for non-identity matrix */
   {
     flag=0;
     break;
   }
if(flag)
   printf("Identity matrix");
else
   printf("Not an Identity matrix");
return 0;
}

Execution 1:
Enter the class of square matrix:
3
Enter 3×3 matrix:
1  0  0
0  1  0
0  0  1
Identity matrix

Execution 2:
Enter the class of square matrix:
4
Enter 4×4 matrix:
1  0  0  0
0  1  0  2
0  0  1  0
0  0  0  1
Not an Identity matrix

Specification: Accept a square matrix and print whether the matrix is a magic square or not

Show Logic

Magic square: It is a square matrix, the sum of diagonal, reverse diagonal, any row and any column are equal.

Logic:

We first calculate the sum of diagonal elements (dsum) and then compare with the sum of reverse diagonal elements, sum of every row and sum of every column. if any where we find inequality then flag is assigned with 0

if(dsum!=sum)
  flag=0;

Program:

#include<stdio.h>
int main()
{
int n,i,j,sum,dsum,flag,a[50][50];
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
 for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);
flag=1;
/* Sum of diagonal elements */
dsum=0;
for(i=0;i<n;i++)
  dsum=dsum+a[i][i];
/* Sum of reverse diagonal elements */
for(sum=0,i=0,j=n-1;i<n;i++,j--)
  sum=sum+a[i][j];
if(sum!=dsum)
  flag=0;
for(i=0;i<n;i++)
{
  for(sum=0,j=0;j<n;j++)
    sum=sum+a[i][j];       /* calculating sum of rows */
  if(sum!=dsum)
   flag=0;
}
for(i=0;i<n;i++)
{
  for(sum=0,j=0;j<n;j++)
    sum=sum+a[j][i];       /* calculating sum of columns */
  if(sum!=dsum)
    flag=0;
}
if(flag)
   printf("Magic square");
else
   printf("Not a magic square");
return 0;
}

Execution 1:
Enter the class of square matrix:
3
Enter 3×3 matrix:
4  9  2
3  5  7
8  1  6
Magic square

Execution 2:
Enter the class of square matrix:
4
Enter 4×4 matrix:
16      3       2       13
5       10      11      8
9       6       7       12
4       15      14      1
Magic square

Specification: Accept a nxm matrix and print the elements helical or in spiral order

Show Logic

spiral or helical traversal of a matrix

We traverse part by part and layer by layer until visiting n*m elements.

sri = 0, sci = 0,eri=n,eci=m;
/*  sri - starting row index
eri - ending row index
sci - starting column index
eci - ending column index
i - iterator
*/

helical or spiral traversal of matrix

/* Print the first row from the remaining rows */
for (i = sci; i <= eci; i++)
   printf("%d ", a[sri][i]);
sri++;   /* resetting the starting row index */
/* Print the last column from the remaining columns */
for (i = sri; i <=eri; i++)
   printf("%d ", a[i][eci]);
eci--;    /* resetting the ending column index */
/* Print the last row from the remaining rows */
if ( sri <= eri)
{
for (i = eci; i >= sci; --i)
   printf("%d ", a[eri][i]);
eri--;    /* resetting the ending row index  */
}
/* Print the first column from the remaining columns */
if (sci <= eci)
{
for (i = eri; i >= sri; --i)
   printf("%d ", a[i][sci]);
sci++;   /* resetting starting column index */
}

Program:

#include <stdio.h>
int main()
{
int a[50][50],n,m,i,j,sri,eri,sci,eci;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
    scanf("%d",&a[i][j]);
sri = 0, sci = 0,eri=n-1,eci=m-1;
   /*  sri - starting row index
       eri - ending row index
       sci - starting column index
       eci - ending column index
       i - iterator
   */
printf("\n");
while (sri <= eri && sci <= eci)
{
   /* Print the first row from the remaining rows */
   for (i = sci; i <= eci; i++)
       printf("%d ", a[sri][i]);
   sri++;
   /* Print the last column from the remaining columns */
   for (i = sri; i <=eri; i++)
       printf("%d ", a[i][eci]);
   eci--;
   /* Print the last row from the remaining rows */
   if ( sri <= eri)
   {
      for (i = eci; i >= sci; --i)
          printf("%d ", a[eri][i]);
      eri--;
   }
   /* Print the first column from the remaining columns */
   if (sci <= eci)
   {
      for (i = eri; i >= sri; --i)
          printf("%d ", a[i][sci]);
      sci++;
   }
}
return 0;
}

Execution:
Enter the class of matrix:
4 5
Enter 4×5 matrix:
12  45  67  88  22
32  56  44  23  25
78  50  33  80  16
76  23  31  27  11
12  45  67  88  22  25  16  11  27  31  23  76  78  32  56  44  23 80  33  50

Specification: Accept a 2×2 matrix and print its determinant.

Show Logic

Determinant of a 2×2 matrix

determinant of 2x2 matrix

Logic:

The determinant of a[2][2] array is

d=a[0][0]*a[1][1]-a[0][1]*a[1][0];

Program:

#include<stdio.h>
int main()
{
int a[2][2],i,j,d;
printf("Enter a 2x2 matrix:\n");
for(i=0;i<2;i++)
  for(j=0;j<2;j++)
    scanf("%d",&a[i][j]);
d=a[0][0]*a[1][1]-a[0][1]*a[1][0];
printf("The determinant of matrix %d",d);
return 0;
}

Execution:
Enter a 2×2 matrix:
5  6
3  4
The determinant of matrix 2

Specification: Accept a 3×3 matrix and print its determinant

Show Logic

Determinant of a 3×3 matrix is

 Determinant of a 3x3 matrix

Logic:

The determinant of a[3][3] array is

3x3 matrix

d=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);

Program:

#include<stdio.h>
int main()
{
int a[3][3],i,j,d;
printf("Enter a 3x3 matrix:\n");
for(i=0;i<3;i++)
  for(j=0;j<3;j++)
     scanf("%d",&a[i][j]);
d=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);
printf("The determinant of matrix %d",d);
return 0;
}

Execution:
Enter a 3×3 matrix:
3 4 6
1 3 8
9 7 2
The determinant of matrix 10

Specification: Accept a 2×2 matrix and print the inverse matrix

Show Logic

Inverse of a 2×2 matrix

inverse of a 2x2 matrix

To find the inverse, swap the positions of a and d, put minus before b and c and divide every element by determinant.

Logic:

Inverse of a 2×2 array

2x2 matrix

d=a[0][0]*a[1][1]-a[0][1]*a[1][0]; /*calculating determinant */
/* swapping a[0][0] and a[1][1] */
temp=a[0][0];
a[0][0]=a[1][1];
a[1][1]=temp;
/* adding – to a[0][1] and a[1][0] */
a[0][1]=-1*a[0][1];
a[1][0]=-1*a[1][0];
/* dividing every element by determinant */
for(i=0;i<2;i++)
  for(j=0;j<2;j++)
     a[i][j]=a[i][j]/d;

Program:

#include<stdio.h>
int main()
{
int i,j;
float a[2][2],d,temp;
printf("Enter a 2x2 matrix:\n");
for(i=0;i<2;i++)
  for(j=0;j<2;j++)
    scanf("%f",&a[i][j]);
d=a[0][0]*a[1][1]-a[0][1]*a[1][0]; /*calculating determinant */
/* swapping a[0][0] and a[1][1] */
temp=a[0][0];
a[0][0]=a[1][1];
a[1][1]=temp;
/* adding – to a[0][1] and a[1][0] */
a[0][1]=-1*a[0][1];
a[1][0]=-1*a[1][0];
/* dividing every element by determinant */
for(i=0;i<2;i++)
  for(j=0;j<2;j++)
    a[i][j]=a[i][j]/d;
printf("The inverse of matrix:\n");
for(i=0;i<2;i++)
{
   for(j=0;j<2;j++)
      printf("%8.1f",a[i][j]);
   printf("\n\n");
}
return 0;
}

Execution:
Enter a 2×2 matrix:
4  7
2  6
The inverse of matrix:
0.6    -0.7
-0.2     0.4
Specification: Accept a 3×3 matrix and print the inverse of matrix

Show Logic

Inverse of a 3×3 matrix

inverse of a 3x3 matrix

Logic:

3x3 matrix1

Calculating determinant

d=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);

Calculating cofactor of A and storing into c

cofactor 1

c[0][0]=a[1][1]*a[2][2]-a[1][2]*a[2][1];

cofactor 2

c[0][1]=a[1][0]*a[2][2]-a[1][2]*a[2][0];

cofactor 3

c[0][2]=a[1][0]*a[2][1]-a[1][1]*a[2][0];

cofactor 4

c[1][0]=a[0][1]*a[2][2]-a[2][1]*a[0][2];
c[1][1]=a[0][0]*a[2][2]-a[2][0]*a[0][2];
c[1][2]=a[0][0]*a[2][1]-a[0][1]*a[2][0];
c[2][0]=a[0][1]*a[1][2]-a[0][2]*a[1][1];
c[2][1]=a[0][0]*a[1][2]-a[0][2]*a[1][0];
c[2][2]=a[0][0]*a[1][1]-a[0][1]*a[1][0];

Storing transpose of C into X, Now x will have Adjoint of A

for(i=0;i<3;++)
  for(j=0;j<3;j++)
    x[j][i]=c[i][j];

Dividing every element by determinant in adjoint (X) results inverse of matrix

for(i=0;i<3;i++)
  for(j=0;j<3;j++)
     x[i][j]=x[i][j]/d;

Program:

#include<stdio.h>
int main()
{
int a[3][3],c[3][3],d,i,j;
float x[3][3];
printf("Enter a 3x3 matrix:\n");
for(i=0;i<3;i++)
  for(j=0;j<3;j++)
     scanf("%d",&a[i][j]);
d=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);
c[0][0]=a[1][1]*a[2][2]-a[1][2]*a[2][1];
c[0][1]=a[1][0]*a[2][2]-a[1][2]*a[2][0];
c[0][2]=a[1][0]*a[2][1]-a[1][1]*a[2][0];
c[1][0]=a[0][1]*a[2][2]-a[2][1]*a[0][2];
c[1][1]=a[0][0]*a[2][2]-a[2][0]*a[0][2];
c[1][2]=a[0][0]*a[2][1]-a[0][1]*a[2][0];
c[2][0]=a[0][1]*a[1][2]-a[0][2]*a[1][1];
c[2][1]=a[0][0]*a[1][2]-a[0][2]*a[1][0];
c[2][2]=a[0][0]*a[1][1]-a[0][1]*a[1][0];
for(i=0;i<3;i++)
 for(j=0;j<3;j++)
   x[j][i]=c[i][j];
for(i=0;i<3;i++)
  for(j=0;j<3;j++)
   x[i][j]=x[i][j]/d;
printf("Invese of matrix:\n");
for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
    printf("%7.1f",x[i][j]);
   printf("\n\n");
}
return 0;
}

Execution:
Enter a 3×3 matrix:
1 2 3
0 1 4
5 6 0
Inverse of matrix:
-24.0  -18.0    5.0
-20.0  -15.0    4.0
-5.0   -4.0    1.0

Note: It is a lengthy procedure to calculate inverse of a matrix as the number of dimensions grow and logic also changes dimension to dimension. We will see how we can print transpose of nxn matrix using recursion in future sessions.

Previous post:

Next post: