1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
int num_rows(const Matrix& m)
{
return m.size();
}
typedef vector<vector<double>> Matrix;
//Example Matrix
Matrix A =
{
{ 0, 2, 0, -2},
{ 2, 0, 1, 0, 1.6},
{ 0, 1, 0, 1, 0},
{ -2, 0, 1, 0, 0},
{2.5, 1.6, 0, 0, 0}
};
//Function with error
bool is_matrix(const Matrix& m)
{
//returns false if there are 0 rows
if (num_rows(m) < 1)
{
return false;
}
//if 2 or more rows..
else if (num_rows(m) >= 2)
{
//check the number of elements in each row
for (int i = 1; i < num_rows(m); i++)
{
//if at any point the number of elements are not equal return false
if (m[i].size() != m[i-1].size())
{
return false;
}
}
}
//only other case is where num_rows(m) == 1 in this case just return true
return true;
}
|