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. )
#include <iostream>
#include <cmath>
usingnamespace 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=newfloat*[n];
for (int i=0;i<n;i++)
{
A[i]=newfloat[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';
}
}
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 ...
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.
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.
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:
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.
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.
#include <iostream>
#include <cmath>
usingnamespace 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=newfloat*[n];
for (int i=0;i<n;i++)
{
A[i]=newfloat[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';
}
}
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/