Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Solve system of equation by Gaussian elimination (http://www.go4expert.com/articles/solve-equation-gaussian-elimination-t17552/)

Nadr 16May2009 10:43

Solve system of equation by Gaussian elimination
 

Introduction



Code for solving system of equation by Gaussian elimination method. The Code is well commented and would not need any further explanation

The code



Code: Cpp

#include <stdio.h>
#include <conio.h>
#define N 100

/****************************************************************/
/***** Gauss elimination method to solve system of equation *****/
/****************************************************************/

void main()
{
    float coeff[N][N+1]={{4,2,1,11},{2,3,4,20},{3,5,3,22},};
    /* Co-efficient inputing variables */
    int i,j,k;    /*    Loop variables */
    int n=3;        /* Number of equations */
    float pivot;    /* pivoting variables */
    float sum;    /* Back substitution sum storing variable */
    float x[N];  /* values of the variables i.e. x's */
    char ch;        /* choice inputing variable */
    do
    {
        printf("\nEnter the number of variables\n");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n+1;j++)
            {
                printf("\nEnter %d row %d col element\n",i+1,j+1);
                scanf("%f",&coeff[i][j]);
            }
        }
        for(k=0;k<n;k++)
        {
            for(i=k+1;i<n;i++)
            {
                pivot=coeff[i][k]/coeff[k][k];
                for(j=k;j<n+1;j++)
                    coeff[i][j]=coeff[i][j]-pivot*coeff[k][j];
            }
        }
        x[n-1]=coeff[n-1][n]/coeff[n-1][n-1];
        for(i=n-2;i>=0;i--)
        {
            sum=0;
            for(j=i;j<n+1;j++)
                sum=sum+coeff[i][j]*x[j];
            x[i]=(coeff[i][n]-sum)/coeff[i][i];
        }
        for(i=0;i<n;i++)
            printf("\nx%d = %g\n",i+1,x[i]);
        printf("\nDo you wish to continue[y/n]\n");
        fflush(stdin);
        scanf("%c",&ch);
    }while(ch=='y' || ch=='Y');
    getch();
}

Post if you have any queries.

kiddo 29May2009 12:34

Re: Solve system of equation by Gaussian elimination
 
What is the use of fflush(stdin)?
can you explain for me?
i don't really know about it,
but I often seen it after the end of a loop.....

nice....

shabbir 29May2009 14:01

Re: Solve system of equation by Gaussian elimination
 
Flushes the input buffer

kiddo 30May2009 20:15

Re: Solve system of equation by Gaussian elimination
 
yeah, I know about flushs the inputBuffer.
But, what I want to ask, "Why we must flush the inputBuffer? Is it important?".
Coz I couldn't answert that question before, when my friend asked me.

---------------------------------------------------
Sorry if my English not that good:surprised

shabbir 31May2009 07:04

Re: Solve system of equation by Gaussian elimination
 
At times the input buffer contains input for which some scanf /cin statements are skipped without asking for input and so its a god practice to flush it.

shabbir 3Jun2009 09:39

Re: Solve system of equation by Gaussian elimination
 
Nomination this Article for Article of the month - May 2009

imjames407 4Jun2009 16:11

Re: Solve system of equation by Gaussian elimination
 
Oh my god it really great.What about Eular's law?

JamC 6Jun2009 12:41

Re: Solve system of equation by Gaussian elimination
 
shabbir,

I would NOT nominate this for Article of the Month...
Reason- Does not follow ANSI C-

int main void

Should there be a requirement as such- if you are going to nominate this..

And
Code:

  float coeff[N][N+1]={{4,2,1,11},{2,3,4,20},{3,5,3,22},};
Not need...

SaswatPadhi 6Jun2009 12:56

Re: Solve system of equation by Gaussian elimination
 
:iagree: +1 JamC.

Nadr 6Jun2009 15:07

Re: Solve system of equation by Gaussian elimination
 
The code works in TurboC because my assignments wanted it to work in that compiler and that is the one used by my university and it does not matter if its nominated or not but my intentions were to share my code for members


All times are GMT +5.5. The time now is 12:44.