# Break and Continue problem

Discussion in 'C' started by girish3110, Nov 9, 2006.

1. ### girish3110New Member

Joined:
Nov 8, 2006
Messages:
7
0
Trophy Points:
0
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,b,c,d,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-(b*y)-(c*z))/a; //Calculate new value of x.
y2=(d-(a*x)-(c*z))/b; //Calculate new value of y.
z2=(d-(a*x)-(b*y))/c; //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-(b*y2)-(c*z2))/a; //Calculate new value of x.
y=(d-(a*x2)-(c*z2))/b; //Calculate new value of y.
z=(d-(a*x2)-(b*y2))/c; //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...

Joined:
Jul 12, 2004
Messages:
15,329
377
Trophy Points:
83
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.

3. ### girish3110New Member

Joined:
Nov 8, 2006
Messages:
7
Thanx!! 