Skip to main content

C++ TwoD Array 2D Beginner Programming

C++ TwoD Arrays(2D)

2DArray is known as data structure statement.
TwoDArray use to store collection of same datatype data of fix size or user required size.We can say array is a collection of variables of same datatype.

same datatype means if we want to store rollNumbers of Students then we'll use integer( int ) data type because rollNumbers are integers numbers.


if we want to store name of students then we'll use string datatype to store names of different students because name is combination of characters or Alphabets.


if we want to store rollNumbers of 50 Students then we'll create 50 variables of int datatype.No this is not a good Programming practice.


for better performance we'll create an integer TwoDarray having size of 50 to store rollnumbers of Students.


if we declare an TwoDarray of student rollNumbers then each rollnumber of each student called element of TwoDarray and every element or rollNumber is accessed by that specific index.


when we create an TwoDarray it reserve memory locations of specified size. TwoDarray index start from 0 if you want to access value of first element from first memory location of an array then we'll use 0 index.


datatype myarray [ ][ ] ;


it is known as Two dimension array. in array double [ ] [ ] it must b an integer value greater than zero (0) like [2][3] and you can use any c++ supportable datatype.


for example if you want to add or create 50 elements in an array called rollNumber of integer datatype you'll use following statement


datatype myarray [ ][ ] ;

int rollNumber [2][2] ;

you'll use following statement for initialize TwoDarray elements or values

           arrayname            row      col           value
int rollNumber [ 0 ][ 0 ] = 100 ;
int rollNumber [ 0 ][ 1 ] = 100 ;


or you can use following statement for adding values in one statement
int rollNumber[2][2] = { {100 , 101} , {102 , 103 }} ;

Number of elements or values in { } can't be larger than the number of [ arraysize ]

if you want to print 4th element or value you can access it by using 4th index in [ ] with array name because index of an array is start from 0.

rollNumber[1,1];

in c++ you can use this statement in cout to print the 4th element value like

cout<<rollNumber[1][1];

Example 1

Array

index

  1. #include <iostream>  
  2. using namespace std; 
  3. int main( ){ 
  4.     // Array declaration 3 rows and 2 columns
  5.    int rollNumber[3][2]={{100 , 101},
  6.                          {102 , 103},
  7.                          {104 , 105}  };
  8.     //value at 0,0 index
  9.     cout <<"0 Row 0 col :"<<rollNumber[0][0]<<endl;
  10.     //value at 3rd row,2nd col    
  11.     cout <<"3rd Row 2nd col :"<<rollNumber[2][1]<<endl;
  12.     
  13.     //value at 2nd row and 2nd col index
  14.     cout<<"2nd row 2nd col :"<<rollNumber[1][1]<<endl;

  15.     // updating value at 1,1 index
  16.     rollNumber[1][1]=540;
  17.     //updated value at 1st row and 1st col
  18.     cout<<"2nd row 2nd col updated:"<<rollNumber[1][1]<<endl;
  19.     return 0;
  20. }
                                                Console :
                                                0 Row 0 col : 100
                                                3rd Row 2nd col : 105
                                                2nd row 2nd col : 103
                                                2nd row 2nd col updated : 540

                                                Example 2

                                                Printing Complete Array

                                                index

                                                1. #include <iostream>
                                                2. using namespace std; 
                                                3. int main( ){ 
                                                4.     // Array declaration 3 rows and 2 columns
                                                5.    int rollNumber[3][2]={{100 , 101},
                                                6.                          {102 , 103},
                                                7.                          {104 , 105}  };
                                                8.    for(int row=0;row<3;row++){
                                                9.        for(int col=0;col<2;col++){
                                                10.        cout<<rollNumber[row][col]<<" ";
                                                11.        }
                                                12.        cout<<endl;
                                                13.    }
                                                14.      return 0;
                                                15. }
                                                                                              Console :
                                                                                              100 101
                                                                                              102 103
                                                                                              104 105
                                                                                              Animated Gif of TwoDArray statement
                                                                                              Watch Video
                                                                                              C++ TwoDArrays for Beginners Lecture in Urdu Hindi

                                                                                              Comments

                                                                                              Popular posts from this blog

                                                                                              C++ Classes and Objects Programming

                                                                                              Animated Gif of class and object C++ class and Object A class is a blue print and combination of Different functions and variables. The  difference between C and C++  is that  C  is a procedural  programming  and does not support classes and objects, while  C++  provide support of Object Oriented Programming(OOP) and classes  therefore  C++  can be called a hybrid language. The variables and functions within a class are called members(variables,functions) of the class. Syntax of C++ Class A class definition starts with the keyword  class. For example, we defined the Student data type using the keyword  class  as follows. class Student { public:   string name; // member variables   int rollNumber; } A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as  private  o...

                                                                                              C++ Pass by Reference and Value Beginner Programming

                                                                                              Animated Gif of Pass by Reference & Value C++ pass by Reference & value There is two way of accessing values of variables and passing values in function argument, first one is pass by value and second one is pass by reference . Reference variable is another name for a existing variable. Once a reference variable is initialized the reference name may be used to refer to the variable. A reference must be initialized when it is created.You can't have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.Once a reference is initialized to an object, it cannot be changed to refer to another object. & operator use to declare reference variable which is use to store variable value, you can access the value of the variable by using original variable name or by using the reference variable. Syntax of Reference  variable  is very simple //variable initialization int vari = 10; //sim...

                                                                                              C++ Member Initializer Beginner Programming

                                                                                              C++ Member initializer Animated gif example  Member initializer C++ Member initializer for Beginners Lecture