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:
1 2 3
|
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.
On to your formatting.
(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.
Other advice:
- 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 |