You are on page 1of 23

Addition of two matrix

#include <iostream.h>
#include<conio.h>
void main()

int A[10][10], B[10][10], C[10][10];

int i, j, c, r;

cout << "enter the number of rows of matrix A :" ;

cin >> r ;

cout << "enter the number of columns of matrix B :" ;

cin >> c;

cout << "enter the elements of matrix A " << endl ;

for (i=0; i<r ; i++)

for (j=0; j<c; j++)

cout << "enter elements a " << i+1 << j+1 << ":" ;

cin >> A[i][j];

cout << "the entered matrix A is :" ;

for (i=0; i<r; i++)

{
cout << "\n ";

for (j=0; j<c; j++)

cout << A[i][j] << "\t " ;

cout << endl << "enter the rows of matrix B:" ;


cin >> r;

cout << endl << "enter the columns of matrix B :" ;

cin >> c;

cout << "enter the elements of matrix B : " << endl ;

for (i=0; i<r; i++)

for (j=0; j<c; j++)

{
cout << "enter the elements b " << i+1 << j+1 << ":" ;

cin >> B[i][j];

cout << endl << "the entered matrix B is : " ;

for (i=0; i<r; i++)

{
cout << "\n " ;

for (j=0; j<c; j++)

cout << B[i][j] << "\t " ;


}

cout << endl << "the resultant matrix is C : is " ;

for (i=0; i<r; i++)

{
for (j=0; j<c; j++)
C[i][j]=A[i][j]+B[i][j];
}
for (i=0; i<r; i++)
{
cout << "\n " ;
for (j=0; j<c; j++)

cout << C[i][j] << "\t " ;


}
getch();
}
Output:
enter the number of rows of matrix A :3
enter the number of columns of matrix A :3
enter the elements of matrix A
enter elements a 13:12
enter elements a 21:13
enter elements a 22:14
enter elements a 23:15
enter elements a 31:16
enter elements a 32:17
enter elements a 33:18
the entered matrix A is :
10 11 12
13 14 15
16 17 18
enter the rows of matrix B:3
enter the columns of matrix B :3
enter the elements of matrix B :
enter the elements b 11:19
enter the elements b 12:20
enter the elements b 13:21
enter the elements b 21:22
enter the elements b 22:23
enter the elements b 23:24
enter the elements b 31:25
enter the elements b 32:26
enter the elements b 33:27
the entered matrix B is :
19 20 21
22 23 24
25 26 27
the resultant matrix is C : is
29 31 33
35 37 39
41 43 45
Process returned 0 (0x0) execution time : 31.260 s
Press any key to continue.
Subtraction of two matrix
#include <iostream.h>
#include<conio.h>
void main()

{
int A[10][10], B[10][10], C[10][10];
int i, j, c, r;
cout << "enter the number of rows of matrix A :" ;
cin >> r ;

cout << "enter the number of columns of matrix B :" ;

cin >> c;

cout << "enter the elements of matrix A " << endl ;

for (i=0; i<r ; i++)

for (j=0; j<c; j++)

{
cout << "enter elements a " << i+1 << j+1 << ":" ;

cin >> A[i][j];

cout << "the entered matrix A is :" ;

for (i=0; i<r; i++)

{
cout << "\n ";

for (j=0; j<c; j++)

cout << A[i][j] << "\t " ;

cout << endl << "enter the rows of matrix B:" ;

cin >> r;

cout << endl << "enter the columns of matrix B :" ;


cin >> c;

cout << "enter the elements of matrix B : " << endl ;

for (i=0; i<r; i++)

for (j=0; j<c; j++)

{
cout << "enter the elements b " << i+1 << j+1 << ":" ;
cin >> B[i][j];

cout << endl << "the entered matrix B is : " ;

for (i=0; i<r; i++)

{
cout << "\n " ;

for (j=0; j<c; j++)

cout << B[i][j] << "\t " ;


}

cout << endl << "the resultant matrix is C : is " ;

for (i=0; i<r; i++)

{
for (j=0; j<c; j++)

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

for (i=0; i<r; i++)


{
cout << "\n " ;

for (j=0; j<c; j++)

cout << C[i][j] << "\t " ;

}
getch();
}
Output
enter the number of rows of matrix A :3
enter the number of columns of matrix A :3
enter the elements of matrix A
enter elements a 11:78
enter elements a 12:98
enter elements a 13:56
enter elements a 21:43
enter elements a 22:23
enter elements a 23:85
enter elements a 31:12
enter elements a 32:32
enter elements a 33:67
the entered matrix A is :
78 98 56
43 23 85
12 32 67
enter the rows of matrix B:3
enter the columns of matrix B :3
enter the elements of matrix B :
enter the elements b 11:11
enter the elements b 12:34
enter the elements b 13:23
enter the elements b 21:35
enter the elements b 22:9
enter the elements b 23:51
enter the elements b 31:3
enter the elements b 32:7
enter the elements b 33:37

the entered matrix B is :


11 34 23
35 9 51
3 7 37
the resultant matrix is C : is
67 64 33
8 14 34
9 25 30
Process returned 0 (0x0) execution time : 99.753 s
Press any key to continue.
Multiplication of two matrix
#include<iostream.h>
#include<coino.h>
void main()

{
int A[10][10],B[10][10], C[10][10];
int i,j, k;

cout << "enter the rows and columns of matrix A" << endl ;

for (i=0; i<2; i++)

for (j=0; j<2; j++)

cout << "enter the element a " << i+1 << j+1 << ":";

cin >> A[i][j];

cout << "enter the rows and columns of matrix B" << endl ;

for (i=0; i<2; i++)

for (j=0; j<2; j++)

cout << "enter the element b " << i+1 << j+1 << ":";

cin >> B[i][j];


}

C[i][j]=0;
cout << "the resultant matrix is " << endl;

for (i=0; i<2; i++)


{

for (j=0; j<2; j++)

{
C[i][j]=0;

for (k=0; k<2; k++)

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

cout << C[i][j] << "\t " ;

getch();
}

Output
enter the rows and columns of matrix A
enter the element a 11:1
enter the element a 12:1
enter the element a 21:1
enter the element a 22:1
enter the rows and columns of matrix B
enter the element b 11:2
enter the element b 12:2
enter the element b 21:2
enter the element b 22:2
the resultant matrix is
4 4 4 4
Process returned 0 (0x0) execution time : 4.913 s
Press any key to continue.
Program to print coordinate of simple cube
#include<iostream.h>
#include<conio.h>
void main()
{
int i, j, k, n;
cout << "enter the number of atoms lying on any axis " ;

cin >> n;

for (i=0; i<=n; i++)

for (j=0; j<=n; j++)

for (k=0; k<=n; k++)

cout << i << " " << j << " " << k << endl ;
}
getch();
}

Output-1
enter the number of atoms lying on any axis 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Process returned 0 (0x0) execution time : 1.163 s
Press any key to continue.
Output-2
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2

Process returned 0 (0x0) execution time : 1.600 s


Press any key to continue.
Program to print coordinate of simple cube (33)
#include<iostream.h>
#include<conio.h>
void main()
{

int i, j, k, n;

cout << "enter the number of atoms lying on any axis " ;

cin >> n;

for (i=0; i<=n; i++)

for (j=0; j<=n; j++)

for (k=0; k<=n; k++)

cout << i << " " << j << " " << k << endl ;

if (i>0 && j>0 && k>0)

cout << (i+i-1)*0.5 << " " << (j+j-1)*0.5 << " " << (k+k-1)*0.5 <<
endl;
}

getch();

}
Output-1
enter the number of atoms lying on any axis 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0.5 0.5 0.5

Process returned 0 (0x0) execution time : 1.050 s


Press any key to continue.
Output-2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
0.5 0.5 0.5
1 1 2
0.5 0.5 1.5
1 2 0
1 2 1
0.5 1.5 0.5
1 2 2
0.5 1.5 1.5
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
1.5 0.5 0.5
2 1 2
1.5 0.5 1.5
2 2 0
2 2 1
1.5 1.5 0.5
2 2 2
1.5 1.5 1.5

Process returned 0 (0x0) execution time : 0.963 s


Press any key to continue.
Program to print coordinate of BCC Structure (22)

#include<iostream.>
#include<conio.h>
void main()

int i, j, n;

cout << "enter the number of atoms on any axis " ;

cin >> n;

for (i=0; i<=n; i++)

for (j=0; j<=n; j++)

cout << i << " " << j << endl;

if (i>0 && j>0 )

cout << (i+i-1 )*0.5 << " " << (j+j-1)*0.5 << endl;

getch();

}
Output
enter the number of atoms on any axis 2
0 0
0 1
0 2
1 0
1 1
0.5 0.5
1 2
0.5 1.5
2 0
2 1
1.5 0.5
2 2
1.5 1.5

Process returned 0 (0x0) execution time : 1.363 s


Press any key to continue.
Program to print coordinate of FCC Structure (33)
#include<iostream.h>

#include<conio.h>;

void main()

int i, j, k, n;

cout << "enter the number of atoms lying on any axis " ;

cin >> n;

for (i=0; i<=n; i++)

for (j=0; j<=n; j++)

for (k=0; k<=n; k++)

cout << i << " " << j << " " << k << endl ;

if (i==0 && j>0 && k>0)

cout << i*0.5 << " " << (j+j-1)*0.5 << " " << (k+k-1)*0.5 <<
endl;

if (i>0 && j==0 && k>0)

cout << (i+i-1)*0.5 << " " << j*0.5 << " " << (k+k-1)*0.5 <<
endl;

if (i>0 && j>0 && k==0)

cout << (i+i-1)*0.5 << " " << (j+j-1)*0.5 << " " << k*0.5 << endl;
if (i>0 && j>0 && k>0)

cout << i << " " << (j+j-1)*0.5 << " " << (k+k-1)*0.5 << endl;

cout << (i+i-1)*0.5 << " " << j << " " << (k+k-1)*0.5 << endl;

cout << (i+i-1)*0.5 << " " << (j+j-1)*0.5 << " " << k << endl;

getch();

}
Output
enter the number of atoms lying on any axis 2
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 0.5 0.5
0 1 2
0 0.5 1.5
0 2 0
0 2 1
0 1.5 0.5
0 2 2
0 1.5 1.5
1 0 0
1 0 1
0.5 0 0.5
1 0 2
0.5 0 1.5
1 1 0
0.5 0.5 0
1 1 1
1 1 2
1 2 0
0.5 1.5 0
1 2 1
1 2 2
2 0 0
2 0 1
1.5 0 0.5
2 0 2
1.5 0 1.5
2 1 0
1.5 0.5 0
2 1 1
2 1 2
2 2 0
1.5 1.5 0
2 2 1
2 2 2

Process returned 0 (0x0) execution time : 1.651 s


Program to find the inverse of Matrix (33)
#include<iostream.h>
#include<conio.h>
void main()

int A[10][10], i, j, r, c;

float determinant = 0;

cout << "enter the row of matrix A " ;

cin >> r;

cout << "enter the columns of matrix A" ;

cin >> c;
cout<<"Enter elements of matrix A:\n";

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

cout << "enter the elements a" << i+1 << j+1 << ":" ;
cin>>A[i][j];

}
cout << "\nGiven matrix is:";

for(i = 0; i < 3; i++){

cout<<"\n";
for(j = 0; j < 3; j++)

cout<<A[i][j]<<"\t";

for(i = 0; i < 3; i++)

determinant = determinant + (A[0][i] * (A[1][(i+1)%3] *


A[2][(i+2)%3] - A[1][(i+2)%3] * A[2][(i+1)%3]));
cout<<"\n\n determinant: "<<determinant;
cout<<"\n\n Inverse of matrix is: \n";

for(i = 0; i < 3; i++){

for(j = 0; j < 3; j++)

cout<<((A[(j+1)%3][(i+1)%3] * A[(j+2)%3][(i+2)%3]) -
(A[(j+1)%3][(i+2)%3] * A[(j+2)%3][(i+1)%3]))/ determinant<<"\t";

cout<<"\n";

}
getch();

}
Output:
enter the row of matrix A: 3
enter the columns of matrix A:4
Enter elements of matrix A:
enter the elements a11:1
enter the elements a12:2
enter the elements a13:3
enter the elements a21:4
enter the elements a22:5
enter the elements a23:6
enter the elements a31:7
enter the elements a32:8
enter the elements a33:0

Given matrix is:


1 2 3
4 5 6
7 8 0

determinant: 27

Inverse of matrix is:


-1.77778 0.888889 -0.111111
1.55556 -0.777778 0.222222
-0.111111 0.222222 -0.111111

Process returned 0 (0x0) execution time : 10.213 s


Press any key to continue.
Program to find the transpose of Matrix (33)
#include<iostream.h>
#include<conio.h>
void main()

int A[10][10], trans[10][10], r, c, i, j;


cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
cout << endl << "Enter elements of matrix: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << "Enter elements a" << i + 1 << j + 1 << ": ";

cin >> A[i][j];

}
cout << endl << "Entered Matrix: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << " " << A[i][j];

if(j == c - 1)

cout << endl << endl;


}

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

trans[j][i]=a[i][j];

}
cout << endl << "Transpose of Matrix: " << endl;

for(i = 0; i < c; ++i)

for(j = 0; j < r; ++j)

cout << " " << trans[i][j];

if(j == r - 1)

cout << endl << endl;

}
getch();

}
Output
Enter rows and columns of matrix: 3
3
Enter elements of matrix:
Enter elements a11: 1
Enter elements a12: 2
Enter elements a13: 3
Enter elements a21: 4
Enter elements a22: 5
Enter elements a23: 6
Enter elements a31: 7
Enter elements a32: 8
Enter elements a33: 9
Entered Matrix:
123

456

789
Transpose of Matrix:
147

258

369

Process returned 0 (0x0) execution time : 6.566 s


Press any key to continue.

You might also like