Jump to content
• Advertisement

Public Group

# help with solving matrices program

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hey I am trying to make a program to solve a system of 3 linear equations using matrices using rref. I can't seem to get the fourth matrix right and was wondering if somebody could point out what I was doing wrong. Thanks
#include <iostream>
#include <cctype>

using namespace std;

/* Function Prototypes */

void introduction();
void inputMatrix();
void outputMatrix();
void solveSystem();

/* Constants */

const int ROWS = 3;
const int COLUMNS = ROWS + 1;

/* Variables */

int matrix[ROWS][COLUMNS];

int main()
{
char solveAgain;

introduction();
do {
inputMatrix();
outputMatrix();
solveSystem();
cout << "\nWould you like to solve another system? (y/n): ";
cin >> solveAgain;
solveAgain = toupper(solveAgain);
} while (solveAgain == 'Y');

return 0;
}

void introduction()
{
cout << "\tEnter the Matrix!\n\n" << endl;
cout << "This program will solve a system of " << ROWS << " equations with" << endl;
cout << ROWS << " unknown varibles using row reduction echelon form." << endl;
cout << endl;
cout << "Enter the coefficients in this order:" << endl;
cout << endl;
cout << " 11 | 12 | 13 | 14 " << endl;
cout << "-------------------" << endl;
cout << " 21 | 22 | 23 | 24 " << endl;
cout << "-------------------" << endl;
cout << " 31 | 32 | 33 | 34 " << endl;
}

void inputMatrix()
{
cout << endl;

for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
{
cout << "Matrix" << i + 1 << j + 1 << ": ";
cin >> matrix[j];
}
}
}

void outputMatrix()
{
cout << endl;

for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
{
cout << matrix[j] << "\t";
}
cout << endl;
}
}

void solveSystem()
{
// first matrix
for (int i = 1; i < ROWS; ++i)
{
double multiplier = matrix[0];
for (int j = 0; j < COLUMNS; ++j)
{
matrix[j] = multiplier * matrix[0][j] - matrix[j] * matrix[0][0];
}
}
outputMatrix();

// second matrix
for (i = 2; i < ROWS; ++i)
{
double multiplier = matrix[1];
for (int j = 1; j < COLUMNS; ++j)
{
matrix[j] = multiplier * matrix[1][j] - matrix[j] * matrix[1][1];
}
}
outputMatrix();

// third matrix
for (i = 2; i < ROWS; ++i)
{
double divider = matrix[2];
for (int j = 2; j < COLUMNS; ++j)
{
matrix[j] = matrix[j] / divider;
}
}
outputMatrix();

// fourth matrix
for (i = 0; i < ROWS - 1; ++i)
{
double multiplier = matrix[2][2];
for (int j = 0; j < COLUMNS; ++j)
{
matrix[j] = multiplier * matrix[j] - matrix[2][j] * matrix[2];
}
}
outputMatrix();
}


#### Share this post

##### Share on other sites
Advertisement
Hi FadedMaximus.

Your method is a little confusing: I'm not entirely sure what the four matrices are supposed to do. The first two matrices successfully reduce to upper-triangular form, but the second two are a mystery to me. It certainly doesn't look like they are moving towards echelon form. If you could explain the purposes of the four matrices then I may be able to produce something a bit closer to your original intention.

It's impossible to reduce the matrix to echelon form in general using integers, as the normalising divide would round everything to hell. Also, no attempts are made to ensure that this is possible, so divide-by-zero errors should be expected in the singular case.
Can you be sure that the division in 'third matrix' goes perfectly?

I suspect that your problem with the fourth matrix may be that matrix[2][2] and matrix[2] are the wrong way around:
// fourth matrix	for (i = 0; i < ROWS - 1; ++i)	{		double multiplier = matrix[i][2];		for (int j = 0; j < COLUMNS; ++j)		{			matrix[j] = multiplier * matrix[j] - matrix[2][j] * matrix[2][2];		}	}
but again, I'm not sure as I can't tell what it's supposed to do. In my test case, this brings the solution to very nearly echelon form, but I suspect that's only because I have a conveniently placed 1 at a[2][3].

Regards
Admiral

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5
• Advertisement

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!