c++ declare 2 dimensional array code example

Example 1: three dimensional array c++

// Two dimensional array
int a[2][3]= {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    cout << a[1][1]; // Output is 5

// Three dimensional array
//[2] is elements; [3] is rows in elements; [4] is column in elemnents 
int a[2][3][2]= {
        //Element 0
        { {1, 2}, 
          {2, 3}, 
          {4, 5} 
            
        },
        
        
        // Element 1
        { {6, 7}, 
          {8, 9}, 
          {10, 11} 
            
        }
    };
    
    cout << a[0][1][1]; // Prints 3

Example 2: multidimensional array c++

int MyArray[2][3] = { {2, 3, 4}, {12, 13, 14} };

	//visualize
	//2, 3, 4
	//12, 13, 14

	std::cout << MyArray[0][2] << endl; // 4
	std::cout << MyArray[1][1] << endl; // 13

//First square bracket means how many rows you want in your array
//Second squire bracket is for how many qulam you want in your array

Tags:

Cpp Example