8.11 – Matrix – Programs to practice part-1

by subbu on November 2, 2013

Specification: Accept a nxm matrix and print the matrix as it is.

Show Logic

Common loop used to store the data into a matrix by accepting from the keyboard using scanf()

for(i=0;i<n;i++)      /* Here n is the number of rows */
  for(j=0;j<m;j++)    /* Here m is the number of columns */
     scanf("%d",&a[i][j]);

Common loop used to traverse the matrix row by row

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

Every element accessed from the matrix is printed using the printf() statement.

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

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,m,i,j;
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]);
printf("The given 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   67   88
32   56   44   23
78   56   33   89
The given matrix:
12   45   67   88
32   56   44   23
78   56   33   89

Specification: Accept a nxm matrix and print the mean of matrix.

Show Logic

Mean of matrix: Average of all the elements in a matrix is called the mean.

Logic:
Common loop used to traverse the matrix row by row is

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

Every element accessed from the matrix is added to a result variable sum.

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

The mean of matrix can be calculated through dividing the sum of elements by the number of elements.

mean=(float)sum/(n*m);  /* n*m gives the number of elements in a matrix */

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,m,i,j,sum;
float mean;
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]);
sum=0;
for(i=0;i<n;i++)
    for(j=0;j<m;j++)
      sum=sum+a[i][j];
mean=(float)sum/(n*m);
printf("Mean of matrix %f",mean);
return 0;
}

Execution:
Enter the class of matrix:
3   4
Enter 3×4 matrix:
12   45   67   88
32   56   44   23
78   56   33   89
Mean of matrix: 51.916667

Specification: Accept a nxm matrix and print the sum of each row.

Show Logic

Common loop used to traverse the matrix row by row is

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

Here outer loop selects a row and the inner loop helps to access the elements of that row one by one.

To calculate the sum of each row, we initialize a result variable sum while entering into the inner loop that is when a new row is selected. Elements of selected row are added to the result variable sum as loop progresses. At the end of inner loop we print the sum of row and continue to the outer loop (next row).

for(i=0;i<n;i++)
{
  sum=0;                        /* resetting the result variable for every row */
  for(j=0;j<m;j++)
      sum=sum+a[i][j];  /* adding elements of row */
  printf("\n%d",sum);      /* printing the sum of row */
}

Program:

#include<stdio.h>
int main()
{
int a[50][50],n,m,i,j,sum;
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]);
printf("Sum of individual rows:\n");
for(i=0;i<n;i++)
{
   sum=0;              /* resetting result variable to 0 */
   for(j=0;j<m;j++)
       sum=sum+a[i][j];
   printf("\n%d",sum); /* printing sum of row */
}
return 0;
}

Execution:
Enter the class of matrix:
3   4
Enter 3×4 matrix:
12   45   67   88
32   56   44   23
78   56   33   89
Sum of individual rows:
212
155
256

Specification: Accept a nxm matrix and print its transpose

Show Logic

Transpose of matrix: Printing rows as columns and columns as rows is called transpose

Logic:

transpose of matrix

To print the 0th column, rows must be changed for j=0, 1, 2 … n-1 by keeping the column 0 constant.

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

To print the 1st column, rows must be changed for j=0, 1, 2 … n-1 by keeping the column 1 constant.

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

To print columns one after another the above loop can be controlled by an outer loop for i=0, 1, 2, … m-1.

for(i=0;i<m;i++)               /* selects column */
{
  for(j=0;j<n;j++)            /* prints the elements of selected column */
     printf("%5d",a[j][i]);
  printf("\n\n");
}

Program:

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

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

Specification: Accept a nxm matrix, store the sum of rows and columns in the same matrix and print the resultant matrix.

Logic:

Rows sum is stored in a[i][m].
Columns sum is stored in a[n][i].
Sum of rows sum and columns sum is called grand sum is stored in a[n][m]

sum of elements in a matrix

Program:

#include<stdio.h>
int main()
{
int n,m,i,j,sum,gsum,a[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]);
gsum=0;
for(i=0;i<n;i++)
{
   sum=0;
   for(j=0;j<m;j++)
       sum=sum+a[i][j];       /* calculating sum of rows */
   a[i][m]=sum;               /* storing sum of rows into a[i][m] */
   gsum=gsum+sum;             /* Adding rows sum to grand sum */
}
for(i=0;i<m;i++)
{
    sum=0;
    for(j=0;j<n;j++)
        sum=sum+a[j][i];       /* calculating sum of columns */
    a[n][i]=sum;               /* storing sum of columns into a[n][i] */
    gsum=gsum+sum;             /* Adding columns sum to grand sum */
}
a[n][m]=gsum;                       /* storing grand sum into a[n][m] */
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:
5   7   3   2
8   9   0   12
6   4   3   2
The resultant matrix:
5      7    3     2    17
8      9    0    12   29
6      4    3    2    15
19   20  6    16    122

Specification: Accept a nxm matrix and print its norm.

Logic:

Norm of matrix: Square root of sum of squares of all the elements is called the norm of matrix.

Program:

#include<stdio.h>
#include<math.h>
int main()
{
int a[50][50],n,m,i,j;
long int sum;
double norm;
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]);
sum=0;
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
      sum=sum+a[i][j]*a[i][j];       /* calculating sum of squires of elements */
norm=sqrt(sum);                      /* square root of sum of squires of elements */
printf("The norm of matrix %lf",norm);
return 0;
}

Execution:
Enter the class of matrix:
3    4
Enter 3×4 matrix:
4  5   6   3
1   2   4   5
5   3   2   1
The norm of matrix: 13.076697

Previous post:

Next post: