jacobi coding help

Hi everybody

i have a problem in jacobi coding i wanna help me

the code below
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
#include<iostream>
#include<cmath>


using namespace std;
int main()
{int b[4][4];
int a [4][4];
int r=0, i=0 ,j=0,x=0,y=0 ;
for(i=0;i<=4;i++)
{	for(j=0;j<=4;j++)
cin>>a[i][j];
}
{for(i=1;i<4;i++)
{	for(j=1;j<4;j++)
r=(a[i+1][j]+a[i-1][j]+a[i][j+1]+a[i][j-1]+a[i][j])/5;
//r++;
a[i][j]=r;
cout<<"    " << a[i][j]<<"  ";
}
}


system("pause");

return 0;
}


i want the enter the no
and have the result as shown below

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

i wanna see the last array
and i want to get the result in a new array

How can i do that

iam begginer in programing

thanks
:)
Indentation. Because it helps.
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
#include<iostream>
#include<cmath>

int main()
{
  int b[4][4];  // unused variable
  int a[4][4]; // 16 elements
  int r=0, i=0, j=0, x=0, y=0;

  for ( i=0; i<=4 ;i++) // this uses 0-4. Five values. a has only four: 0-3
  {
    for(j=0;j<=4;j++) // this uses 0-4. Five values. a has only four: 0-3
      cin >> a[i][j];  // you read 25 elements.  Out of range error.
  }

  {
    for ( i=1; i<4; i++) // this uses 1-3
    {
      for ( j=1; j<4; j++) // this uses 1-3
        r = (a[i+1][j] + a[i-1][j] + a[i][j+1] + a[i][j-1] + a[i][j]) / 5;  // calculated 9 times.
        // 4 is an invalid index.  Out of range error.
        // integer division discards remainder, i.e. 4/5 == 0


      // These happen only within the outer loop; three times
      //r++;
      a[i][j]=r;
      cout<<"    " << a[i][j]<<"  ";
    }
  }

  return 0;
}

Fix the more obvious errors first.
Topic archived. No new replies allowed.