Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to check whether a matrix is a square matrix in C++

1 Answer

0 votes
#include <iostream>
#include <vector>

bool isSquareMatrix(const std::vector<std::vector<int>>& matrix) {
    // Get the number of rows
    int rows = matrix.size();

    // Check if the matrix is empty
    if (rows == 0) {
        return false; // An empty matrix is not a square matrix
    }

    // Get the number of columns in the first row
    int cols = matrix[0].size();

    // Check if the number of rows equals the number of columns
    return rows == cols;
}

int main() {
     std::vector<std::vector<int>> matrix = {
      {5, 8, 9, 10},
      {1, 4, 6, 13},
      {7, 3, 0, 18},
      {6, 8, 9, 20},
    };

    // Check if the matrix is a square matrix
    if (isSquareMatrix(matrix)) {
        std::cout << "The matrix is a square matrix." << std::endl;
    } else {
        std::cout << "The matrix is not a square matrix." << std::endl;
    }
}



/*
run:

The matrix is a square matrix.

*/

 



answered Oct 6, 2025 by avibootz
...