Matrix Formation – C Program

A Matrix (plural Matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.

For example, the dimensions of the matrix below are 2 × 3 (read “two by three”), because there are two rows and three columns.

{\displaystyle {\begin{bmatrix}1&9&-13\\20&5&-6\end{bmatrix}}.}

So, today we will create C Program of Matrix in which data will be taken from the user dynamically. I used to write C Programs in Code Blocks.

Also, we will perform Addition, Subtraction and Transpose of both the Matrix. The Program which I had created is simple to understand for beginners because i have used only following keywords in the code:

  1. Arrays (to store input data from user)
  2. Nested For Loop
  3. If Else Conditional Operators (to compare both the  matrix)

Below image is the output the program:

matrix

Matrix – C Program

Try to analyze the above given data and create the program from your own.

Failed? Click More below to get the Solution…

Here is the solution. C Program Code file is attached at the end of this article.

 

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

void main()
{

 int x1,y1,i,j;

 clrscr();
 printf(“Enter the order for 1st matrix: \ni= “);
 scanf(“%d”,&x1);
 printf(“j= “);
 scanf(“%d”,&y1);

 int mat1[x1][y1];
 printf(“Enter data:\n”);

 for (i=0; i<x1; i++)
 {
  for (j=0; j<y1; j++)
  {
   scanf(“%d”,&mat1[i][j]);
  }
 }
 printf(“\n”);
 printf(“1st Matrix %d x %d is:\n”,x1,y1);
 printf(“\n”);

 for (i=0; i<x1; i++)
 {
  for (j=0; j<y1; j++)
  {
   printf(“\t%d”,mat1[i][j]);
  }
 printf(“\n”);
 }

 int x2,y2;
 printf(“Enter the order for 2nd matrix: \ni= “);
 scanf(“%d”,&x2);
 printf(“j= “);
 scanf(“%d”,&y2);

 int mat2[x2][y2];
 printf(“Enter data:\n”);

 for (i=0; i<x2; i++)
 {
  for (j=0; j<y2; j++)
  {
   scanf(“%d”,&mat2[i][j]);
  }
 }
 printf(“\n”);
 printf(“2nd Matrix %d x %d is:\n”,x2,y2);
 printf(“\n”);

 for (i=0; i<x2; i++)
 {
  for (j=0; j<y2; j++)
  {
   printf(“\t%d”,mat2[i][j]);
  }
 printf(“\n”);
 }

 printf(“\n”);
 if (x1==x2 && y1==y2)
 {
  printf(“Addition of Matrix 1 and 2”);
  printf(“\n”);
  for (i=0; i<x1; i++)
  {
   for (j=0; j<y1; j++)
   {
    printf(“\t%d”,mat1[i][j]+mat2[i][j]);
   }
  printf(“\n”);
  }

  printf(“\n”);
  printf(“Subtraction of Matrix 1 and 2”);
  printf(“\n”);
  for (i=0; i<x1; i++)
  {
   for (j=0; j<y1; j++)
   {
    printf(“\t%d”,mat1[i][j]-mat2[i][j]);
   }
  printf(“\n”);
  }
 }

 else
 {
  printf(“Matrix 1 and 2 are unequal, therefore addition and subtraction cannot be performed!”);
 }

 printf(“\n”);
 printf(“\n”);
 printf(“Transpose of the 1st matrix %d x %d is:\n”,x1,y1);
 printf(“\n”);

 for (i=0; i<y1; i++)
 {
  for (j=0; j<x1; j++)
  {
   printf(“\t%d”,mat1[j][i]);
  }
 printf(“\n”);
 }
 printf(“\n”);
 printf(“Transpose of the 2nd matrix %d x %d is:\n”,x2,y2);
 printf(“\n”);

 for (i=0; i<y2; i++)
 {
  for (j=0; j<x2; j++)
  {
   printf(“\t%d”,mat2[j][i]);
  }
 printf(“\n”);
 }
}

If you have any queries, more simplified programs or solutions then kindly comment below. Also you can Email me your doubts and queries related to program.

Thank You!

Attachment: Matrix_Formation.c 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s