0% found this document useful (0 votes)
19 views2 pages

Linear Search

Uploaded by

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

Linear Search

Uploaded by

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

#include <iostream>

using namespace std;

#define FOR(a, b, c) for(int (a) = (b); (a) < (c); (a)++)

void PrintMatrix(float Matrix[3][6], int rows, int cols) {


FOR(i, 0, rows) {
FOR(j, 0, cols) {
cout << Matrix[i][j] << "\t";
}
cout << endl;
}
}

void SetDiagonal(float Matrix[3][6], int row, float Pivot) {


FOR(i, 0, 6) {
Matrix[row][i] /= Pivot;
}
}

void SetElement(float Matrix[3][6], int CurrRow, int PivotRow, float Pivot, float
curr) {
float temp = curr / Pivot;
FOR(i, 0, 6) {
if (Matrix[CurrRow][i] == curr) {
Matrix[CurrRow][i] = 0;
} else {
Matrix[CurrRow][i] -= temp * Matrix[PivotRow][i];
}
}
}

void GaussJordan(float Matrix[3][6]) {


FOR(Col, 0, 3) {
FOR(Row, 0, 3) {
if (Row == Col) {
if (Matrix[Row][Col] != 1.0) {
SetDiagonal(Matrix, Row, Matrix[Row][Row]);
}
} else {
if (Matrix[Row][Col] != 0.0) {
SetElement(Matrix, Row, Col, Matrix[Col][Col], Matrix[Row]
[Col]);
}
}
}
}
}

int main() {
cout << "Hello! This is a program to find the inverse of a matrix using the
Gauss-Jordan method in C++" << endl;
float Matrix[3][6] = {
{1, 2, -2, 1, 0, 0},
{-1, 3, 0, 0, 1, 0},
{0, -2, 1, 0, 0, 1}
};

cout << "THE ORIGINAL MATRIX IS:" << endl;


PrintMatrix(Matrix, 3, 6);

GaussJordan(Matrix);

cout << "\nTHE INVERSE MATRIX IS:" << endl;


PrintMatrix(Matrix, 3, 6);

cout << endl << "-_____________________________________-" << endl << endl;


cout << "Name: SAKSHAM PRATAP" << endl;
cout << "Enrollment No.: 20820802823" << endl;
cout << "Course & Branch: BTECH ECE" << endl;
cout << "Group: 2" << endl;

return 0;
}

Hello! This is a program to find the inverse of a matrix using the Gauss-Jordan
method in C++
THE ORIGINAL MATRIX IS:
1 2 -2 1 0 0
-1 3 0 0 1 0
0 -2 1 0 0 1

THE INVERSE MATRIX IS:


1 0 0 3 2 6
0 1 0 1 1 2
0 0 1 2 2 5

-_____________________________________-

Name: SAKSHAM PRATAP


Enrollment No.: 20820802823
Course & Branch: BTECH ECE
Group: 2

You might also like