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
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 ;
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
- #include <iostream>
- using namespace std;
- int main( ){
- // Array declaration 3 rows and 2 columns
- int rollNumber[3][2]={{100 , 101},
- {102 , 103},
- {104 , 105} };
- //value at 0,0 index
- cout <<"0 Row 0 col :"<<rollNumber[0][0]<<endl;
- //value at 3rd row,2nd col
- cout <<"3rd Row 2nd col :"<<rollNumber[2][1]<<endl;
- //value at 2nd row and 2nd col index
- cout<<"2nd row 2nd col :"<<rollNumber[1][1]<<endl;
- // updating value at 1,1 index
- rollNumber[1][1]=540;
- //updated value at 1st row and 1st col
- cout<<"2nd row 2nd col updated:"<<rollNumber[1][1]<<endl;
- return 0;
- }
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
- #include <iostream>
- using namespace std;
- int main( ){
- // Array declaration 3 rows and 2 columns
- int rollNumber[3][2]={{100 , 101},
- {102 , 103},
- {104 , 105} };
- for(int row=0;row<3;row++){
- for(int col=0;col<2;col++){
- cout<<rollNumber[row][col]<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Console :
100 101
102 103
104 105
Animated Gif of TwoDArray statement
Watch Video
C++ TwoDArrays for Beginners Lecture in Urdu Hindi
Comments
Post a Comment