### Gauss Jordan Algorithm

closed account (y0XSE3v7)
Hi everyone! I'm a beginner in c++ and have never programmed in any other language before. Right now I am trying to write a simple console application that will use the gauss jordan algorithm to find the solution of a system with n unknowns without using any of the advanced concepts like classes, overloading operators etc. The problem is the code does give me an identity matrix in the coefficient matrix as it is supposed to but it doesnt even touch the last column that is supposed to give the value of the unknowns. What am I doing wrong here?

(To simplfy my question I want to multiply a row and substract it from another one and it does but nothing happens to the last column or last entries. By the way I can shorten the code and write the problematic part if you want me to. )

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include using namespace std; int main() { int n;//n is the number of unknowns cout<<"n: "; cin>>n; float ** A;//Declare a pointer to pointer to use as a dynamic 2D array A=new float*[n]; for (int i=0;i>A[r][c]; } } cout<<'\n'; for (int r=0;r
Last edited on
hello,
i didn't take a close look at your code and my reply will defintely NOT solve your problem, but one tip:
Do no prompt for the whole matrix as an input "element for element".
I'd suggest you simply use a hard coded matrix for testing first, because it will be much less effort than to answer 9 requests for input in case of a rather simple example.
At least that might be useful in testing your algorithm ...
Double check the second arg of those for loops.
closed account (y0XSE3v7)
Thanks for the tip I'll keep that in mind, I know the code looks too messed up but you dont need to look at the whole code just the part written in bold letters.

To test it I just enter n=2 so I only have to enter 6 values 4 for the coefficient matrix and 2 for the constant matrix it doesnt even take 30 seconds.
closed account (y0XSE3v7)
And one more thing, is there a way to input complete rows intead of entering elements one by one as you said?
Hi,

i guess in the end, for a useful application, you should consider to read the input from a .txt - file.
But what you intend is s.th. like

[code]
cin >> num1 >> num2 >> num3 ...
but it won't work without knowing hwo many elts you're about to read.

It wont work without separators i guess, (or someone knows a better ? ... that would be fine for me as well) and string interpretation or s.th.like that.

A = [2 2
0 2]
and b = [4 4] (should fit) would be a good example ... maybe

your code first puts the elements of the matrix on the diagonal to be "1" and then so on, but as the values in the matrix are overwritten, the elements of b won't change. Could that be the pronblem ?
however I think that's the main problem ... but i am far from sure.
Last edited on
Hi, "fevzi".

Nobody writes a Gauss-Jordan program for fun, so I assume you are a student in Computer Programming, Engineering, Science, or some other field that will use math frequently.

I would like to second the advice given by "fluppe": you really should consider inputting data from a text file. Especially during testing/debugging, it will save you the trouble of repeatedly entering the same data manually, and possibly entering typos. This will become more important as your system sizes get bigger. There are a few sample programs posted off the following link if you are interested:

http://www.akiti.ca/DynamicArraysIntro.html

Pick one of those techniques, adapt it for your own application, and use it. If you move forward in the fields of computer programming, engineering, science, etc., chances are you will be using an array input routine a lot more in the future, so find a good routine and lay the groundwork for your future work.
Other people are helping you with the problem, so I won't really butt in and interfere with their efforts. However, I would like to point out that something is missing from your code.

You allocated memory using `new`. It's your responsibility to release it via `delete` or `delete[]` back to the operating system when it's no longer needed. The memory will be freed when the program ends, HOWEVER, for all intents and purposes, you not freeing the memory you allocate when you're done with it is a bug, called a memory leak. You might want to fix it.

-Albatross

P.S. Some people would actually recommend that you use vectors for this application instead of pointers. I tend to agree with those people.
http://www.cplusplus.com/reference/vector/vector/
closed account (y0XSE3v7)
Micheal37 you're right I am an engineering student but not computer engineering but electric electronics engineering and believe it or not I am doing it for fun!Also sorry for the delay probably different time zones.
closed account (y0XSE3v7)
Guys I have just found what the problem was thank you all for help. The code below works fine except for really small numbers it gives "nan" which has probably something to do with the type of the elements but it can solve systems of equations now.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include using namespace std; int main() { int n;//n is the number of unknowns cout<<"n: "; cin>>n; float ** A;//Declare a pointer to pointer to use as a dynamic 2D array A=new float*[n]; for (int i=0;i>A[r][c]; } } cout<<'\n'; for (int r=0;r
Last edited on
PLEASE USE CODE TAGS (the <> formatting button) when posting code.