How to get matrix size in C++

1 Answer

0 votes
#include <iostream>
  
int main() {
    int matrix[][4] = { {2, 0, 5, 0},
                        {0, 4, 0, 0},
                        {0, 8, 0, 9} };
      
    int rows = sizeof matrix / sizeof matrix[0]; 
    int cols = sizeof matrix[0] / sizeof(int); 
  
    std::cout << "rows: " << rows << "\n";
    std::cout << "cols: " << cols << "\n";
    std::cout << "total cells: " << rows * cols << "\n";
}
  
  
  
  
/*
run:
    
rows: 3
cols: 4
total cells: 12
    
*/

 



answered Nov 19, 2022 by avibootz
edited Oct 1, 2025 by avibootz
...