8.12 – Matrix – Programs to practice Part-2

by subbu on November 3, 2013

Specification: Accept a square matrix and print all the diagonal elements and trace.

Show Logic

Trace: Sum of diagonal elements is called trace.

Trace of a square matrix

Diagonal elements are the element whose row index and the column index are equal. According to the above example diagonal elements are
a[0][0] a[1][1] a[2][2] …
a[n-1][n-1] We can traverse only the diagonal elements using for i=0, 1, 2 … n-1 and a[i][i]

for(i=0;i<n;i++)
       a[i][i]

We can print the diagonal elements using the printf() statement.

for(i=0;i<n;i++)
    printf("%5d",a[i][i]);

Trace of the matrix is the sum of diagonal elements. It can get by adding diagonal elements to the result variable sum for every iteration

sum=0;
for(i=0;i<n;i++)
     sum=sum+a[i][i];

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,i,j,sum;
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]);
printf("Diagonal elements are:\n");
sum=0;
for(i=0;i<n;i++)
{
   printf("%5d",a[i][i]);    /* printing diagonal elements */
   sum=sum+a[i][i];        /* adding diagonal elements to get trace */
}
printf("\nTrace of matrix: %d",sum);
return 0;
}

Execution:
Enter the class of square matrix:
4
Enter 4×4 matrix:
12   15    16    20
4     10   2   21
33    14   23   9
22    43   54   6
Diagonal elements are:
12   10   23   6
Trace of matrix: 51

Specification: Accept a square matrix and print the reverse diagonal elements.

Show Logic

Reverse diagonal elements

According to the above example, the reverse diagonal elements are
a[0][3] (a[0][n-1])
a[1][2] a[2][1] ……
a[3][0] (a[n-1][0])
Here the row index must be for i=0, 1, 2, …. n-1 and the column index must be for j=n-1, n-2, …. 2, 1, 0

The following loop traverses only the reverse diagonal elements.

for(i=0,j=n-1;i<n;i++,j--)
               a[i][j]

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,i,j;
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]);
printf("The reverse diagonal elements are:\n");
for(i=0,j=n-1;i<n;i++,j--)
     printf("%5d",a[i][j]);
return 0;
}

Execution:
Enter the class of square matrix:
4
Enter 4×4 matrix:
12    15    16    20
4     10     2     21
33   14     23    9
22   43    54     6
The reverse diagonal elements are:
20   2   14   22

Specification: Accept a square matrix and print lower diagonal elements.

Show Logic

Lower diagonal elements

According to the above example, the lower diagonal elements are,
a[1][0] a[2][0], a[2][1] a[3][0], a[3][1], a[3][2] By observing the above elements we come to know that, the elements whose row index is more than the column index are lower diagonal elements. The condition i>j while navigating through the matrix filters the lower diagonal elements because i is used to select  row index and j is used to select column index.

for(i=0;i<n;i++)
{
   for(j=0;j<m;j++)
     if(i>j)
       printf("%5d",a[i][j]);
   printf("\n\n");
}

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,i,j;
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]);
printf("The lower diagonal elements are:\n");
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
       if(i>j)
         printf("%5d",a[i][j]);
    printf("\n\n");
}
return 0;
}

Execution:
Enter the class of square matrix:
4
Enter 4×4 matrix:
12   15   16   20
4    10   2   21
33   14   23   9
22   43   54   6
The lower diagonal elements are:
4
33    14
22    43    54

Specification: Accept a square matrix and print the upper diagonal elements.

Show Logic

Upper diagonal elements

According to the above example, the upper diagonal elements are,
a[0][1], a[0][2], a[0][3] a[1][2], a[1][3] a[2][3] By observing the above elements we come to know that, the elements whose row index is less than the column index are upper diagonal elements. The condition i<j while navigating matrix filters the upper diagonal elements because i is used to select  row index and j is used to select column index.

for(i=0;i<n;i++)
{
  for(j=0;j<m;j++)
     if(i<j)
       printf("%5d",a[i][j]);
     else
       printf("     ");
  printf("\n\n");
}

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,i,j;
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]);
printf("The upper diagonal elements are:\n");
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
       if(i<j)
         printf("%5d",a[i][j]);
       else
          printf("     ");      /*      5 spaces        */
    printf("\n");
}
return 0;
}

Execution:
Enter the class of square matrix:
4
Enter 4×4 matrix:
12   15   16   20
4     10   2     21
33   14   23   9
22   43   54   6
The upper diagonal elements are:
15     16    20
2      21
9

Specification: Accept a nxm matrix, copy the transpose of matrix on to another matrix and print the resultant matrix.

Show Logic

Copying transpose of matrix

for(i=0;i<n;i++)
   for(j=0;j<m;j++)
      b[j][i]=a[i][j];

Here outer loop (“i”) is used to select rows of first matrix and columns of second matrix.
Inner loop (“j”) is used to copy the columns of first matrix into rows of second matrix.
When “i” is 0 then first row of first matrix is copied onto first column of second matrix (b[j][0]=a[0][j]).
When “i” is 1 then second row of first matrix is copied onto second column of second matrix (b[j][1]=a[1][j]).
When “i” is 2 then third row of first matrix is copied onto third column of second matrix (b[j][2]=a[2][j]).
When “i” is 3 then fourth row of first matrix is copied onto fourth column of second matrix (b[j][3]=a[3][j]).

Program:

#include<stdio.h>
int main()
{
int i,j,n,m,a[50][50],b[50][50];
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]);
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
       b[j][i]=a[i][j];
printf("Transpose of matrix:\n");
for(i=0;i<m;i++)
{
   for(j=0;j<n;j++)
      printf("%5d",b[i][j]);
   printf("\n");
}
return 0;
}

Execution:
Enter the class of matrix:
4   5
Enter 4×5 matrix:
5     8    3     11    7
10   9   13    19   20
6    4    3     12    18
16  25   52   34    43
Transpose of matrix:
5    10     6     16
8    9      4    25
3    13     3    52
11  19     12   34
7    20   18    43

Specification: Accept two nxm matrices and print the sum of them by storing onto another matrix.

Logic:

c[i][j]=a[i][j]+b[i][j]

Program:

#include<stdio.h>
int main()
{
int i,j,n,m,a[50][50],b[50][50],c[50][50];
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter the first matrix of %dx%d:\n",n,m);
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
     scanf("%d",&a[i][j]);
printf("Enter the second matrix of %dx%d:\n",n,m);
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
     scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
       c[i][j]=a[i][j]+b[i][j];
printf("The sum of two matrices:\n");
for(i=0;i<n;i++)
{
   for(j=0;j<m;j++)
     printf("%5d",c[i][j]);
   printf("\n");
}
return 0;
}

Execution:
Enter the class of matrix:
3   4
Enter the first matrix of  3×4:
4    5    7    8
2    1    6    1
6    9    2    6
Enter the second matrix of 3×4:
10   34    4    5
12   54    15   7
10   9    3   2
The sum of two matrices:
14    39    11    13
14    55    21    8
16    18    5     8

Previous post:

Next post: