## Matrix program in c using array

Here all the Matrix programs in c using array are available. In this program, we will add two matrices, subtract two matrices and multiply two matrices in c using an array.## Addition of matrices in c

```
#include<stdio.h>
#include<conio.h>
int main()
{
int C[3][3],A[3][3],B[3][3],i,j,ch;
clrscr();
printf(“\n Matrix A\n”);
printf(“\n Enter the numbers you want to insert in the matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
A[i][j]=ch;
}
}
printf(“\n Matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,A[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n\n Matrix B”);
printf(“\n Enter the numbers you want to insert in the matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
B[i][j]=ch;
}
}
printf(“\n Matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,B[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n A+B=\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
C[i][j]= A[i][j]+B[i][j];
printf(“%d”,C[i][j]);
printf(“\t”);
}
printf(“\n”);
}
return 0;}
```

** Approach to make C program for addition of matrices:**

1. Using Nested for loop, we initialize array elements in two matrices A & B.

2. Using Nested for loop, we print all the numbers entered by the user on the screen.

3. Using Nested for loop, we add the corresponding elements of matrices. For this we use the following statement:

C[i][j]= A[i][j]+B[i][j];

## Subtraction of two matrices in c

In this matrix program in c using array, we will subtract two matrices. First, we insert numbers in the matrix entered by the user in Matrix A and Matrix B using for loop. Now we take another matrix that contains subtraction of corresponding elements of matrices using for loop.

```
#include<stdio.h>
#include<conio.h>
int main()
{
int C[3][3],A[3][3],B[3][3],i,j,ch;
clrscr();
printf(“\n Matrix A\n”);
printf(“\n Enter the numbers”);
printf(“\n you want to insert”);
printf(“ in the matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
A[i][j]=ch;
}
}
printf(“\n Matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,A[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n\n Matrix B”);
printf(“\n Enter the numbers”);
printf(“\n you want to insert”);
printf(“ in the matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
B[i][j]=ch;
}
}
printf(“\n Matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,B[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n A-B=\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
C[i][j]= A[i][j]-B[i][j];
printf(“%d”,C[i][j]);
printf(“\t”);
}
printf(“\n”);
}
return 0;
}
```

**Approach to make C program for subtraction of matrices:**

1. Using Nested for loop, we insert the matrices element.

2. Using Nested for loop, we print all the numbers entered by the user on the screen.

3. Using Nested for loop, we subtract the corresponding elements of matrices. For this we use the following statement:

C[i][j]= A[i][j]-B[i][j];

## Matrix program in c using array

Following are the examples of the square matrix: -

Matrix A

A[1][1] A[1][2] A[1][3]

A[2][1] A[2][2] A[2][3]

A[3][1] A[3][2] A[3][3]

Matrix B

B[1][1] B[1][2] B[1][3]

B[2][1] B[2][2] B[2][3]

B[3][1] B[3][2] B[3][3]

Matrix C

C[1][1] C[1][2] C[1][3]

C[2][1] C[2][2] C[2][3]

C[3][1] C[3][2] C[3][3]

To make a Matrix program in c using an array, we use nested for loop.

Questions and Answers

**How do you declare a 2d array?**

int A[r][c];

where r is the number of rows

and c is the number of columns

**What is a two-dimensional array?**

A two-dimensional array is a matrix with several rows and columns.

for example:

A[1][1] A[1][2] A[1][3]

A[2][1] A[2][2] A[2][3]

A[3][1] A[3][2] A[3][3]

**What is a square matrix?**

Square matrices are those which have the same number of rows and columns. Following are the examples of the square matrix:-

Matrix A

A[1][1] A[1][2] A[1][3]

A[2][1] A[2][2] A[2][3]

A[3][1] A[3][2] A[3][3]

These are all the Matrix program in c using an array. Thanks for reading this program if you have any query you can comment below and also you can contact us.

Programs you must like:

1. Menu driven program in c

Nice one

ReplyDeletePlease provide the source code of array operations like addition of elements in an array, deletion and traversal.

ReplyDelete