Here all the Matrix programs in c using array are available. This program will add two matrices, subtract two matrices, and multiply two matrices.
Addition of matrices in c
In this Matrix program in c using an array, we will add 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 the addition of corresponding elements of matrices using for loop.
#include <stdio.h> int main() { int C[3][3], A[3][3], B[3][3], i, j, ch; printf("\n Matrix A\n"); printf("\n Enter the numbers you want to insert :"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("\t"); scanf("%d", &ch); A[i][j] = ch; } } printf("\n Matrix A:\n"); for (i = 0; i < 3; i++) { for (j = 0; 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 = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("\t"); scanf("%d", &ch); B[i][j] = ch; } } printf("\n Matrix B:\n"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("%d", B[i][j]); printf("\t"); } printf("\n"); } printf("\n A+B=\n"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { C[i][j] = A[i][j] + B[i][j]; printf("%d", C[i][j]); printf("\t"); } printf("\n"); } return 0; }
Program to add Two Matrix in C
Approach for C program for addition of matrices:
- Take input of both the matrices from the user.
- Using Nested for loop, we add the corresponding elements of matrices. Run an outer loop from i=0 to number to rows & inner loop will run from j=0 to the number of columns. 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 an 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> int main() { int C[3][3], A[3][3], B[3][3], i, j, ch; printf("\n Matrix A\n"); printf("\n Enter the numbers you want to insert :"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("\t"); scanf("%d", &ch); A[i][j] = ch; } } printf("\n Matrix A:\n"); for (i = 0; i < 3; i++) { for (j = 0; 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 = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("\t"); scanf("%d", &ch); B[i][j] = ch; } } printf("\n Matrix B:\n"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { printf("%d", B[i][j]); printf("\t"); } printf("\n"); } printf("\n A+B=\n"); for (i = 0; i < 3; i++) { for (j = 0; j <3; j++) { C[i][j] = A[i][j] - B[i][j]; printf("%d", C[i][j]); printf("\t"); } printf("\n"); } return 0; }Approach for C program for subtraction of matrices:
- Using Nested for loop, we insert the matrices element.
- We subtract the corresponding elements of matrices.
- Run two loops, i ->0 to n and j->0 to n.
- Use C[i][j]= A[i][j]-B[i][j]; to perform subtraction of corresponding elements.
Matrix program in c using array
Matrix: A matrix is an ordered array of numbers. We can only perform arithmetic operations like addition, subtraction, and multiplication of matrices in a square matrix. Square matrices are those which have the same number of rows and columns.
Following is the example of the square matrix: -
Matrix A
A[0][0] A[0][1] A[0][2]
A[1][0] A[1][1] A[1][2]
A[2][0] A[2][1] A[2][2]
Matrix A
A[0][0] A[0][1] A[0][2]
A[1][0] A[1][1] A[1][2]
A[2][0] A[2][1] A[2][2]
We use a nested for loop to make a Matrix program in c using an array.
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.
What is a square matrix?
Square matrices are those which have the same number of rows and columns.
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.
What is a square matrix?
Square matrices are those which have the same number of rows and columns.
Programs you must like: