### alternating values

hi can you help me. i want to have my value in 2nd column under x1, ek0 and ek2 will become the value of my ek0, ek1, and ek2 instead

this is the part of the program

do
{
cout<<"\n"<<count+1<<"."<<setw(16);
for (i=0;i<n;i++)
{
y=x[i];
x[i]=a[i][n];
for (j=0;j<n;j++)
{
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
if (abs(x[i]-y)<=eps)
flag++;
cout<<x[i]<<setw(18);
cout<<abs(x[i]-y)<<setw(18);

Iter x0 x1 x2 ek0 ek1 ek2
------------------------------------------------------------------------------------------------------------------
1. 2.500000 2.500000 7.166667 7.166667 -2.761905 2.761905

2. 4.086310 1.586310 8.155754 0.989087 -1.940760 0.821145

3. 4.004659 0.081650 7.991680 0.164074 -1.999192 0.058432

4. 3.998758 0.005901 7.999451 0.007772 -2.000611 0.001419

5. 4.000084 0.001326 8.000130 0.000679 -1.999945 0.000665

6. 4.000003 0.000082 7.999992 0.000138 -2.000000 0.000055
Last edited on
OK, so you're solving a matrix equation by Gauss-Seidel.

Why don't you show a runnable code (in code tags) and we can format your output for you.
Yes, Thanks!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include using namespace std; int main() { cout.precision(6); cout.setf(ios::fixed); int n,i,j,k,flag=0,count=0; cout<<"\nEnter the no. of equations: "; cin>>n; double a[n][n+1]; double x[n]; double eps,y; cout<<"\nEnter the elements of the augmented matrix (row is per equation without its variables and signs):\n"; for (i=0;i>a[i][j]; cout<<"\nEnter the criteria for termination: "; cin>>eps; for (i=0;i
Last edited on
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.
I'm really sorry this is my first time posting here.

number of equations is =3
criteria for termination is =0.001

sample input numbers under elements of the augmented matrix

-8 1 -2 l -20
2 -6 -1 l -38
-3 -1 7 l -34

Thank you very much I'm really sorry once again
Last edited on
There are quite a lot of things wrong with your code.

The following is (currently) illegal in standard C++ because these would be variable-length arrays:
 ``123`` `````` cin>>n; double a[n][n+1]; double x[n]; ``````

You have muddled up your loops by trying to output the change in x[i] values in the same i loop as actually calculating those values.
- Update ALL the x[i] values, making sure that you finish that i loop.
- Start a new i loop and output all the abs(x[i]-y[i]) values. Note that you will need y[] to be an array, not a scalar, to do this.

(1) You are putting all the setw() items after the thing you are trying to output. It should be before.
e.g.
` cout<<x[i]<<setw(18);`
should be
` cout<<setw(18)<<x[i];`
You will have to correct this in a lot of places.

(2) You are failing to compensate for the width of part items on the line, and also you may need to use "left" to correctly align strings;
e.g.
` cout<<"ek"<<i<<setw(18);`
should be
` cout<<"ek"<<setw(18-2) << left << i;`

(3) The width of some of your headers do not match those in the column of text. For example, the width of your iterator count "(k)" is given as 10, but when you come to output it then you use 18.

- Don't use variable-length arrays (until the standard gets round to allowing them).
- Don't use "magic numbers" like 18 - put its value in a single variable and use that
- Use some in-line spacing and more consistent indentation.
- Read your input from file; it's a pain to put it in from the terminal (or redirect it from a file).

If you make these corrections your output will be
 ```(k) x0 x1 x2 ek0 ek1 ek2 ---------------------------------------------------------------------------------------------------------------- 1 2.500000 7.166667 -2.761905 2.500000 7.166667 2.761905 2 4.086310 8.155754 -1.940760 1.586310 0.989087 0.821145 3 4.004659 7.991680 -1.999192 0.081650 0.164074 0.058432 4 3.998758 7.999451 -2.000611 0.005901 0.007772 0.001419 5 4.000084 8.000130 -1.999945 0.001326 0.000679 0.000665 6 4.000003 7.999992 -2.000000 0.000082 0.000138 0.000055 The solution is as follows: x0 = 4.000003 x1 = 7.999992 x2 = -2.000000```

Last edited on
Thank you very much! I am just a beginner to this and just modifying everything that was taught in school can I request the full formatted program because to be honest I had hard time understanding only by words, please?
Last edited on
Topic archived. No new replies allowed.