Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My code in c for gauss jacobi isn't working? why is it so?

0 votes
asked Mar 4, 2019 by Harika (160 points)
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

int a[5][5],b[5],n,i,j,flag=1,key=0;

char c;

float e,sum,xo[10],x1[10],s;

setbuf(stdout,NULL);

printf("This program illustrates Gauss Jacobi Method in C :\n");

printf("Enter the dimension of the coefficient matrix A:");

scanf("%d",&n);

printf("Enter the %d elements of the coefficient matrix A:",n*n);

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

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

scanf("%d",&a[i][j]);

printf("Enter the elements of the constant matrix B:");

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

scanf("%d",&b[i]);

printf("The given system of linear equations is\n");

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

{

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

{

if((j==0 || j==1 ) && (a[i][j+1]>0))

c='+';

else if(j==2)

c='=';

printf("%dx%d\t%c",a[i][j],j+1,c);

}

printf("%d",b[i]);

printf("\n");

}

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

    {

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

        {

            if(i!=j)

               s=s+a[i][j];

        }

        if(fabs(a[i][i])<fabs(s))

        {

            flag=0;

            break;

        }

    }

if(flag==0)

{

printf("Error!The given system of linear equations is not diagonally dominant.");

exit(0);

}

else

{   

    printf("The given system of linear equations is diagonally dominant.a\n");

printf("Enter the initial approximate solution to the given system:");

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

{

scanf("%f\t",&xo[i]);

}

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

{

printf("x%d=%f\t",i+1,xo[i]);

}

printf("Enter the allowed error:");

scanf("%f",&e);

do{

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

{

sum=b[i];

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

{

if(j!=i)

sum=sum-a[i][j]*xo[j];

}

x1[i]=sum/a[i][i];

if(fabs(x1[i]-xo[i])<e)

{

      key=key+1;

      xo[i]=x1[i];

}

}

}while(key<n);

           

}

if(key==n)

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

     printf("%f\t",xo[i]);

}

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...