Gauss Jordan Algorithm

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. )


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cmath>

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<n;i++)
    {
        A[i]=new float[n+1];
    }
    cout<<'\n';
    for (int r=0;r<n;r++)//Get the array elements from the user
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<"A["<<r+1<<"]"<<"["<<c+1<<"] :";
            cin>>A[r][c];
        }
    }
    cout<<'\n';
    for (int r=0;r<n;r++)//Display the array on the console screen
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<" "<<A[r][c]<<" ";
        }
        cout<<'\n';
    }

    for (int a=0;a<n;a++)
    {
        for(int x=0;x<(n+1);x++)
        {
            A[a][x]=A[a][x]/A[a][a];
        }
        for (int i=0;i<n;i++)
        {

            for (int j=0;j<(n+1);j++)
            {
                if(a==i) continue;
                A[i][j]=A[i][j]-A[a][j]*A[i][a];
            }
        }
    }
    cout<<"Solution of the system:"<<'\n';
    for (int r=0;r<n;r++)//The result after apllying the gauss algorithm
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<" "<<A[r][c]<<" ";
        }
        cout<<'\n';
    }

}
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.
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.
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.

Anyway, regarding your code:

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/
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.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>

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<n;i++)
    {
        A[i]=new float[n+1];
    }
    cout<<'\n';
    for (int r=0;r<n;r++)//Get the array elements from the user
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<"A["<<r+1<<"]"<<"["<<c+1<<"] :";
            cin>>A[r][c];
        }
    }
    cout<<'\n';
    for (int r=0;r<n;r++)//Display the array on the console screen
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<" "<<A[r][c]<<" ";
        }
        cout<<'\n';
    }

    for (int a=0;a<n;a++)
    {
        float k=A[a][a];
        for(int x=0;x<n+1;x++)
        {
            A[a][x]=A[a][x]/k;
        }
        for (int i=0;i<n;i++)
        {
            float m=A[i][a];
            for (int j=0;j<n+1;j++)
            {
                if(i==a)continue;
                A[i][j]=A[i][j]-A[a][j]*m;
            }
        }
    }
    cout<<"Solution of the system:"<<'\n'<<'\n';
    for (int r=0;r<n;r++)//The result after apllying the gauss algorithm
    {
        for (int c=0;c<n+1;c++)
        {
            cout<<" "<<A[r][c]<<" ";
        }
        cout<<'\n';
    }

}
Last edited on
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.