Thursday, 13 May 2021

Two dimensional arrays in C programming

A tow dimensional array is an array of one dimensional array. In other words two dimensional array in C is an array of one dimensional array.

we can taught a two dimensional array as table, where row and columns indicates two dimensions of the array.


 

Declaration:

 Before using two dimensional arrays in C program you must declare it. The general syntax to declare a two dimensional array is

Syntax: type array-name[number of rows][number of columns];

Example: int a[2][3];

Declaration tell to the compiler three things

  1. The type of the array.
  2. The name of the array
  3. The number of elements stored in it.

Initialization:

Declaration of array reserves only memory for the elements in the array. No values will be stored. The values of array elements are stored when it is initialized.

1. A two dimensional array ma be initialized at the time of its declaration.

2.  Example: int a[2][2]={10.20.30.40};

3.Two dimensional arrays may be initialized row by row.

    Example: int a[2][2]={ {10.20},{30.40}};

4.When the array is completely initialized, C permits is to omit the first dimensional value in its initialization.   

   Example: int a[][2]={10,20,30,40};

5.When the elements are to be initialized to zero, the following shortcut method may be used.

  Example: int a[2][2]={ {0},{0}};

6. To initialize large arrays we use for loop statements. For two dimensional arrays we use two for loops. One for reading row values and one for reading columns values.

   Example:

              for(i=0;i<50;i++)

               {

                  for(j=0;j<50;j++)

                  {

                            scanf("%d",&a[i][j]);

                     }

             }

Accessing: Since 2D array consist of two indices we use two for loops to access / print array elements.

Example:

              for(i=0;i<50;i++)

               {

                  for(j=0;j<50;j++)

                  {

                            printf("%d",&a[i][j]);

                     }

             }


0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top