Iterating through a multidimensional array

I'm working on a program that will include a very basic "map editor" of a sorts. (The "map" is actually a 2D integer array of 1's and 0's, 1 = wall, 0 = open space). And I want to add a tool to draw a line of 1's or 0's horizontally or vertically and currently I get a user to input a start coordinate, an end coordinate, and a tile type (1 or 0). However when it comes to iterating through this array and changing the contents of the elements, only the first (starting) coordinate is changed. All others stay unchanged.

Iterative Code:
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
void operateSetLine(int x, int y, int endX, int endY, int tile)
{
     if(x == endX)
     {
          if(y > endY)
          {
              for(int i = y; y >= endY; --y)
              {
                      nScenario[i-1][x-1] = tile;
              }
              return;
          }
          if(y < endY)
          {
              for(int i = y; y <= endY; ++y)
              {
                      nScenario[i-1][x-1] = tile;
              } 
              return;
          }
     }
     if(y == endY)
     {
          if(x > endX)
          {
              for(int i = x;x > endX; x--)
              {
                      
                      nScenario[y-1][i-1] = tile;
              } 
              return;
          }
          if(x < endX)
          {
              for(int i = x; x < endX; x++)
              {
                      nScenario[y-1][i-1] = tile;
              }
              return;
          }
     }
}


Any advice would be greatly appreciated, so thanks in advance :)
You have stuff like this for all your loops:
for(int i = y; y >= endY; --y)

Think about i.
1
2
3
4
              for(int i = y; y >= endY; --y)  // <- you are modifying y
              {
                      nScenario[i-1][x-1] = tile; // <- but are using i as your index
              }


since i never changes, you only modify the first tile.

You probably meant to modify i in the loop:

1
2
3
4
              for(int i = y; i >= endY; --i)  // <- decrement i, and compare i with endY
              {
                      nScenario[i-1][x-1] = tile;
              }


You appear to have this problem in all of your loops.


EDIT: ninja'd
Last edited on
Ahh... Stupid mistake there on my part :) Thanks for the help.
Topic archived. No new replies allowed.