Posted on by Kalkicode
Code Matrix

# Check if the matrix is a binary matrix

The problem involves checking whether a given matrix is a binary matrix or not. A binary matrix is a matrix in which each element is either 0 or 1. The task is to determine if all elements in the matrix satisfy this condition.

## Example

Consider the following matrices:

Matrix 1:

``````1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1``````

Matrix 2:

``````1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1``````

In Matrix 1, all elements are either 0 or 1, making it a binary matrix. In Matrix 2, there is an element with a value of 3, which violates the binary matrix condition.

## Idea to Solve the Problem

To determine if a matrix is binary, we need to check each element in the matrix. If any element is not 0 or 1, then the matrix is not binary. We can achieve this by iterating through each element of the matrix and checking if its value is within the range [0, 1].

## Algorithm

1. Define a function `print_matrix` that takes the number of rows `row`, number of columns `col`, and the matrix `matrix[row][col]` as parameters. This function will display the matrix elements.
2. Define a function `is_binary_matrix` that takes the number of rows `row`, number of columns `col`, and the matrix `matrix[row][col]` as parameters. This function will check if the given matrix is binary. a. Call the `print_matrix` function to display the matrix. b. Initialize a variable `ans` as 1, assuming the matrix is binary. c. Use nested loops to iterate through each element of the matrix.
• If any element is not in the range [0, 1], set `ans` to 0. d. If `ans` is still 1, print "Is a Binary Matrix"; otherwise, print "Is Not a Binary Matrix."
3. In the `main` function: a. Define two test matrices, `matrix1` and `matrix2`, with appropriate elements. b. Get the size of each matrix using `sizeof` and calculate the number of rows and columns. c. Call the `is_binary_matrix` function for each test matrix.

## Pseudocode

``````print_matrix(row, col, matrix):
loop through rows i from 0 to row-1:
loop through columns j from 0 to col-1:
print matrix[i][j]
print a newline

is_binary_matrix(row, col, matrix):
call print_matrix with row, col, and matrix
initialize ans as 1
loop through rows i from 0 to row-1:
loop through columns j from 0 to col-1:
if matrix[i][j] is not in [0, 1]:
set ans to 0
if ans is 1:
print "Is a Binary Matrix\n\n"
else:
print "Is Not a Binary Matrix\n\n"

main:
matrix1 = ...  // Define the first test matrix
row = calculate number of rows in matrix1
col = calculate number of columns in matrix1
call is_binary_matrix with row, col, and matrix1

matrix2 = ...  // Define the second test matrix
row = calculate number of rows in matrix2
col = calculate number of columns in matrix2
call is_binary_matrix with row, col, and matrix2``````

## Code Solution

``````// C Program
// Check if the matrix is a binary matrix
#include <stdio.h>

//Displaying of the matrix elements
void print_matrix(int row, int col, int matrix[row][col])
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
printf("  %d", matrix[i][j]);
}
printf("\n");
}
}
// Determine that given matrix is binary matrix or not
void is_binary_matrix(int row, int col, int matrix[row][col])
{
print_matrix(row, col, matrix);

int ans = 1;

// Outer loop execute rows
for (int i = 0; i < row && ans == 1; ++i)
{
// Outer loop executes column
for (int j = 0; j < col && ans == 1; ++j)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
// When element is  not binary value
ans = 0;
}
}
}
if (ans == 1)
{
printf("  Is a Binary Matrix\n\n");
}
else
{
printf("  Is Not a Binary Matrix\n\n");
}
}
int main()
{

int matrix1[][4] =
{
{ 1 , 1 , 0 , 1 } ,
{ 0 , 0 , 0 , 1 } ,
{ 1 , 1 , 1 , 1 } ,
{ 1 , 1 , 0 , 1 } ,
{ 0 , 1 , 1 , 1 }
};
// Get the size
int row = sizeof(matrix1) / sizeof(matrix1[0]);
int col = sizeof(matrix1[0]) / sizeof(matrix1[0][0]);

// Test case 1
is_binary_matrix(row, col, matrix1);

int matrix2[][6] =
{
{ 1 , 1 , 0 , 1 , 1 , 1 } ,
{ 1 , 1 , 0 , 1 , 1 , 1 } ,
{ 1 , 1 , 3 , 1 , 1 , 1 }
};
// Get the size
row = sizeof(matrix2) / sizeof(matrix2[0]);
col = sizeof(matrix2[0]) / sizeof(matrix2[0][0]);
// Test case 2
is_binary_matrix(row, col, matrix2);
return 0;
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````
``````/*
Java program
Check if the matrix is a binary matrix
*/

public class MyMatrix
{
//Displaying of the matrix elements
public void print_matrix(int row, int col, int matrix[][])
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
System.out.print("  " + matrix[i][j] );
}
System.out.print("\n");
}
}
// Determine that given matrix is binary matrix or not
public void is_binary_matrix(int row, int col, int matrix[][])
{
print_matrix(row, col, matrix);
boolean ans = true;
// Outer loop execute rows
for (int i = 0; i < row && ans == true; ++i)
{
// Outer loop executes column
for (int j = 0; j < col && ans == true; ++j)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
// When element is  not binary value
ans = false;
}
}
}
if (ans == true)
{
System.out.print("  Is a Binary Matrix\n\n");
}
else
{
System.out.print("  Is Not a Binary Matrix\n\n");
}
}
public static void main(String[] args)
{
MyMatrix mat = new MyMatrix();
int[][] matrix1 = {
{
1 , 1 , 0 , 1
} ,
{
0 , 0 , 0 , 1
} ,
{
1 , 1 , 1 , 1
} ,
{
1 , 1 , 0 , 1
} ,
{
0 , 1 , 1 , 1
}
};
// Get the size
int row = matrix1.length;
int col = matrix1[0].length;
// Test case 1
mat.is_binary_matrix(row, col, matrix1);
int[][] matrix2 =
{
{
1 , 1 , 0 , 1 , 1 , 1
} ,
{
1 , 1 , 0 , 1 , 1 , 1
} ,
{
1 , 1 , 3 , 1 , 1 , 1
}
};

// Get the size
row = matrix2.length;
col = matrix2[0].length;
// Test case 2
mat.is_binary_matrix(row, col, matrix2);
}
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````
``````// Include header file
#include <iostream>
#define R 5
#define C 4
using namespace std;
/*
C++ program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
public:
// Displaying of the matrix elements
void print_matrix(int row, int col, int matrix[R][C])
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cout << "  " << matrix[i][j];
}
cout << "\n";
}
}
//  Determine that given matrix is binary matrix or not
void is_binary_matrix(int row, int col, int matrix[R][C])
{
this->print_matrix(row, col, matrix);
bool ans = true;
//  Outer loop execute rows
for (int i = 0; i < row && ans == true; ++i)
{
//  Outer loop executes column
for (int j = 0; j < col && ans == true; ++j)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
//  When element is  not binary value
ans = false;
}
}
}
if (ans == true)
{
cout << "  Is a Binary Matrix\n\n";
}
else
{
cout << "  Is Not a Binary Matrix\n\n";
}
}
};
int main()
{
MyMatrix mat = MyMatrix();
int matrix1[R][C] = {
{
1 , 1 , 0 , 1
} , {
0 , 0 , 0 , 1
} , {
1 , 1 , 1 , 1
} , {
1 , 1 , 0 , 1
} , {
0 , 1 , 1 , 1
}
};
//  Get the size
int row = sizeof(matrix1) / sizeof(matrix1[0]);
int col = sizeof(matrix1[0]) / sizeof(matrix1[0][0]);
//  Test case 1
mat.is_binary_matrix(R, C, matrix1);

return 0;
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix
``````
``````// Include namespace system
using System;
/*
C# program
Check if the matrix is a binary matrix
*/
public class MyMatrix
{
// Displaying of the matrix elements
public void print_matrix(int row, int col, int[,] matrix)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
Console.Write("  " + matrix[i,j]);
}
Console.Write("\n");
}
}
//  Determine that given matrix is binary matrix or not
public void is_binary_matrix(int row, int col, int[,] matrix)
{
print_matrix(row, col, matrix);
Boolean ans = true;
//  Outer loop execute rows
for (int i = 0; i < row && ans == true; ++i)
{
//  Outer loop executes column
for (int j = 0; j < col && ans == true; ++j)
{
if (!(matrix[i,j] >= 0 && matrix[i,j] <= 1))
{
//  When element is  not binary value
ans = false;
}
}
}
if (ans == true)
{
Console.Write("  Is a Binary Matrix\n\n");
}
else
{
Console.Write("  Is Not a Binary Matrix\n\n");
}
}
public static void Main(String[] args)
{
MyMatrix mat = new MyMatrix();
int[,] matrix1 = {
{
1 , 1 , 0 , 1
} , {
0 , 0 , 0 , 1
} , {
1 , 1 , 1 , 1
} , {
1 , 1 , 0 , 1
} , {
0 , 1 , 1 , 1
}
};
//  Get the size
int row = matrix1.GetLength(0);
int col = matrix1.GetLength(1);
//  Test case 1
mat.is_binary_matrix(row, col, matrix1);
int[,] matrix2 = {
{
1 , 1 , 0 , 1 , 1 , 1
} , {
1 , 1 , 0 , 1 , 1 , 1
} , {
1 , 1 , 3 , 1 , 1 , 1
}
};
//  Get the size
row = matrix2.GetLength(0);
col = matrix2.GetLength(1);
//  Test case 2
mat.is_binary_matrix(row, col, matrix2);
}
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````
``````<?php
/*
Php program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
// Displaying of the matrix elements
public	function print_matrix(\$row, \$col, & \$matrix)
{
for (\$i = 0; \$i < \$row; ++\$i)
{
for (\$j = 0; \$j < \$col; ++\$j)
{
echo "  ". \$matrix[\$i][\$j];
}
echo "\n";
}
}
//  Determine that given matrix is binary matrix or not
public	function is_binary_matrix(\$row, \$col, & \$matrix)
{
\$this->print_matrix(\$row, \$col, \$matrix);
\$ans = true;
//  Outer loop execute rows
for (\$i = 0; \$i < \$row && \$ans == true; ++\$i)
{
//  Outer loop executes column
for (\$j = 0; \$j < \$col && \$ans == true; ++\$j)
{
if (!(\$matrix[\$i][\$j] >= 0 && \$matrix[\$i][\$j] <= 1))
{
//  When element is  not binary value
\$ans = false;
}
}
}
if (\$ans == true)
{
echo "  Is a Binary Matrix\n\n";
}
else
{
echo "  Is Not a Binary Matrix\n\n";
}
}
}

function main()
{
\$mat = new MyMatrix();
\$matrix1 = array(array(1, 1, 0, 1), array(0, 0, 0, 1), array(1, 1, 1, 1), array(1, 1, 0, 1), array(0, 1, 1, 1));
//  Get the size
\$row = count(\$matrix1);
\$col = count(\$matrix1[0]);
//  Test case 1
\$mat->is_binary_matrix(\$row, \$col, \$matrix1);
\$matrix2 = array(array(1, 1, 0, 1, 1, 1), array(1, 1, 0, 1, 1, 1), array(1, 1, 3, 1, 1, 1));
//  Get the size
\$row = count(\$matrix2);
\$col = count(\$matrix1[0]);
//  Test case 2
\$mat->is_binary_matrix(\$row, \$col, \$matrix2);
}
main();``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1
1  1  0  1
1  1  3  1
Is Not a Binary Matrix
``````
``````/*
Node Js program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
// Displaying of the matrix elements
print_matrix(row, col, matrix)
{
for (var i = 0; i < row; ++i)
{
for (var j = 0; j < col; ++j)
{
process.stdout.write("  " + matrix[i][j]);
}
process.stdout.write("\n");
}
}
//  Determine that given matrix is binary matrix or not
is_binary_matrix(row, col, matrix)
{
this.print_matrix(row, col, matrix);
var ans = true;
//  Outer loop execute rows
for (var i = 0; i < row && ans == true; ++i)
{
//  Outer loop executes column
for (var j = 0; j < col && ans == true; ++j)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
//  When element is  not binary value
ans = false;
}
}
}
if (ans == true)
{
process.stdout.write("  Is a Binary Matrix\n\n");
}
else
{
process.stdout.write("  Is Not a Binary Matrix\n\n");
}
}
}

function main()
{
var mat = new MyMatrix();
var matrix1 = [
[1, 1, 0, 1] ,
[0, 0, 0, 1] ,
[1, 1, 1, 1] ,
[1, 1, 0, 1] ,
[0, 1, 1, 1]
];
//  Get the size
var row = matrix1.length;
var col = matrix1[0].length;
//  Test case 1
mat.is_binary_matrix(row, col, matrix1);
var matrix2 = [
[1, 1, 0, 1, 1, 1] ,
[1, 1, 0, 1, 1, 1] ,
[1, 1, 3, 1, 1, 1]
];
//  Get the size
row = matrix2.length;
col = matrix2[0].length;
//  Test case 2
mat.is_binary_matrix(row, col, matrix2);
}
main();``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````
``````#  Python 3 program
#  Check if the matrix is a binary matrix

class MyMatrix :
#  Displaying of the matrix elements
def print_matrix(self, row, col, matrix) :
i = 0
j = 0
while (i < row) :
j = 0
while (j < col) :
print("  ", matrix[i][j], end = "")
j += 1

print("\n", end = "")
i += 1

#   Determine that given matrix is binary matrix or not
def is_binary_matrix(self, row, col, matrix) :
self.print_matrix(row, col, matrix)
ans = True
#   Outer loop execute rows
i = 0
j = 0
while (i < row and ans == True) :
j = 0
#   Outer loop executes column
while (j < col and ans == True) :
if (not(matrix[i][j] >= 0 and matrix[i][j] <= 1)) :
#   When element is  not binary value
ans = False

j += 1

i += 1

if (ans == True) :
print("  Is a Binary Matrix\n\n", end = "")
else :
print("  Is Not a Binary Matrix\n\n", end = "")

def main() :
mat = MyMatrix()
matrix1 = [
[1, 1, 0, 1] ,
[0, 0, 0, 1] ,
[1, 1, 1, 1] ,
[1, 1, 0, 1] ,
[0, 1, 1, 1]
]
#   Get the size
row = len(matrix1)
col = len(matrix1[0])
#   Test case 1
mat.is_binary_matrix(row, col, matrix1)
matrix2 = [
[1, 1, 0, 1, 1, 1] ,
[1, 1, 0, 1, 1, 1] ,
[1, 1, 3, 1, 1, 1]
]
#   Get the size
row = len(matrix2)
col = len(matrix1[0])
#   Test case 2
mat.is_binary_matrix(row, col, matrix2)

if __name__ == "__main__": main()``````

#### Output

``````   1   1   0   1
0   0   0   1
1   1   1   1
1   1   0   1
0   1   1   1
Is a Binary Matrix

1   1   0   1
1   1   0   1
1   1   3   1
Is Not a Binary Matrix
``````
``````#  Ruby program
#  Check if the matrix is a binary matrix

class MyMatrix
#  Displaying of the matrix elements
def print_matrix(row, col, matrix)
i = 0
j = 0
while (i < row)
j = 0
while (j < col)
print("  ", matrix[i][j])
j += 1
end

print("\n")
i += 1
end

end

#   Determine that given matrix is binary matrix or not
def is_binary_matrix(row, col, matrix)
self.print_matrix(row, col, matrix)
ans = true
#   Outer loop execute rows
i = 0
j = 0
while (i < row && ans == true)
j = 0
#   Outer loop executes column
while (j < col && ans == true)
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
#   When element is  not binary value
ans = false
end

j += 1
end

i += 1
end

if (ans == true)
print("  Is a Binary Matrix\n\n")
else
print("  Is Not a Binary Matrix\n\n")
end

end

end

def main()
mat = MyMatrix.new()
matrix1 = [
[1, 1, 0, 1] ,
[0, 0, 0, 1] ,
[1, 1, 1, 1] ,
[1, 1, 0, 1] ,
[0, 1, 1, 1]
]
#   Get the size
row = matrix1.length
col = matrix1[0].length
#   Test case 1
mat.is_binary_matrix(row, col, matrix1)
matrix2 = [
[1, 1, 0, 1, 1, 1] ,
[1, 1, 0, 1, 1, 1] ,
[1, 1, 3, 1, 1, 1]
]
#   Get the size
row = matrix2.length
col = matrix2[0].length
#   Test case 2
mat.is_binary_matrix(row, col, matrix2)
end

main()``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix

``````
``````/*
Scala program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
//  Displaying of the matrix elements
def print_matrix(row: Int, col: Int, matrix: Array[Array[Int]]): Unit = {
var i: Int = 0;
var j: Int = 0;
while (i < row)
{
j = 0;
while (j < col)
{
print("  " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
}
//   Determine that given matrix is binary matrix or not
def is_binary_matrix(row: Int, col: Int, matrix: Array[Array[Int]]): Unit = {
this.print_matrix(row, col, matrix);
var ans: Boolean = true;
//   Outer loop execute rows
var i: Int = 0;
var j: Int = 0;
while (i < row && ans == true)
{
j = 0;
//   Outer loop executes column
while (j < col && ans == true)
{
if (!(matrix(i)(j) >= 0 && matrix(i)(j) <= 1))
{
//   When element is  not binary value
ans = false;
}
j += 1;
}
i += 1;
}
if (ans == true)
{
print("  Is a Binary Matrix\n\n");
}
else
{
print("  Is Not a Binary Matrix\n\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var mat: MyMatrix = new MyMatrix();
var matrix1: Array[Array[Int]] = Array(Array(1, 1, 0, 1), Array(0, 0, 0, 1), Array(1, 1, 1, 1), Array(1, 1, 0, 1), Array(0, 1, 1, 1));
//   Get the size
var row: Int = matrix1.length;
var col: Int = matrix1(0).length;
//   Test case 1
mat.is_binary_matrix(row, col, matrix1);
var matrix2: Array[Array[Int]] = Array(Array(1, 1, 0, 1, 1, 1), Array(1, 1, 0, 1, 1, 1), Array(1, 1, 3, 1, 1, 1));
//   Get the size
row = matrix2.length;
col = matrix2(0).length;
//   Test case 2
mat.is_binary_matrix(row, col, matrix2);
}
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````
``````/*
Swift 4 program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
//  Displaying of the matrix elements
func print_matrix(_ row: Int, _ col: Int, _ matrix: [
[Int]
])
{
var i: Int = 0;
var j: Int = 0;
while (i < row)
{
j = 0;
while (j < col)
{
print("  ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
//   Determine that given matrix is binary matrix or not
func is_binary_matrix(_ row: Int, _ col: Int, _ matrix: [
[Int]
])
{
self.print_matrix(row, col, matrix);
var ans: Bool = true;
//   Outer loop execute rows
var i: Int = 0;
var j: Int = 0;
while (i < row && ans == true)
{
j = 0;
//   Outer loop executes column
while (j < col && ans == true)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
//   When element is  not binary value
ans = false;
}
j += 1;
}
i += 1;
}
if (ans == true)
{
print("  Is a Binary Matrix\n\n", terminator: "");
}
else
{
print("  Is Not a Binary Matrix\n\n", terminator: "");
}
}
}
func main()
{
let mat: MyMatrix = MyMatrix();
let matrix1: [[Int]] = [
[1, 1, 0, 1] ,
[0, 0, 0, 1] ,
[1, 1, 1, 1] ,
[1, 1, 0, 1] ,
[0, 1, 1, 1]
];
//   Get the size
var row: Int = matrix1.count;
var col: Int = matrix1[0].count;
//   Test case 1
mat.is_binary_matrix(row, col, matrix1);
let matrix2: [[Int]] = [
[1, 1, 0, 1, 1, 1] ,
[1, 1, 0, 1, 1, 1] ,
[1, 1, 3, 1, 1, 1]
];
//   Get the size
row = matrix2.count;
col = matrix2[0].count;
//   Test case 2
mat.is_binary_matrix(row, col, matrix2);
}
main();``````

#### Output

``````   1   1   0   1
0   0   0   1
1   1   1   1
1   1   0   1
0   1   1   1
Is a Binary Matrix

1   1   0   1   1   1
1   1   0   1   1   1
1   1   3   1   1   1
Is Not a Binary Matrix
``````
``````/*
Kotlin program
Check if the matrix is a binary matrix
*/
class MyMatrix
{
//  Displaying of the matrix elements
fun print_matrix(row: Int, col: Int, matrix: Array<Array<Int>>): Unit
{
var i: Int = 0;
var j: Int = 0;
while (i < row)
{

while (j < col)
{
print("  " + matrix[i][j]);
j += 1;
}
j = 0;
print("\n");
i += 1;
}
}
//   Determine that given matrix is binary matrix or not
fun is_binary_matrix(row: Int, col: Int, matrix: Array <Array<Int>> ): Unit
{
this.print_matrix(row, col, matrix);
var ans: Boolean = true;
//   Outer loop execute rows
var i: Int = 0;
var j: Int = 0;
while (i < row && ans == true)
{

//   Outer loop executes column
while (j < col && ans == true)
{
if (!(matrix[i][j] >= 0 && matrix[i][j] <= 1))
{
//   When element is  not binary value
ans = false;
}
j += 1;
}
j = 0;
i += 1;
}
if (ans == true)
{
print("  Is a Binary Matrix\n\n");
}
else
{
print("  Is Not a Binary Matrix\n\n");
}
}
}
fun main(args: Array <String> ): Unit
{
var mat: MyMatrix = MyMatrix();
var matrix1: Array<Array<Int>> = arrayOf(
arrayOf(1, 1, 0, 1),
arrayOf(0, 0, 0, 1),
arrayOf(1, 1, 1, 1),
arrayOf(1, 1, 0, 1),
arrayOf(0, 1, 1, 1));
//   Get the size
var row: Int = matrix1.count();
var col: Int = matrix1[0].count();
//   Test case 1
mat.is_binary_matrix(row, col, matrix1);
var matrix2: Array<Array<Int>> = arrayOf(
arrayOf(1, 1, 0, 1, 1, 1),
arrayOf(1, 1, 0, 1, 1, 1),
arrayOf(1, 1, 3, 1, 1, 1));
//   Get the size
row = matrix2.count();
col = matrix2[0].count();
//   Test case 2
mat.is_binary_matrix(row, col, matrix2);
}``````

#### Output

``````  1  1  0  1
0  0  0  1
1  1  1  1
1  1  0  1
0  1  1  1
Is a Binary Matrix

1  1  0  1  1  1
1  1  0  1  1  1
1  1  3  1  1  1
Is Not a Binary Matrix
``````

## Output Explanation

The mentioned C code implements the above algorithm to check if a given matrix is binary or not. It iterates through each element of the matrix and checks if its value is within the range [0, 1]. The output demonstrates whether each test matrix is a binary matrix or not.

## Time Complexity

The time complexity of the algorithm depends on the number of elements in the matrix. Therefore, the time complexity is O(row * col), where row is the number of rows and col is the number of columns in the matrix. This is because the algorithm needs to iterate through all elements in the matrix to determine if it is a binary matrix.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post