Friday, June 5, 2020

What is 2D and 3D Array in C ? Explanation & Example

Lets us Understand 2D and 3D array in simple and detailed manner . In this Post first of all we will understand what 2D array is with example and then we will move on to 3D array explanation . So to understand 2D and 3D array scroll down ↓ 
 



A two dimensional array gives row-column like structure. This type of array can be intialised at run time or compile time initialization .

Syntax:-      
datatype arrayname[row size][column size] ;



Example:-
int arr[3][2];    //declaration



Initialization:-
int table[3][3]={1,2,3,4,5,6};
or
int table[3][3]={1.2.3.4.5.6};



Run Time Initialization:- 
int table[2][3];
for(i=0;i<2;i++)
{
      for(j=0;j<3;j++)
     {
        scanf("%d"&table[i][j]);
      }
}


Advantages of Array:-

1.     Not necessay to declare large number of variables all the values can be stored in the same array.
2.     Since consecutive memory locations are alloted to the array, reading the values become faster.

Disadvantages of Array:-

1.     An  array can store values of single datatype for which it is created. 
      example:- If an array of integer type is declared then the array can store only integer values.
2.     The array size is fixed.The memory allocated to the array cannot be increased or decreased.
3.     Since, the array cannot be fixed ,if we allocated more than required then memory space will be wasted. If  we allocate less memory space the all values cannot get stored in the arrray.
4.     Since the elements of array are stored in consecutive locations, inserting or deleting a value is not possible.

Program:- Initialize 2dimensional array with 3rows and 2columns and print transpose
#include<stdio.h>
void main()
{
int table[3][2];
for(int i=0;i<3;i++)
{
      for(int j=0;j<2;j++)
   
        scanf("%d",&table[i][j]);
   
}
for(int i=0;i<3;i++)
{
      for(int j=0;j<2;j++)
    printf("%d",table[i][j]);
   
}

for(int i=0;i<2;i++)
{
      for(int j=0;j<3;j++)
   
        printf("%d",table[i][j]);
   
}

}


3D array in C

3 D array is the array which has 3 dimensions as x,y,z,structure. As we make some enginering graphs in x,y,z, axis . It is similar to that of 2D but has an Additional dimension.

Syntax:-      
datatype arrayname[x size][y size][z size] ;



Example:-
int arr[3][2][4];    //declaration



Initialization:-
int table[2][2][2]={1,2,3,4,5,6,7,8};





Programs:-


For many other Language Programs of microprocessor 8086, C++ , C , Data Structures visit our  BLOG 

There are programs of Object-Oriented Programming, Operators (logical, arithmetic ,relational,etc) ,exception handling , inheritance ,string operations ,etc

No comments:

Post a Comment