Programmingoneonone is known for sharing best programming knowledge.

Two or multidimensional array in c++ programming

Two or multidimensional array:

The two-dimensional array represents tables of values consisting of information arranged in rows and columns. and to identify a particular table element we must specify two subscripts.
two and multidimensional array in c++ programming, learn c++ programming
learn c++ programming
the first identifies the element's row and the second identifies the element's column. 
the array which requires two subscripts to identify a particular element is called two-dimensional array. 
The array which content m rows and n columns is called an m-by-n array.


Declaration of a two-dimensional array:

the declaration of the two-dimensional array is the same as one dimensional, the little difference is only of size.

syntext:
            <return-type> <array-name>[rows][columns];

number of elements in the array = rows*columns

Example:
                int array[5][5];
                
in the above example:

int = return type, it means we can only store the integer value in the array.
array = array is the name of the array.
[5][5] = rows and columns of the two-dimensional array.
             
number of elements in array = 5*5 = 25 
means we can store the maximum 25 elements in the array.

Note: the index number for rows and columns for the two-dimensional array starts from 0 and 0. 
        

Initialization the values for the two-dimensional array:

likewise the one-dimensional array we can also give the values to the two-dimensional array using two methods.

at declaration time/ compile time.at run time. 

at declaration time/ compile time:

likewise the one-dimensional array we need to initialize the values 

syntext:
            { {values for 1 row}, {values for 2 row}, ....... {values for m row} };

Example:
                int array[2][3]= {{1,2,3}, {4,5,6}};

in the above example, there is 2 rows and 3 columns. the {1,2,3} are the values for the 1 row, and in the first row the 1 is assigned to the 1-row-1-column, 2 assigns to the 1-row-2-column, 3 assigns to the 1-row-3-column.
and here the {4,5,6} is assign to the 2-row-1-column, 5 assign to 2-row-2-column, 6 assign to 2-row-3-column. 

Note: you can also directly initialize the values to the two-dimensional array using this syntax:

syntax:             <array-name>[location-of-row][location-of-column] = value;

Example:
                arr[0][1] = 2;

in the above example, I initialize the value 2 to the first row and second column of the array.

at run time:


likewise one-dimensional array we also used for loop to assign the values at the run time to the two-dimensional array. but we need two for loop to assign the value to the 2-dimensional array.

syntext:
                for(i=0;i<rows;i++)
                {

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

                    {
                      
                      cin>>arr[i][j];

                    }

                }

Print the elements of the two-dimensional array:

to print the elements of the two-dimensional array we need two for loop with the same syntax likewise to initialize the values to the 2-dimensional array. only the change is that cout<< at the place of cin>>. 
                     
syntax:
            for(i=0;i<rows;i++)
           {
                 for(j=0;j<colums;j++)
                 {
                      cout<<arr[i][j];

                 }


           }


A simple example program to initialize the value and print the values of a two-dimensional array

here in this example, we use two arrays, in the first array we initialize the values at the declaration time and in the second array we initialize the value at the run time using for loop.

#include <iostream>

int main()

{
       
  int first[2][2]={ {1,2}, {3,4}}; 
/* initialize the value at the declaration time */
  int second[2][2];

  std::cout<<"\n enter the values for second array:";


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

  {
           // set the values for second array using for loop
        for(j=0;j<2;j++)
       {
           std::cout<<"\n enter the value for the second array for the"                             << i <<"row and" << j<< "column";                                     std::cin>>first[i][j];

        }

            
  }
        
  std::cout<<"\n print the values of first and second array:";

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

  {
       for(j=0;j<2;j++)
       {
           std::cout<<"\t element of the first array "<<first[i][j];
        }
     std::cout<<"\n";
  }

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

 {
     for(j=0;j<2;j++)
     {
         std::cout<<"\t element of the second array"<< second[i][j];
     }
   std::cout<<"\n";
 }

         return 0;

}


I think now you understood how to declare a two-dimensional array and how to initialize the value to the array and how to print the value of the two-dimensional array.

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."

No comments:

Post a Comment