5

I'm going to ask something about my code provided below... My question is in the line where there's a comment /*This line*/. I used variable y and x: y for the HEIGHT and x for the WIDTH. The very first time I run the program, the code was scanf("%d,%d", &y, &x); but unfortunately, the program was not running properly. But when I modified the code with this scanf("%d,%d", &x, &y);, then, I was able to run the program in turn. I can't understand how it happened, since I had set the y as HEIGHT and x as WIDTH?

   File   Edit   Run   Compile   Project   Options   Debug   Break/watch
╒════════════════════════════════════ Edit ════════════════════════════════════╕
│      Line 1     Col 43  Insert Indent Tab Fill Unindent * C:NONAME.C         │
│#define HEIGHT 5                                                              │
│#define WIDTH 10                                                              │
│                                                                              │
│char enemy[HEIGHT][WIDTH]=                                                    │
│        { {0,0,0,0,0,0,0,0,0,0},                                              │
│          {0,1,1,0,0,1,0,0,0,0},                                              │
│          {0,0,0,1,0,1,0,1,1,0},                                              │
│          {0,0,0,0,0,0,0,0,1,1},                                              │
│          {0,0,1,1,0,1,0,0,0,1} };                                            │
│                                                                              │
│main()                                                                        │
│{                                                                             │
│        char friend[HEIGHT][WIDTH];                                           │
│        int x,y;                                                              │
│                                                                              │
│        clrscr();                                                             │
│                                                                              │
│        for(y=0; y<HEIGHT; y++)                                               |
|               for(x=0; x<WIDTH; x++)                                         |
|                      friend[y][x]='.';                                       |
|                                                                              |
|        while(x >= 0)                                                         |
|        {                                                                     |
|                for(y=0; y<HEIGHT; y++)                                       |
|                {                                                             |
|                        for(x=0; x<WIDTH; x++)                                |
|                                printf("%c", friend[y][x]);                   |
|                        printf("\n");                                         |
|                }                                                             |
|                                                                              |
|                printf("Coordinates: ");                                      |
|                scanf("%d,%d", &x, &y);                       /*This line*/   |
|                                                                              |
|                if(enemy[y][x] == 1)                                          |
|                        friend[y][x]="\xDB";                                  |
|               else                                                           |
|                        friend[y][x]="\xB0";                                  | 
|        }                                                                     |
|}                                                                             │
├─────────────────────────────────── Watch ────────────────────────────────────┤
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
 F1-Help  F5-Zoom  F6-Switch  F7-Trace  F8-Step  F9-Make  F10-Menu   NUM
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Aaron
  • 1,969
  • 6
  • 27
  • 47

2 Answers2

6

The problem is the input data. 5,2 meant row 5, column 2, and the problem is that row 5 does not exist (Height is 5, so you have rows from 0 to 4). As soon as you changed the values, they became 2, 5, which correctly point to row 2 (third row) and column 5 (sixth column).

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • Another question: Since I've set the *array enemy* as `enemy[5][10]`, and the C arrays are zero-based, would it be wrong because I initialized it with 10 columns and 5 rows? – Aaron May 24 '11 at 08:33
  • @aerohn, it is not wrong as long as you access it in the segments 0..4, 0..9. I mean, in your initialization you set ten rows x 5 columns, and you have that: the only gotcha is that you have to remember to access them properly. There are ten rows numbered from 0 to 9, but don't let the maximum row number index fool you, there are still ten rows! – Baltasarq May 24 '11 at 09:17
  • Arrays in C are zero-indexed, basically. Didn't see how old this answer is, sorry... – A Person Jan 10 '14 at 07:51
3

If you are using 5 and 2 for y[HEIGHT] and x[WIDTH], How it will work because your enemy[HEIGHT][WIDTH] array is the array of 5x10.

That is when you are using 5 for y that exceeds it limit that is 0 to 4.

Isn't it.....?

Pushpendra
  • 4,344
  • 5
  • 36
  • 64
  • 1
    HEIGHT is set to 5[i.e. 0-4] and WIDTH is set to 10[i.e. 0-9] for Array enemy, right? When you are trying to scanf("%d%d",&y,&x); and giving 5 and 2 as HEIGHT and WIDTH. But the value of y should be between 0(Minimum) and 4(maximum). But you are giving 5 that exceeds its limit....? – Pushpendra May 24 '11 at 08:07
  • @Pushpendra... yes, arrays in C is zero-based, I got it. I tried to set HEIGHT to 6 and WIDTH to 11... and then I modified the said line as `scanf("%d,%d", &y, &x);`. But still, it wasn't working properly. – Aaron May 24 '11 at 08:15
  • about my array, is this initialization correct? `char enemy[5][10] = { {0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,0,1,0,0,0,0}, {0,0,0,1,0,1,0,1,1,0}, {0,0,0,0,0,0,0,0,1,1}, {0,0,1,1,0,1,0,0,0,1} };` – Aaron May 24 '11 at 08:41