How to work with Two Dimensional Arrays in C
78We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array. The 2D array is also known as Matrix or Table, it is an array of array. See the below image, here each row is an array.
Declaration of 2D array:
Syntax: data_type array_name[row_size][column_size];
Example: int arr[3][3];
So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).
2D Array
Memory Map of 2D Array
|
Logitech diNovo Keyboard for Notebooks
Price: $48.49
List Price: $99.99 |
|
Kindle Wireless Reading Device (6" Display, U.S. Wireless)
Price: $259.00
|
|
Apple iPod touch 8 GB (2nd Generation) [Previous Model]
Price: $186.00
List Price: $199.99 |
|
No Line On The Horizon
Price: $5.99
|
|
21st Century Breakdown (Amazon MP3 Exclusive) [Explicit]
Price: $9.99
|
|
It's Blitz!
Price: $5.00
|
|
Creative Labs Xmod USB Sound Card for PC & Mac
Price: $21.99
List Price: $79.99 |
|
Western Digital My Passport Essential 500 GB USB 2.0 Portable External Hard Drive WDME5000TN (Midnight Black)
Price: $98.88
List Price: $139.99 |
|
Garmin nüvi 780 4.3-Inch Widescreen Bluetooth Portable GPS Navigator with MSN Direct Service
Price: Too low to display
List Price: $699.99 |
|
Garmin Forerunner 305 GPS Receiver With Heart Rate Monitor
Price: Too low to display
List Price: $299.99 |
|
Canon PowerShot SD1200IS 10 MP Digital Camera with 3x Optical Image Stabilized Zoom and 2.5-inch LCD (Silver)
Price: $149.00
|
Code for assigning & displaying 2D Array
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int arr[3][3]={
{12, 45, 63},
{89, 34, 73},
{19, 76, 49}
};
clrscr();
printf(":::2D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
So in the above example we have declared a 2D array named arr which can hold 3x3 elements. We have also initialized that array with values, because we told the compiler that this array will contain 3 rows (0 to 2) so we divided elements accordingly. Elements for column have been differentiated by a comma (,). When compiler finds comma in array elements then it assumes comma as beginning of next element value. We can also define the same array in other ways, like.
int arr[3][3]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][3]={12, 45, 63, 89, 34, 73, 19, 76, 49};
But this kind of declaration is not acceptable in C language programming.
int arr[2][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49};
To display 2D array elements we have to just point out which element value we want to display. In our example we have a arr[3][3], so the array element reference will be from arr[0][0] to arr[2][2]. We can print display any element from this range. But in our example I have used for loop for my convenience, otherwise I had to write 9 printf statements to display all elements of array. So for loop i handles row of 2D array and for loop j handles column. I have formatted the output display of array so that we can see the elements in tabular form.
Visitor's Opinion
Is this tutorial on 2D-Array clear enough to understand?
See results without votingUseful Tutorials
- Best Free PHP Tools & Editors for Every PHP Programmer
PHP (recursive acronym for: PHP Hypertext Preprocessor), is a widely-used, Open Source embedded scripting language. PHP is a server-side scripting language usually written in an HTML context. Unlike an ... - How to enable “Show Hidden Files and Folders” option disabled after virus attack
If you ever faced such problem when your computer had a virus which ultimately disabled “show hidden file and folders” option, then you don’t need to format or restore your setting. I have a trick... - Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you don’t need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you... - How to work from home on internet
How to make money online? This is the common and highest searched term on internet. You will also get numerous answers to this query. But, before following any answer, please make sure which type of money... - How to setup network between XP and Vista PCs
Vista is Microsoft’s new operating system and it provides different network and security method than Windows XP, to its user. So in order to create network between Vista and XP, we have to enable few...
PrintShare it! — Rate it: up down flag this hub










bucles says:
2 months ago
nice-1!