"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Saturday, January 16, 2010

Solve Linear equations through Gauss Elimination method.


#include<iostream.h>
#include<conio.h>
#include<math.h&gt
void gauss1(int n, float a[10][10], float b[10], float x[10]);
void elim(int n, float a[10][10], float b[10]);
void bsub(int n, float a[10][10], float x[10]);


void main()
{
clrscr();
int status, n, i, j;
float a[10][10], b[10], x[10];


cout<<"\n What is the size of the system / dimmension (n) :";


cin>> n ;


cout<<"\n Enter coefficients a(i,j) , row-wise :";


for( i=0 ;i < n ; i ++ )


for (j=0;j < n ; j++)


cin>>a[i][j];


cout<<"\n Input vector b :";

for(i=0;i < n; i++)


cin>>b[i];


//obtain solution by gauss elimination method


gauss1(n, a, b, x);


cout<<"\n Solution vector X \n ";


for(i=0;i < n ; i++)


cout<< x[i]<<"\t";


getch();


}






void gauss1(int n, float a[10][10], float b[10],  float x[10])


{


elim(n,a,b);


bsub(n,a,b,x);


return;


}






void elim(int n, float a[10[10], float b[10], float x[10])


{


int i,k,j;


float factor;


for(k=0;k < n-1 ; k++ )


{


 for(i=k+1; i < n;i++)


{


factor = a[i][k]/a[k][k];


    for(j=k+1;j < n;j++)


   { a[i][j] = a[i][j] - factor* a[k][j];}


}


return;


}






void bsub(int n, float a[10][10], float b[10] float x[10])


{


int i,j,k;


float sum;


x[n-1] = b[n-1] / a[n-1][n-1];


for(k = n -2; k>=0; k--)


{


sum = 0.0;


for(j = k+1; j < n ; j++)


sum = sum + a[k][j] * x[j];


x[k] = (b[k] - sum) / a[k][k];


}


return;


}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.