Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
#include<iostream>
#include<string>
#include<cmath>
#include<vector>


using namespace std;


class matrix{

private:
int row;
int coloumn;
double * x;

public:
matrix();
matrix(int nrow,int ncol);
double * gaussElimination(double **a , double *b,matrix & u);
~matrix();


};

matrix::matrix()
{
    row=0 ;
    coloumn=0;
    x=NULL;
}

matrix::~matrix()
{delete x;}

matrix::matrix(int nrow,int ncol)
{
    row=nrow;
    coloumn= ncol;
    for (int i=0;i<row;i++)
        {x=new double [row+1];}
}

double * matrix:: gaussElimination (double ** a,double *b,matrix & u)
{
    int m,n,i,j,k;
    m=u.row;
    n=u.coloumn;
    
    a=new double * [m+1];
    b=new double  [m+1];
    
    for (i=1;i<n;i++)
    {
         a[i]=new double [n+1];
    }

    // row operations
    double Sum,ratio;
    for (k=1;k<=m-1;k++)
        for (i=k+1;i<=m;i++)
        {
            ratio=a[i][k]/a[k][k];
            for (j=1;j<=m;j++)
                a[i][j] -= ratio*a[k][j];
            b[i] -= ratio*b[k];
        }
    
    // backward substitutions
    for (i=m;i>=1;i--)
    {
        Sum=0;
        x[i]=0;
        for (j=i;j<=m;j++)
            Sum += a[i][j]*x[j];
        x[i]=(b[i]-Sum)/a[i][i];
    }
    
    cout<<endl;
    
    return x;
    
    for (i=0;i<=m;i++)
    {delete a[i];}
    delete a,x,b;
    

}

int main(){

    const int m=3,n=3; //row,col
    double **amatrix,*bmatrix;
    
    amatrix=new double * [m+1];
    bmatrix=new double  [m+1];
    
    for (int i=0;i<n;i++)
    {
         amatrix[i]=new double [n+1];
    }
    
    //double amatrix[m][n],bmatrix[m];
    
    for (int j=0;j<m;j++)
    {
        bmatrix[0]=2;bmatrix[1]=10;bmatrix[2]=4;
        for (int i=0;i<n;i++)
        {
            amatrix[0][0]=10;amatrix[0][1]=2;amatrix[0][2]=3;
            amatrix[1][0]=2;amatrix[1][1]=1;amatrix[1][2]=2;
            amatrix[2][0]=3;amatrix[2][1]=50;amatrix[2][2]=2;
        }
    }
    
    matrix matrix1;
    matrix();
    matrix(m,n);
    
    for (int i=0;i<m;i++)
        {cout<<matrix1.gaussElimination(amatrix,bmatrix,matrix1)<<endl;}
    
    for (int i=0;i<=m;i++)
    {delete amatrix[i];}
    delete amatrix,bmatrix;
    

}
Posted
Updated 8-Aug-15 2:02am
v2
Comments
PIEBALDconsult 7-Aug-15 12:55pm    
I suspect it's an integer division issue, but I don't know the subject well enough to understand the code.
enhzflep 7-Aug-15 13:32pm    
Things I see a problem with:
- the constructor matrix(int nrow, int ncol)
You allocate memory nrow times, yet always set x to hold the result. This means you can access the memory requested by 1 of (the last) allocations, and have leaked all of the other ones, no longer able to access or free them.

- In main you enter a loop and set values for bmatrix and amatrix, yet none of the loop variables have any bearing on the values you set, or the indices of the matrix elements you set them to - that is to say, the pair of loops here are unnecessary - you only need to perform the operations once.

I dont have time to look more currently, but will also point out that the program crashes - you're almost certainly accessing memory that you don't 'own'.
[no name] 8-Aug-15 8:14am    
Only a Detail which is not the source of your current Problem:

...
return x;

for (i=0;i<=m;i++)
{delete a[i];}
delete a,x,b;
....

I don't think the code after "return" will be relevant ;)

1 solution

Obviously, your code don't do what you expect, there is 1 general solution to this:
Run your code on debugger and see what it is really doing.
You will see that somewhere, it don't do what you expect.

And you will learn a lot of valuable things.
 
Share this answer
 
v2
Comments
enhzflep 8-Aug-15 4:39am    
In the future,it would be advisable to add content like this as a comment, since it really isn't a solution by any sane definition of the word.
Patrice T 8-Aug-15 7:31am    
It is like the saying:
- Give a fish to a man, he will eat one day.
- Teach him fishing, he will eat for its life.
I consider that learning the debugger is like fishing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900