Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Sunday, November 15, 2020

2D array in C

2D array: simply can be defined as an array of arrays (In other words you can say collection of several one dimensional arrays is called 2D array).

2D array in C | welcome2protec


2D array: simply can be defined as an array of arrays (In other words you can say collection of several one dimensional arrays is called 2D array).

/* Here, you can see four arrays inside one array. That's how 
 * you can say "an array of arrays" in known as 2D array. */
 
   int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};


1D array can be seen as data elements organized in a row. And 2D array is similar to 1D array but, it can be visualized as a grid (or table) with rows and columns as you can see below example.

/* Here, 2D array is organized in one row similar to 1D array */
  
   int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
  
/* But, it can be visualized as a grid (or table)
   with rows and columns as you can see below. */
   
    col-1   col-2    col-3
     
    {1,         2,       3}     // Row-1
    {4,         5,       6}     // Row-2
    {7,         8,       9}     // Row-3
    {10,       11,      12}     // Row-4
  

In above, arr[4][3] first arr[4] represent number of rows, and 2nd arr[3] represent number of columns. Let's understand with a simple example at given below.

Note: 2D array is organized as matrices which can be represented as the collection of rows and columns as you can see above. However 2D arrays are used to create database in data structure.


Example 1: A simple example of 2D array

/* A simple example of 2D array */

#include <stdio.h>
#include <conio.h>

int main()
{
 /* 2D array declaration with initialization*/
 int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; 
 int i, j;
 clrscr();

 /* Traversing 2D array by using nested for loop */
 printf("Elements of 2D array:\n");
 for(i=0; i<=3; i++)
 {  
    for(j=0; j<=2; j++)
    { 
      printf("%d    ",arr[i][j]);
	if(j==2)
	 printf("\n");
    }
  }
  getch();
 }    

Output

Elements of 2D array:
  
  1    2    3
  4    5    6
  7    8    9
  10  11   12 
  

How to take input data from user in 2D array? See below example2


Declaration of 2D array:

The basic syntax of declaring a 2D array is given below:

/* Syntax for declaring a 2D array */
  
  Data_Type Array_Name[Row_Size][Column_Size];
  
/* For Example: An int array of 12 elements (row * column i.e 4*3), 
 * where arr is the name of 2D array, [4] represent number of rows, 
 * and [3] represent number of columns */
 
  int arr[4][3]
  

Initialization of 2D array:

A 2D array can be initialized by three ways

// First method
  int arr[4][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Since the above array have 4 rows and 3 columns. Therefore, the elements will store in array in the order like 1st three elements will be stored in first row, next three elements in 2nd row, and similarly last three elements will be stored in 4th row.


This type of initialization use of nested braces. Each set of inner braces represent one row. since this declaration have total 4 rows so there are 4 sets of inner braces.

// second method
  int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

A 2D array can also be declared as below. In this declaration you must always specify the second dimension even if you are specifying elements during declaration.

/* Third method */
  int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9};

As you can see the second declaration is better and more readable than the 1st and 3rd one. I recommend you to use the 2nd method as it is more readable and in this method you can visualize the rows and columns of 2D array.


Let’s see the below initialization of 2D array to understand it better:

Int arr[2][3] = {2,4,5,8,9,10}      /*Valid declaration*/
Int arr[ ][3] = {2,4,5,8,9,10}      /*Valid declaration*/

Int arr[ ][ ] = {2,4,5,8,9,10}      /*Invalid declaration*/
Int arr[2][ ] = {2,4,5,8,9,10}      /*Invalid declaration*/

Example 2: Taking inputs from user in 2D array

/* Program to take data as input form user */

#include <stdio.h> 
#include <conio.h>

int main()
{
 /* Declaration of 2D array*/
 int arr[3][4];
 /* Counter variables for the loop */
 int i, j;
 clrscr();

 /* Taking input from user */
 for(i=0; i<=2; i++)
 {
    for(j=0; j<=3; j++)
    {
       printf("Enter vlaue for arr[%d][%d]:");
       scanf("%d", &arr[i][j]);
    }
 }
 /* Displaying array elements */
 printf("Elements of 2D array:\n");
 for(i=0; i<=2; i++)
 {
    for(j=0; j<=3; j++)
    {
      printf("%d ", arr[i][j]);
      if(j==3)
      printf("\n");
    }
 }
  getch();
 }

Output

  Enter value for arr[0][0]: 1
  Enter value for arr[0][1]: 2
  Enter value for arr[0][2]: 3
  Enter value for arr[0][3]: 4
  
  Enter value for arr[1][0]: 5
  Enter value for arr[1][1]: 6
  Enter value for arr[1][2]: 7
  Enter value for arr[1][3]: 8
  
  Enter value for arr[2][0]: 9
  Enter value for arr[2][1]: 10
  Enter value for arr[2][2]: 11
  Enter value for arr[2][3]: 12
  
  Elements of 2D array:
  
  1    2    3    4
  5    6    7    8
  9   10   11   12
  

Memory representation of 2D array:

2D array can be visualized as a grid (or table) with rows and columns with x rows and y columns where row number ranges from 0 to (x-1) and columns number ranges from 0 to (y-1). For example: A 2d array arr[4][3] with 4 rows and 3 columns. As you can see at below diagram

Conceptual representation of 2D array | welcome2protec

Every elements in a 2D array is identified by an element name which is in the form of arr[i][j], where 'arr' is the name of an array, and 'i' represent the row index and 'j' represent the column index. that uniquely identify each element in a 2D array as you can see at above diagram.


However, the actual representation of this array in memory would be something like below diagram.

Actual memory representation of 2D array | welcome2protec

Since, this array is of integer type so each elements would use 4 bytes of memory that's the reason there is a difference of 4 in element's addresses.

Note: The address are generally in hexadecimal. In above diagram showing these addresses in integer just because to show you that the elements are stored in contiguous locations.














Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us