2d matrix and changing values of all neighbours

Hello All.

I am working with 2d matrix. I'm trying to change values (from 1 to 4) of all neighbours surrounding my starting point. Unfortunately, my current code is changing all of the neighbours as well as the starting point. How can I avoid this? My code is:

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
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdio>
#include <fstream>
#include <queue>
#include "node.h"
#include <math.h>
#include <string>
 
using namespace std;
 
void loadMap();
void displayMap();
 
//Map size
const int Nsize = 5;                        //number of rows
const int Msize = 7;                        //number of columns
 
//Start location
int sX=2;
int sY=1;
 
 
//Map
static int map[Nsize][Msize];        //work map
 
//Directions
const int dir=8;
static int dirX[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dirY[dir]={0, 1, 1, 1, 0, -1, -1, -1};
static int dir_map[Nsize][Msize];  //map of directions
 
 
int main()
{
        loadMap();
 
        for (int i=0; i<dir; i++)
        {
                for(int j=0; j<dir; j++)
                {
 
 
        map[sX+dirY[i]][sY+dirX[j]] = 4;
                }
        }
 
        displayMap();
 
 
getchar();
return 0;
 
}
 
void loadMap ()
{
//load the map from .txt file
ifstream file;
file.open("testmap.txt");
 
if(file.good()==true)
{
    for(int i=0;i<Nsize;i++)
    {
       for(int j=0;j<Msize;j++)
       {
          file>>map[i][j];
       }
    }
    file.close();
}
else
{
    cout<<"Cannot open the file";
}
}
 
void displayMap()
{
        //Display the map
for(int i=0;i<Nsize;i++)
{
    for(int j=0;j<Msize;j++)
    {
        cout<<" "<<map[i][j];
    }
        cout<<endl;
    }
}


I have tried adding

if ((sX+dirY[i] != sX) && (sY+dirX[j] != sY))

before changing the values inside double for loop, but in this case, it only changes the values of diagonal neighbours, ant not the others.
I will appreciate some help with this.

Thanks in advance
Topic archived. No new replies allowed.