# Help plz!!!

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

1. ### girish3110New Member

Joined:
Nov 8, 2006
Messages:
7
0
Trophy Points:
0
Hello there!! Am havin a lil problem with a program that should carry out iterative methods,namely Jacobi and Seidel methods. The problem is that I don't know (or I can't) how to stop the iteration. The code is given below and if anyone could make any adjustments,plz...
Code:
```#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#define err 1e-4
void main()
{
int a,b,c,d,i,n,choice=0;

//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;
cout<<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;
//clrscr();

//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));

if (choice==1) //If 1 is input, perform Gauss-Jacobi method;
{
int k=1;
float x,y,z,x1,y1,z1,x2,y2,z2;
y=0;
z=0;
x=0;
x1=(d-(b*y)-(c*z))/a;
y1=(d-(a*x)-(c*z))/b;
z1=(d-(a*x)-(b*y))/c;
cout<<"x:"<<setprecision(n)<<x1<<"\t";
cout<<"y:"<<setprecision(n)<<y1<<"\t";
cout<<"z:"<<setprecision(n)<<z1<<endl<<endl;
do
{
x2=x;
y2=y;
z2=z;
x=(d-(b*y1)-(c*z1))/a;
y=(d-(a*x1)-(c*z1))/b;
z=(d-(a*x1)-(b*y1))/c;
k++;
x1=x;
y1=y;
z1=z;
cout<<"x:"<<setprecision(n)<<x1<<"\t";
cout<<"y:"<<setprecision(n)<<y1<<"\t";
cout<<"z:"<<setprecision(n)<<z1<<endl<<endl;
}while(???);

cout<<"Using Gauss-Jacobi Method..."<<endl;
cout<<"Number of iterations performed: "<<k<<endl;
cout<<"Value of x is "<<x1<<endl;
cout<<"Value of y is "<<y1<<endl;
cout<<"Value of z is "<<z1<<endl;
}

else //If 2 is input, perform Gauss-Seidel method.
cout<<"Seidel";
}
```
The Do...WHILE loop is the problem. Try using the following values for the program:
a1=8 , b1=-3 , c1=2 , d1=20
a2 =4 , b2=11 , c2=-1 , d2=33
a3=6 , b3=3 , c3=12 , d3=35

The answers should be x= 3.0168 , y= 1.9859 and z=0.9118

Joined:
Apr 4, 2005
Messages:
1,645
87
Trophy Points:
0
Occupation:
Programmer
Location:
Kolkata, India
What algorithm is this?

3. ### girish3110New Member

Joined:
Nov 8, 2006
Messages:
7
0
Trophy Points:
0
It's used to solve a system of linear equations by using iteration and the method is that of Jacobi.

Joined:
Nov 8, 2006
Messages:
7