The goal of this lab is to develop a C++ program to implement Gauss-Jordan Elimination for solving systems of linear equations. Students will read an augmented matrix from a file, process it using Gauss-Jordan Elimination, and transform it into a Reduced Row Echelon Form (RREF) to obtain the solution. This process will provide hands-on experience in reading a matrix from a data file, matrix manipulation, numerical methods, and algorithmic problem-solving. Through this lab, students will:
A system of linear equations consists of multiple equations that need to be solved simultaneously. One efficient method for solving such systems is Gaussian Elimination, which transforms the system into an upper triangular form and then applies back-substitution to find the solution. In this lab, you will implement Gauss-Jordan Elimination in C++ by converting a given Python implementation into an equivalent C++ program. Gauss-Jordan Elimination is an advanced form of Gaussian Elimination that converts the system into Reduced Row Echelon Form (RREF), allowing for a direct solution without the need for back-substitution. The goal is to apply row operations on an augmented matrix representation of a linear system to systematically solve for the unknown variables. This lab will also require you to read the augmented matrix from a data file for further processing.
//Global Variables
const int ROWS = 3; // Number of equations (rows)
const int COLS = 4; // Number of columns (3 variables + 1 augmented column)
// Function to read a 3x4 augmented matrix from a file
bool readMatrixFromFile(const string &filename, double matrix[][COLS]) {
ifstream file; //create a file object
file.open(filename); //open the file
if (!file) {
cerr << "Error: Unable to open file!" << endl;
return false;
}
// Read matrix from file and store it in a 2D array
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
file >> matrix[i][j]; // Read each value into the matrix
}
}
file.close(); //Closes the file after reading its contents.
return true;
}
A system of linear equations can be written in augmented matrix form:
\[ \begin{bmatrix} 2 & -3 & 1 & \vert & -22 \\ 7 & 9 & -3 & \vert & 14 \\ 6 & 7 & 2 & \vert & 91 \end{bmatrix} \]Here, the left part of the matrix represents the coefficients of the variables, while the rightmost column represents the constants on the right-hand side of the equations.
After Applying Gauss-Jordan Elimination, the matrix is transformed into Reduced Row Echelon Form (RREF):
\[ \begin{bmatrix} 1 & 0 & 0 & \vert & -4 \\ 0 & 1 & 0 & \vert & 11 \\ 0 & 0 & 1 & \vert & 19 \end{bmatrix} \]Gauss-Jordan Elimination is an extension of Gaussian Elimination that transforms a matrix into the identity matrix, making it easier to extract solutions directly. This method provides a direct solution without requiring back substitution, unlike standard Gaussian Elimination.
for each row i:
divisor = matrix[i][i]
for each column j in row i:
matrix[i][j] /= divisor
for each row j not equal to i:
multiplier = -matrix[j][i]
for each column k in row j:
matrix[j][k] = matrix[j][k] + multiplier*matrix[i][k]
Gauss-Jordan Elimination Solver
---------------------------------------------------------------------------
Please Enter the filename containing the augmented matrix: data.txt
Original Augmented Matrix:
2 -3 1 -22
7 9 -3 14
6 7 2 91
After Applying Gauss-Jordan Elimination (RREF Form):
1 0 0 -4
0 1 0 11
0 0 1 19
Solution:
x1 = -4
x2 = 11
x3 = 19
Simulation ended. Thank you!
---------------------------------------------------------------------------
| Category | Criteria | Points |
|---|---|---|
| Correctly Declare Required Variables: (10 points) |
String variable:
|
2 |
Correct declaration of a 2D array of doubles:
|
4 | |
Global Variables
|
4 | |
| User Inputs (2 points) |
Reads the filename from standard input and correctly passes it to the readMatrixFromFile function. | 2 |
| gaussJordanElimination Function Implementation (30 points) |
Function Prototype: void gaussJordanElimination(double matrix[][COLS]); | 5 |
| Function Implementation: Accurately implement Gauss-Jordan Elimination. | 20 | |
| Function Activation: The function is called correctly in the main function. | 5 | |
| readMatrixFromFile Functions (20 points) | Function Prototype: bool readMatrixFromFile(const string &filename, double matrix[][COLS]); | 5 |
| Function Implementation: Read the augmented matrix from a file and accurately store it in a 2D array. | 10 | |
| Function Activation: The function is called correctly in the main function. | 5 | |
| displayMatrix Functions (15 points) | Function Prototype: void displayMatrix(double matrix[][COLS]); | 5 |
| Function Implementation: Display the 2D matrix with proper formatting. | 5 | |
| Function Activation: The function is called correctly in the main function. | 5 | |
| Main Function Implementation (10 points) |
Prompt the user to enter input in a clear and correct format. | 5 |
| Proper formatting of program output for readability and clarity. | 5 | |
| Code Readability & Comments (5 points) |
Maintain consistent indentation and spacing throughout the code. | 2 |
| Include comments that explain the purpose of key sections and logic. | 3 | |
| Test Cases & Output Accuracy (5 points) |
The output for different test cases is correct and consistent. | 5 |
| AI disclaimer (3 points) |
Please put an AI disclaimer at the top of your source code as comments. | 3 |
| Errors |
Your program has syntax errors, compiling errors, running errors, or infinite loops. | -50 points |
/*
Author: Your Name
Date: Month/Day/Year
Lab Purpose: Gauss-Jordan Elimination for Solving Linear Equations
A.I. Disclaimer: please put your A.I. disclaimer here
*/
Please submit your Lab 3 source code file: lab3.cpp in D2L!