Yeah, i am trying to create a Linear Algebra matrix. And some one helped me with it, and now i have two classes, one for The matrix, and the other one for Fraction.. when the code was completed, the code wasn't working properly, and the person told be, it could be because i'm using double, and i should create Fractions.. And i am not familiar with Fraction class or how i should replace the doubles with the. so i have been trying to figure it out since Friday, nothing seems to work. Can you please help, if it is not too much to ask
Here is the code
void Matrix::print(ostream& out) {
int r, c;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
out << setprecision(3) << this->get(r, c) << '\t';
}
out << endl;
}
}
void Matrix::randomize(int min, int max) {
srand(time(NULL));
int r, c;
int range = max - min + 1;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
double random = min + rand() % range;
this->set(r, c, random);
}
}
}
void Matrix::scaleRow(int row, double scale) {
int c;
for (c = 0; c < this->size+1; c++) {
double scaledValue = this->get(row,c) * scale;
this->set(row, c, scaledValue);
}
}
void Matrix::addRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double sum = this->get(row1, c) + this->get(row2, c);
this->set(row1, c, sum);
}
}
void Matrix::swapRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double temp = this->get(row1, c);
this->set(row1, c, this->get(row2, c));
this->set(row2, c, temp);
}
}
bool Matrix::swapZeroColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (this->get(r, column) != 0) {
this->swapRows(r, column);
return true;
}
}
return false;
}
void Matrix::reduceColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (r != column) {
double reduceFactor = - 1 / this->get(r, column);
this->scaleRow(r, reduceFactor);
this->addRows(r, column);
}
}
}
bool Matrix::solve() {
int c;
for (c = 0; c < this->size; c++) {
if (this->get(c, c) == 0) {
if (!this->swapZeroColumn(c)) {
// no non-zero elements in column, not singular
return false;
}
}
// scale diagonal element to 1
this->scaleRow(c, 1 / this->get(c, c));
// zero out other elements in column
this->reduceColumn(c);
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int n, int d = 1);
Fraction operator+(Fraction);
Fraction operator-(Fraction );
Fraction operator*(Fraction );
Fraction operator/(Fraction );
void Print(ostream& out);
};
Fraction::Fraction(int n, int d = 1) {
this->numerator = n;
this->denominator = d;
}
Fraction operator +(Fraction num, Fraction den)
{
int n, d, n1, d1, mult;
if (d==0){exit(0);}
num = (n*d1)+(n1*d);
den =(d*d1);
}
Fraction operator -(Fraction num, Fraction den){
int n, d, n1, d1;
if (d==0){exit(0);}
num = (n*d1)-(n1*d);
den =(d*d1);
}
Fraction operator*(Fraction num, Fraction den){
int n,d,n1, d1;;
if (d==0){exit(0);}
num= (n*n1);
den= (d*d1);
}
Fraction operator/(Fraction num, Fraction den){
int n,d,n1, d1;
if (d==0){exit(0);}
num= (n*d1);
den= (d*n1);
}
void main(){
int num, den;
cout<<num<<"/"<<den<<endl;
}