# C Program to Manipulate Matrices

Matrix is a mathematical concept that we used to learn in schools and collages. To learn about matrices, you have to learn linear algebra basics.

Given a set of linear equations, you can derive a matrix and then perform simple operations on them. Linear algebra is beyond the scope of this post, we only discuss about the matrix manipulation program in C.

The C program reads two square matrices and perform addition, subtraction, multiplication and transpose operation on them.

This program is intended for intermediate level learner of C programming and written using Dev-C++ version 4.9.9.2 compiler installed on a Windows 7 64-bit machine.

To understand this program, some familiarity with matrix concepts is necessary as well as recommended.

### Problem Definition

Suppose you are given two matrices, then you can perform following simple operations on them.

2. Subtraction
3. Multiplication
4. Transpose

In this program, you are going to use only square matrices for all operations.

For example, if A and B are two matrices then,

A         $= \begin{bmatrix} 1 & 2 & 3 \\ 1 & 1 & 4 \\ 2 & 3 & 5 \end{bmatrix}$

and

B         $=\begin{bmatrix} 3 & 1 & 3 \\ 2 & 1 & 1 \\ 3 & 6 & 3 \end{bmatrix}$

A + B   $= \begin{bmatrix} 4 & 3 & 6 \\ 3 & 2 & 5 \\ 5 & 9 & 8 \end{bmatrix}$

A – B $=\begin{bmatrix}-2 & 1 & 0 \\ -1 & 0 & 3 \\ -1 & -3 & 2 \end{bmatrix}$

A * B $=\begin{bmatrix} 16 & 21 & 14 \\ 17 & 26 & 16 \\ 27 & 35 & 24 \end{bmatrix}$

AT $= \begin{bmatrix} 1 & 1 & 2 \\ 2 & 1 & 3 \\ 3 & 4 & 5 \end{bmatrix}$

In multiplication of two matrices, the row in A multiply all elements in column in B and the result is added. You do this for all rows and columns.In transpose of matrix, the row becomes the column and the column becomes the row, in the resultant matrix.The addition and subtraction of matrices are straight-forward operations.

### Program Code

/* PROGRAM IN C TO MANIPULATE MATRIX */
#include <stdio.h>
#include <stdlib.h>
#define N 10
main() {
int a[10][10],b[10][10];
int c[10][10];
int i,j,k,n,choice;
void menu(int a[N][N],int b[N][N],int c[N][N],int n);
/* Get the Size of the Matrices */
printf("ENTER SIZE OF MATRIX:");
scanf("%d",&n);
i=0;
while(choice !=5) {
i++;
}
system("PAUSE");
return 0;
}
void menu(int a[N][N],int b[N][N],int c[N][N],int n) {
int i,j,k,choice;
printf("ENTER A CHOICE:\n");
for(i=0;i<30;i++)
printf("_");printf("\n\n");
printf("2.SUBTRACTION\n");
printf("3.MULTIPLICATION\n");
printf("4.TRANSPOSE\n");
printf("5.QUIT\n");
for(i=0;i<30;i++)
printf("_");printf("\n\n");
scanf("%d",&choice);
i=0;
switch(choice) {
case 1: for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
/* Print Result */
system("cls");
printf("RESULT\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
break;
case 2:
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
/* Print Result */
system("cls");
printf("RESULT\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");printf("\n\n");
break;
case 3:
for(i=0;i<n;i++) {
for( j=0;j<n;j++) {
for( k=0;k<n;k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
/* Print the result */
system("cls");
printf("Result\n");
for( i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");
printf("\n\n");
break;
/* Transpose Matrix -A */
case 4:
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[j][i];
}
}
/* Print Result */
system("cls");
printf("*RESULT*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");
printf("\n\n");
break;
default: exit(0);
}
}
/* Read Matrix a and b*/
void read_matrix(int a[N][N],int b[N][N],int n) {
int i,j;
printf("ENTER MATRIX VALUES ROWWISE:\n\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
} }
/* Print Matrix A */
printf("*Matrix-A*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
printf("ENTER MATRIX VALUES ROWWISE:\n\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&b[i][j]);
}
}
/*Print Matrix-B */
printf("*Matrix-B*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%dt",b[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
}