Gauss-siedel method to solve system of equations

Discussion in 'C++' started by Nadr, May 16, 2009.

  1. Nadr

    Nadr New Member

    Joined:
    Oct 16, 2007
    Messages:
    165
    Likes Received:
    1
    Trophy Points:
    0

    Introduction



    Gauss-siedel method to solve system of equations :undecided

    The code



    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #define N 100
    #define ACC 0.000001
    
    /************************************************************/
    /***** Gauss-siedel method to solve system of equations *****/
    /************************************************************/
    
    void main()
    {
    	float coeff[N][N+1]={{4,2,1,11},{3,5,3,22},{2,3,4,20},};
    	/* Co-efficient inputing variables */
    	float x[N]={0,0,0};	/* values of the variables i.e. x's */
    	int n=3;			/* Number of equations */
    	int i,j;			/*	Loop variables */
    	float acc;			/* Accuracy determining variable */
    	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",&arr[i][j]);
    			}
    		}
    		do
    		{
    			acc=coeff[0][n];
    			for(i=0;i<n;i++)
    			{
    				x[i]=coeff[i][n];
    				for(j=0;j<n;j++)
    				{
    					if(i==j)
    						continue;
    					x[i]=x[i]-coeff[i][j]*x[j];
    				}
    				x[i]=x[i]/coeff[i][i];
    			}
    			for(i=0;i<n;i++)
    				acc=acc-coeff[0][i]*x[i];
    		}while(fabs(acc)>=ACC);
    		for(i=0;i<n;i++)
    			printf("%g\t",x[i]);
    		printf("\nDo you wish to continue[y/n]\n");
    		fflush(stdin);
    		scanf("%c",&ch);
    	}while(ch=='Y' || ch=='Y');
    	getch();
    }
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Hi Nadr,

    You forgot to remove the background, it contains the default text :p

    But anyway, the article was very useful, thanx for that. :)
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Done that for him. Thx for pointing it out
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice