Hello!! Below is the code: Code: #include <iostream.h> #include <iomanip.h> void main() { bool convergence=false; //Variable to store status for convergence. int a[3],b[3],c[3],d[3],i,n,choice,count=1; float x,y,z,x1,y1,z1,x2,y2,z2; //Displays the 'format' of the linear equations. for (i=1;i<=3;i++) cout<<"a"<<i<<"x + b"<<i<<"y + c"<<i<<"z = d"<<i<<endl<<endl; cout<<"Make sure that the system of linear equations are diagonally dominant..."<<endl; //Asks user to input the values of a,b,c,d and the accuracy of the calculation. for (i=0;i<3;i++) { cout<<"Enter value of a"<<i+1<<" :"; cin>>a[i]; cout<<"Enter value of b"<<i+1<<" :"; cin>>b[i]; cout<<"Enter value of c"<<i+1<<" :"; cin>>c[i]; cout<<"Enter value of d"<<i+1<<" :"; cin>>d[i]; cout<<endl; } cout<<"Enter the accuracy(number of decimal places): "; cin>>n; //Displays the linear equations. for (i=0;i<3;i++) cout<<a[i]<<"x + "<<b[i]<<"y + "<<c[i]<<"z = "<<d[i]<<endl; cout<<endl; //Asks user to choose iterative method. do { cout<<"Choose iterative method..."<<endl<<endl; cout<<"(1) Gauss Jacobi"<<endl; cout<<"(2) Gauss Seidel"<<endl<<endl; cout<<"Enter choice: "; cin>>choice; if ((choice==1) || (choice==2)) //Exit the WHILE..DO loop if either 1 or 2 is input. break; }while((choice!=1)||(choice!=2)); //Display menu until 1 or 2 is input. if (choice==1) //If 1 is input, perform Gauss-Jacobi method; { cout<<endl<<endl; //Initial values of x,y and z. y=0; z=0; x=0; //Carry out first iteration. x2=(d[0]-(b[0]*y)-(c[0]*z))/a[0]; //Calculate new value of x. y2=(d[1]-(a[1]*x)-(c[1]*z))/b[1]; //Calculate new value of y. z2=(d[2]-(a[2]*x)-(b[2]*y))/c[2]; //Calculate new value of z. //setiosflags(ios::fixed) used to allow values to have same number of decimal places. cout<<"x:"<<setiosflags(ios::fixed)<<setprecision(n)<<x2<<"\t"; //Display first value of x. cout<<"y:"<<setiosflags(ios::fixed)<<setprecision(n)<<y2<<"\t"; //Display first value of y. cout<<"z:"<<setiosflags(ios::fixed)<<setprecision(n)<<z2<<endl<<endl; //Display first value of z. //Continue iterations with the previous calculated values of x,y and z. for (int j=1;j<=14;j++) { //Store values obtained from previous iteration in order to calculate convergence. x1=x; y1=y; z1=z; x=(d[0]-(b[0]*y2)-(c[0]*z2))/a[0]; //Calculate new value of x. y=(d[1]-(a[1]*x2)-(c[1]*z2))/b[1]; //Calculate new value of y. z=(d[2]-(a[2]*x2)-(b[2]*y2))/c[2]; //Calculate new value of z. count++; //Increment count,i.e. number of iterations peformed. //Store values of x,y and z obtained from most recent iteration. x2=x; y2=y; z2=z; //Display current values of x,y and z for most recent iteration. cout<<"x:"<<setiosflags(ios::fixed)<<setprecision(n)<<x2<<"\t"; cout<<"y:"<<setiosflags(ios::fixed)<<setprecision(n)<<y2<<"\t"; cout<<"z:"<<setiosflags(ios::fixed)<<setprecision(n)<<z2<<endl<<endl; [COLOR=DarkRed]if ((x2-x1)==0) break;[/COLOR] } //Display final result of iterations performed. cout<<"Using Gauss-Jacobi Method..."<<endl; //Check if convergence has been achieved or not. if (convergence=true) cout<<"Convergence achieved."<<endl; else cout<<"No convergence achieved."<<endl; cout<<"Number of iterations performed: "<<count<<endl; //Display total number of iterations performed. cout<<"Value of x is "<<setiosflags(ios::fixed)<<setprecision(n)<<x2<<endl; //Display final value of x. cout<<"Value of y is "<<setiosflags(ios::fixed)<<setprecision(n)<<y2<<endl; //Display final value of y. cout<<"Value of z is "<<setiosflags(ios::fixed)<<setprecision(n)<<z2<<endl<<endl; //Display final value of z. } } //End of Program\\ I'm having problem with the code in red. The for loop does not stop,thought the values of x1 and x2 become the same... Plz help,me...
As x1 and x2 are float they can have 0.0001 as the value diff and your condition may not satisfy. Type cast it to int.