Hello,
I'm having a bit of a problem. I need to write a program that will read a matrix from text file, display the matrix, then reduce it to RREF and display the reduced matrix. I have part of the code that gets it to display the original matrix but I have no idea how to get it to RREF. Any help will be really appreciated. Thanks!!!
#include <fstream> //connects program to fstream
#include <iostream> //connects program to iostream
#include <cmath> //connects program to cmath
usingnamespace std; //ALlows programmer to use namespace
int rows, columns; //creates variables to hold numbers for matrix
int firstNum, num; //creates variables to solve problems in matrix
int main() //Main is the beginning of the actual program
{//Start Main
//This gets the number of rows and columns that you will need
cout << "How many unknowns? "<<endl; //Allows the user to input data into array
cin >> rows; //adds in rows
columns = rows + 1;
cout<<endl;
cout<<"Your matrix is "<<endl<<endl;
float Matrix[rows][columns]; //this creates the first matrix that the user inputs
float SolvedMatrix[rows][columns]; //this creates the solved matrix
//This puts data into the actual matrix
ifstream Infile; //allows programmer to use files
Infile.open("matrixdata.txt"); //opens file for reading
for (int i = 0; i < rows; i++) //start for rows
{//start for
for (int n = 0; n < columns; n++) //start for columns
{//start for
Infile>>Matrix[i][n];
}//end for
}//end for
Infile.close(); //closes the file
firstNum = Matrix[0][0]; //this finds the first number of the array
for (int i = 0; i < rows; i++) //this for loop displays the first matrix for the user
{//start for
for (int n = 0; n < columns; n++)
{//start for
cout << Matrix[i][n] << " ";
}//end for
cout <<endl;
}//end for
cout << endl; //this puts a space inbetween the unsolved matrix and the solved matrix
//This solves the first row of the matrix
for (int i = 0; i < columns; i++)
{
num = Matrix[0][i];
SolvedMatrix[0][i] = num / firstNum; //this is the equation to solve the first line of the matrix
}
//This solves the next rows of the matrix
for (int i = 1; i < rows; i++)
{//start for
for (int n = 0; n < columns; n++)
{//start for
firstNum = Matrix[i][0] * -1; //this makes the first number negative of each row
num = Matrix[i][n]; //this finds the number being solved
SolvedMatrix[i][n] = (num / firstNum) + SolvedMatrix[0][n]; //this is the equation to solve the matrix
}//end for
}//end for
cout<<"The solved matrix is "<<endl<<endl;
//This displays the new Matrix
for (int i = 0; i < rows; i++)
{//start for
for (int n = 0; n < columns; n++)
{//start for
cout << SolvedMatrix[i][n] << " ";
}//end for
cout << " " << endl;
}//end for
return 0; //Program returns nothing
}//End Main
I've managed to get this far, but I'm having a problem with getting the math correct. the most bottom right number should be a nine. Done on paper.