Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Two-Dimensional Array

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
like image 639
Aaron Avatar asked May 24 '11 07:05

Aaron


People also ask

What is the 2 dimensional array in C?

The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure.

What are 2 dimensional arrays?

A two-dimensional array is a data structure that contains a collection of cells laid out in a two-dimensional grid, similar to a table with rows and columns although the values are still stored linearly in memory.

Does C support 2D arrays?

The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.

Which is syntax for 2 dimensional array?

Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4];


2 Answers

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).

like image 100
Baltasarq Avatar answered Oct 18 '22 15:10

Baltasarq


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.....?

like image 41
Pushpendra Avatar answered Oct 18 '22 15:10

Pushpendra