8.13 – Matrix – programs to practice part-3

by subbu on November 3, 2013

Specification: Accept a nxm matrix, sort every row in ascending order and print the resultant matrix.

Show Logic

The loop to sort the elements of an array using bubble sort is

for(j=n-2;j>=0;j--)
  for(i=0;i<=j;i++)
   if(a[i+1]<a[i])
   {
     temp=a[i];
     a[i]=a[i+1];
     a[i+1]=temp;
   }

To sort the elements of 0th row in a matrix using bubble sort is

for(j=m-2;j>=0;j--)   /* here m is the number of columns */
   for(i=0;i<=j;i++)
      if(a[0][i+1]<a[0][i])
      {
        temp=a[0][i];
        a[0][i]=a[0][i+1];
        a[0][i+1]=temp;
      }

If we want to sort every row in a matrix then rows can be selected using another loop

for(k=0;k<n;k++)  /* here n is the number of rows */
{
    for(j=m-2;j>=0;j--)  /* here m is the number of columns */
      for(i=0;i<=j;i++)
        if(a[k][i+1]<a[k][i])
        {
           temp=a[k][i];
           a[k][i]=a[k][i+1];
           a[k][i+1]=temp;
        }
}

Program: 

#include<stdio.h>
int main()
{
int a[50][50],n,m,i,j,k,temp;
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(k=0;k<n;k++)    /* here n is the number of rows */
{
   for(j=m-2;j>=0;j--)   /* here m is the number of columns */
      for(i=0;i<=j;i++)
        if(a[k][i+1]<a[k][i])
        {
          temp=a[k][i];
          a[k][i]=a[k][i+1];
          a[k][i+1]=temp;
        }
}
printf("The resultant matrix:\n");
for(i=0;i<n;i++)
{
   for(j=0;j<m;j++)
      printf("%5d",a[i][j]);
   printf("\n");
}
return 0;
}

Execution:
Enter the class of matrix:
3   4
Enter 3×4 matrix:
12   45   17   88
32   56   44   23
78   56   33   89
The resultant matrix:
12   17   45   88
23   32   44   56
33   56   78   89

Specification: Accept two matrices and print the product of them (matrix multiplication).

Show Logic

Product of two matrices

Let us assume that the class of 1st matrix is nxm, 2nd matrix is pxq
To multiply two matrices, columns of the first matrix must be equal to the rows of second matrix and class of resultant matrix would be nxq

m==p

Variable “i” is used to select rows of first matrix for i=0, 1, 2, 3, … n-1
Variable “j” is used to select columns of second matrix for j=0, 1, 2, 3, …q-1

for(i=0;i<n;i++)  /* selecting rows of first matrix */
  for(j=0;j<q;j++)  /* selecting columns of second matrix */
      a[i][?]*b[?][j]

Variable “k” is used to multiply and add the elements of selected row of first matrix and column of second matrix for k=0, 1, 2, …. m-1 or p-1

for(i=0;i<n;i++)      /* selecting rows of first matrix */
  for(j=0;j<q;j++)      /* selecting columns of second matrix */
     for(sum=0,k=0;k<m;k++)
          sum=sum+a[i][k]*b[k][j]

We will store the sum of products of elements of selected row and column into other matrix at “i” row and “j” column.

for(i=0;i<n;i++)       /* selecting rows of first matrix */
  for(j=0;j<q;j++)       /* selecting columns of second matrix */
    {
       for(sum=0,k=0;k<m;k++)
          sum=sum+a[i][k]*b[k][j]
       c[i][j]=sum;
    }

Program:

#include<stdio.h>
int main()
{
int i,j,k,sum,n,m,p,q,a[50][50],b[50][50],c[50][50];
printf("Enter the class of first matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter the class of second matrix:\n");
scanf("%d%d",&p,&q);
if(m!=p)
   printf("Cannot multiply");
else
{
    /* reading data into first matrix */
    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]);
    /* reading data into second matrix */
    printf("Enter %dx%d matrix:\n",p,q);
    for(i=0;i<p;i++)
      for(j=0;j<q;j++)
         scanf("%d",&b[i][j]);
    /* calculating product of two matrices */
    for(i=0;i<n;i++)
       for(j=0;j<q;j++)
       {
          for(sum=0,k=0;k<m;k++)
             sum=sum+a[i][k]*b[k][j];
          c[i][j]=sum;
       }
    printf("Product of two matrices:\n");
    for(i=0;i<n;i++)
    {
       for(j=0;j<q;j++)
          printf("%5d",c[i][j]);
       printf("\n");
    }
}/* end of else*/
return 0;
}

Execution1:
Enter the class of first matrix:
3     4
Enter the class of second matrix:
5    3
Cannot multiply

Execution2:
Enter the class of first matrix:
3    4
Enter the class of second matrix:
4    4
Enter   3×4 matrix:
1    2   3   5
2    1   3   4
1    4   3   2
Enter 4×4 matrix:
3   4    5    6
1   2    3    4
2   1    3    1
2   3    5    3
Product of two matrices:
23   27   48   33
21   25   42   31
17   21   36   31

Specification: Accept a nxm matrix and print its mean, variance and standard deviation

Show Logic

Mean: It is the average of all the elements in an array

sum=0;
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
     sum=sum+a[i][j];
mean=(float)sum/(n*m);

Variance: For each element subtract the element from mean and square, Average of these squares is called variance (The average of the squared differences from the Mean)

sum_sq=0;
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
     sum_sq=sum_sq+pow(mean-a[i][j],2); /* sum of squired differences */
var=sum_sq/(n*m);     /* average of squired differences */

Standard deviation: The square root of variance is called standard deviation

stddev=sqrt(var);

Program:

#include<stdio.h>
#include<math.h>
int main()
{
int a[50][50],n,m,i,j,sum;
float mean,var,stddev,sum_sq;
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]);
/* calculating mean of matrix */
sum=0;
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
      sum=sum+a[i][j];
mean=(float)sum/(n*m);
/* calculating variance of matrix */
sum_sq=0;
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
      sum_sq=sum_sq+pow(mean-a[i][j],2); /* sum of squired differences */
var=sum_sq/(n*m);                    /* average of squired differences */
/* calculating standard deviation */
stddev=sqrt(var);
printf("Mean of matrix %f",mean);
printf("\nVariance of matrix %f",var);
printf("\nStandard deviation of matrix %f",stddev);
return 0;
}

Execution:
Enter the class of matrix:
2       3
Enter 2×3 matrix:
12      10      11
21      23      14
Mean of matrix 15.166667
Variance of matrix 25.138887
Standard deviation of matrix 5.013869

Specification: Accept a nxm matrix and print the minimum and maximum elements of matrix

Show Logic

We assume that the first element of matrix is minimum and maximum

min=max=a[0][0];

We access elements row by row using the loop

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

if the new element is less than the min (a[i][j]<min) than the new element is assigned to min

if(a[i][j]<min)
    min=a[i][j];

if the new element is greater than the max (a[i][j]>max) than the new element is assigned to max

if(a[i][j]>max)
    max=a[i][j];

Putting altogether:

min=max=a[0][0];
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
  {
     if(a[i][j]<min)
       min=a[i][j];
     if(a[i][j]>max)
       max=a[i][j];
  }

Program:

#include<stdio.h>
#include<math.h>
int main()
{
int a[50][50],n,m,i,j,min,max;
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]);
min=max=a[0][0];
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
  {
    if(a[i][j]<min)
       min=a[i][j];
    if(a[i][j]>max)
       max=a[i][j];
  }
printf("Minimum element %d",min);
printf("\nMaximum element %d",max);
return 0;
}

Execution:
Enter the class of matrix:
2 3
Enter 2×3 matrix:
25  65  34
33  76  44
Minimum element 25
Maximum element 76

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

Show Logic

Symmetric matrix: It is a square matrix that is equal to its transpose

The loop used to traverse a square matrix row by row is

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

The loop used to traverse a square matrix column by column (transpose)

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

We compare both the elements of matrix, elements of its transpose using a common loop. flag is assigned with zero if any inequality found

flag=1;
for(i=0;i<n;i++)
   for(j=0;j<n;j++)
     if(a[i][j]!=a[j][i])
     {
       flag=0;
       break;
     }

If flag is unchanged (1) then the matrix is symmetric otherwise not a symmetric matrix.

if(flag)
    printf("Symmetric matrix");
else
    printf("Not a symmetric 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(a[i][j]!=a[j][i])
    {
       flag=0;
       break;
    }
if(flag)
   printf("Symmetric matrix");
else
   printf("Not a symmetric matrix");
return 0;
}

Execution:
Enter the class of square matrix:
3
Enter 3×3 matrix:
1  7  3
7  4  -5
3  -5  6
Symmetric matrix

Execution:
Enter the class of square matrix:
3
Enter 3×3 matrix:
2 4 5
6 7 8
2 3 5
Not a symmetric matrix

Previous post:

Next post: