0% found this document useful (0 votes)
9 views10 pages

Matrix Operations in Turbo C++

C++ Program

Uploaded by

SJV
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Matrix Operations in Turbo C++

C++ Program

Uploaded by

SJV
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Here's a simple program in Turbo C++ to perform various operaƟons on matrices, including finding

the sum, product, transpose, trace, and inverse of matrices. We'll assume the matrices are 3x3 for
simplicity.

```cpp

#include <iostream>

#include <iomanip>

using namespace std;

const int N = 3;

// FuncƟon to input a matrix

void inputMatrix(double mat[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

cin >> mat[i][j];

// FuncƟon to print a matrix

void printMatrix(double mat[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

cout << setw(10) << mat[i][j] << " ";

cout << endl;

// FuncƟon to find the sum of two matrices

void sumMatrices(double mat1[N][N], double mat2[N][N], double result[N][N]) {


for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[i][j] = mat1[i][j] + mat2[i][j];

// FuncƟon to find the product of two matrices

void productMatrices(double mat1[N][N], double mat2[N][N], double result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[i][j] = 0;

for (int k = 0; k < N; k++) {

result[i][j] += mat1[i][k] * mat2[k][j];

// FuncƟon to find the transpose of a matrix

void transposeMatrix(double mat[N][N], double result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[j][i] = mat[i][j];

// FuncƟon to find the trace of a matrix

double traceMatrix(double mat[N][N]) {

double trace = 0;
for (int i = 0; i < N; i++) {

trace += mat[i][i];

return trace;

// FuncƟon to find the inverse of a matrix

bool inverseMatrix(double mat[N][N], double result[N][N]) {

double det = mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) -

mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) +

mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);

if (det == 0) {

cout << "Matrix is singular, inverse does not exist." << endl;

return false;

double invDet = 1.0 / det;

result[0][0] = (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) * invDet;

result[0][1] = (mat[0][2] * mat[2][1] - mat[0][1] * mat[2][2]) * invDet;

result[0][2] = (mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]) * invDet;

result[1][0] = (mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2]) * invDet;

result[1][1] = (mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0]) * invDet;

result[1][2] = (mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2]) * invDet;

result[2][0] = (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]) * invDet;

result[2][1] = (mat[0][1] * mat[2][0] - mat[0][0] * mat[2][1]) * invDet;

result[2][2] = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) * invDet;

return true;

}
int main() {

double mat1[N][N], mat2[N][N];

double result[N][N];

double trace;

cout << "Enter the elements of the first 3x3 matrix (row-wise):" << endl;

inputMatrix(mat1);

cout << "Enter the elements of the second 3x3 matrix (row-wise):" << endl;

inputMatrix(mat2);

// Sum of matrices

sumMatrices(mat1, mat2, result);

cout << "Sum of matrices:" << endl;

printMatrix(result);

// Product of matrices

productMatrices(mat1, mat2, result);

cout << "Product of matrices:" << endl;

printMatrix(result);

// Transpose of the first matrix

transposeMatrix(mat1, result);

cout << "Transpose of the first matrix:" << endl;

printMatrix(result);

// Trace of the first matrix

trace = traceMatrix(mat1);

cout << "Trace of the first matrix: " << trace << endl;
// Inverse of the first matrix

if (inverseMatrix(mat1, result)) {

cout << "Inverse of the first matrix:" << endl;

printMatrix(result);

return 0;

```

### ExplanaƟon:

1. **FuncƟon DefiniƟons**:

- `inputMatrix`: Takes input for a 3x3 matrix.

- `printMatrix`: Prints a 3x3 matrix.

- `sumMatrices`: Calculates the sum of two 3x3 matrices.

- `productMatrices`: Calculates the product of two 3x3 matrices.

- `transposeMatrix`: Calculates the transpose of a 3x3 matrix.

- `traceMatrix`: Calculates the trace (sum of diagonal elements) of a 3x3 matrix.

- `inverseMatrix`: Calculates the inverse of a 3x3 matrix using the formula for the inverse of a 3x3
matrix.

2. **Main FuncƟon**:

- Prompts the user to input the elements of two 3x3 matrices.

- Calculates and prints the sum, product, transpose, and trace of the matrices.

- AƩempts to calculate and print the inverse of the first matrix, checking if it is singular.

This program covers basic matrix operaƟons for a 3x3 matrix. For larger matrices or more complex
operaƟons, consider using a library designed for linear algebra.
#include <iostream.h>

#include <iomanip.h>

#include <conio.h>

const int N = 3;

// FuncƟon to input a matrix

void inputMatrix(double mat[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

cin >> mat[i][j];

// FuncƟon to print a matrix

void printMatrix(double mat[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

cout << setw(10) << mat[i][j] << " ";

cout << endl;

// FuncƟon to find the sum of two matrices

void sumMatrices(double mat1[N][N], double mat2[N][N], double result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[i][j] = mat1[i][j] + mat2[i][j];

}
}

// FuncƟon to find the product of two matrices

void productMatrices(double mat1[N][N], double mat2[N][N], double result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[i][j] = 0;

for (int k = 0; k < N; k++) {

result[i][j] += mat1[i][k] * mat2[k][j];

// FuncƟon to find the transpose of a matrix

void transposeMatrix(double mat[N][N], double result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[j][i] = mat[i][j];

// FuncƟon to find the trace of a matrix

double traceMatrix(double mat[N][N]) {

double trace = 0;

for (int i = 0; i < N; i++) {

trace += mat[i][i];

return trace;
}

// FuncƟon to find the inverse of a matrix

bool inverseMatrix(double mat[N][N], double result[N][N]) {

double det = mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) -

mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) +

mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);

if (det == 0) {

cout << "Matrix is singular, inverse does not exist." << endl;

return false;

double invDet = 1.0 / det;

result[0][0] = (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) * invDet;

result[0][1] = (mat[0][2] * mat[2][1] - mat[0][1] * mat[2][2]) * invDet;

result[0][2] = (mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]) * invDet;

result[1][0] = (mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2]) * invDet;

result[1][1] = (mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0]) * invDet;

result[1][2] = (mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2]) * invDet;

result[2][0] = (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]) * invDet;

result[2][1] = (mat[0][1] * mat[2][0] - mat[0][0] * mat[2][1]) * invDet;

result[2][2] = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) * invDet;

return true;

int main() {

double mat1[N][N], mat2[N][N];

double result[N][N];
double trace;

cout << "Enter the elements of the first 3x3 matrix (row-wise):" << endl;

inputMatrix(mat1);

cout << "Enter the elements of the second 3x3 matrix (row-wise):" << endl;

inputMatrix(mat2);

// Sum of matrices

sumMatrices(mat1, mat2, result);

cout << "Sum of matrices:" << endl;

printMatrix(result);

// Product of matrices

productMatrices(mat1, mat2, result);

cout << "Product of matrices:" << endl;

printMatrix(result);

// Transpose of the first matrix

transposeMatrix(mat1, result);

cout << "Transpose of the first matrix:" << endl;

printMatrix(result);

// Trace of the first matrix

trace = traceMatrix(mat1);

cout << "Trace of the first matrix: " << trace << endl;

// Inverse of the first matrix

if (inverseMatrix(mat1, result)) {

cout << "Inverse of the first matrix:" << endl;

printMatrix(result);
}

getch(); // To keep the console window open

return 0;

You might also like