Check if the matrix is a binary matrix
Here given code implementation process.
// 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
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.
New Comment