Jacobi c++

Where is my mistake? Please help , it only do the first iteration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

const int p=1;
int main()
{
    int n=2;

   float A[n][n];
    for(int i=0;i<=n;i++)
       { for(int j=0;j<=n;j++)
    {
        cout<<"A["<<i<<"]["<<j<<"]: ";
        cin>>A[i][j];
    }
    }

    double b[n];
    cout<<"b: "<<endl;
    for(int i=0;i<=n;i++)
    {
        cout<<"b["<<i<<"] : ";
        cin>>b[i];
    }
    cout<<endl;

    float c[n][n];
    for(int i=0;i<=n;i++)
    {   for(int j=0;j<=n;j++)
    {
         c[i][j]= -(A[i][j]/A[i][i]);
    }
             c[i][i]=0;
    }

    float d[n];
    for(int i=0;i<=n;i++)
    {
        d[i]=b[i]/A[i][i];
    }

    cout<<"d= "<<endl;
    for(int i=0;i<=n;i++)
    {
        cout<<d[i]<<endl;
    }
    cout<<endl;
        float x[n];
        float xnew[n];
        for(int i=0;i<=n;i++)
        {
            x[i]=0;
            cout<<"x["<<i<<"]="<<x[i]<<'\t';
            xnew[i]=0;
        }
        cout<<endl;
        for(int k=0;k<=p;k++)
       { for(int i=0;i<=n;i++)
        {
            xnew[i]=d[i];
            for(int j=0;j<=n;j++)
            {
                if(i!=j)
                {
                    xnew[i]+=c[i][j]*x[i];
                }
            }
            x[i]=xnew[i];
            cout<<"x["<<i<<"]= "<<x[i]<<'\t';
        }
         cout<<endl;
       }
    return 0;
}
Valid indices for an array are 0 to size-1 where size is the number of elements in the array.

You access outside the bounds of the array(s) resulting in undefined behavior. Don't access outside the bounds of the array(s.)
So what should I change?
If you have an array with 2 elements, the indices of those elements will be 0 and 1. If you run your for loop from 0 to <=2, then you will be trying to access indices 0, 1 and 2. If you want to stay in bounds, the for loop should just run from 0 to 1 (i.e. <2).

In general, that's 0 to size-1 as cire pointed out, where size is the number of elements in the array.
Oh my bad .. n=2 because of the arrays .. I need the arrays to be with 3 elements , thats why in the for loops i have <=
Last edited on
anyone ?
If you want 3 elements then make

const int n=3; // Note: const otherwise it's invalid [current] C++

and what the other say:

for(int i=0;i<n;i++) // Note: < not <=
Ok , but it still doesn't work
What exactly does "doesn't work" mean? Does it distribute illegal content? Does it randomly mute your volume or turn it to max? Does it cause inflation in a foreign country? You need to be specific.
Sorry,
Well , it finds the first iteration correctly, but the second and further aren't correct ..
:(
help please :(
:(
If I put the changes coder777 suggested into your code and input random numbers (1 through 18 for A, and 22,23,24 for B), I get this output. What output are you expecting if this isn't what you wanted?

A[0][0]: A[0][1]: A[0][2]: A[1][0]: A[1][1]: A[1][2]: A[2][0]: A[2][1]: A[2][2]: b: 
b[0] : b[1] : b[2] : 
d= 
10
2.2
1.33333

x[0]=0	x[1]=0	x[2]=0	
x[0]= 10	x[1]= 2.2	x[2]= 1.33333	
x[0]= -40	x[1]= -2.2	x[2]= -0.888889	
Last edited on
http://math.semestr.ru/optim/iter.php here is the online calculator for this :)
Topic archived. No new replies allowed.