Linear Equations Solver  1.0
Using Gaussian elimination
Loading...
Searching...
No Matches
main.cpp File Reference

Entry point for the Gaussian Elimination Solver project. More...

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <filesystem>
#include <limits>
#include "utils.h"
#include "methods.h"
#include "interaction.h"
+ Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

Entry point for the Gaussian Elimination Solver project.

Author
Gilbert Young
Date
2024/09/25

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 41 of file main.cpp.

42{
43 char choice;
44 do
45 {
46 string selected_file = SelectInputFile();
47 if (selected_file.empty())
48 {
49 return 1; // File selection failed
50 }
51
52 // Start timer after selecting the file
53 auto start_time = StartTimer();
54
55 vector<vector<double>> matrix;
56 int rows, cols;
57 if (!InitMatrix(matrix, selected_file, rows, cols))
58 {
59 return 1; // Matrix initialization failed
60 }
61
62 ShowEquations(matrix, rows, cols);
63 cout << "Starting Gaussian elimination process..." << endl;
64 int exchange_count = GaussianElimination(matrix, rows, cols);
65 cout << "Gaussian elimination completed." << endl
66 << endl;
67
68 int rank = DetermineRank(matrix, rows, cols);
69 bool consistent = CheckConsistency(matrix, rows, cols);
70
71 if (!consistent)
72 {
73 cout << "The system of equations is inconsistent and has no solution." << endl;
74 }
75 else if (rank < (cols - 1))
76 {
77 ShowGeneralSolution(matrix, rows, cols, rank);
78 }
79 else
80 {
81 vector<double> solution;
82 bool solvable = BackSubstitution(matrix, rows, cols, solution);
83 if (solvable)
84 {
85 DisplaySolution(solution);
86 }
87 else
88 {
89 cout << "The system of equations is inconsistent and has no solution." << endl;
90 }
91 }
92
93 // Stop timer after the solution is displayed
94 StopTimer(start_time);
95 choice = AskRunAgain();
96
97 } while (choice == 'y' || choice == 'Y');
98
100 return 0;
101}
string SelectInputFile()
Allows the user to select an input .in file from the current directory.Returns an empty string if no ...
char AskRunAgain()
Return char The user's choice ('y', 'Y', 'n', 'N').
void WaitForExit()
Waits for the user to press Enter before exiting.
int DetermineRank(const vector< vector< double > > &m, int rows, int cols)
Determines the rank of the coefficient matrix A (excluding augmented column).
Definition methods.cpp:188
int GaussianElimination(vector< vector< double > > &m, int rows, int cols)
Performs Gaussian elimination on the augmented matrix with partial pivoting.
Definition methods.cpp:75
bool BackSubstitution(const vector< vector< double > > &m, int rows, int cols, vector< double > &solution)
Performs back-substitution to find the solution vector.
Definition methods.cpp:137
void ShowGeneralSolution(const vector< vector< double > > &m, int rows, int cols, int rank)
Displays the general solution for systems with infinitely many solutions.
Definition methods.cpp:211
void DisplaySolution(const vector< double > &solution)
Displays the unique solution.
Definition utils.cpp:147
void StopTimer(const chrono::steady_clock::time_point &start)
Definition utils.cpp:162
bool InitMatrix(vector< vector< double > > &m, const string &filename, int &rows, int &cols)
Initializes the matrix by reading from a .in file.
Definition utils.cpp:24
void ShowEquations(const vector< vector< double > > &m, int rows, int cols)
Displays the system of linear equations.
Definition utils.cpp:78
chrono::steady_clock::time_point StartTimer()
Definition utils.cpp:157
bool CheckConsistency(const vector< vector< double > > &m, int rows, int cols)
Checks the consistency of the system of equations.
Definition utils.cpp:123