In: Computer Science
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
Code
//Function name isUpperMatrix that recive 2d array as pointer and the sizes that is number of rows ans num
//columns
bool isUpperMatrix(int **mat, int row, int col)
{
if (row != col)//fist will check if row and col are
same size fo square matrix
return false;//if not then return
false
for(int i=0;i<row;i++)//then will check for next
condition if the element at first diagonal is 0 if not then return
false
for (int j = 0; j < col;
j++)
{
if (i ==
j)
{
if (mat[i][j] != 0)//if element at diagonal is
not 0 then return false
return false;
}
}
return true;//other wise return truel
}
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.