Find maximum element of each row in a matrix
Here given code implementation process.
//C Program
//Find maximum element of each row in a matrix
#include<stdio.h>
//Matrix size
#define ROW 4
#define COL 4
//print max element in each row
void row_max_element(int arr[][COL])
{
int result=0;
for (int i = 0; i < ROW; ++i)
{
//Get first element of i row
result=arr[i][0];
for (int j = 1; j < COL; ++j)
{
if(result<arr[i][j])
{
//Get new max element
result=arr[i][j];
}
}
printf(" %d\n",result);
}
}
int main()
{
int arr[][COL]= {
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};
row_max_element(arr);
return 0;
}
Output
3
4
1
5
/*
C++ Program
Find maximum element of each row in a matrix
*/
#include<iostream>
#define ROW 4
#define COL 5
using namespace std;
class MyMatrix {
public:
//print max element in each row
void row_max_element(int matrix[][COL])
{
int result=0;
for (int i = 0; i < ROW; ++i)
{
//Get first element of i row
result=matrix[i][0];
for (int j = 1; j < COL; ++j)
{
if(result<matrix[i][j])
{
//Get new max element
result=matrix[i][j];
}
}
cout<<" "<<result<<endl;
}
}
};
int main() {
MyMatrix obj;
int matrix[][COL] ={
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};
obj.row_max_element(matrix);
return 0;
}
Output
3
4
1
5
/*
Java Program
Find maximum element of each row in a matrix
*/
public class MyMatrix {
//print max element in each row
public void row_max_element(int [][]matrix)
{
int result=0;
//Get the size of array
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; ++i)
{
//Get first element of i row
result=matrix[i][0];
for (int j = 1; j < col; ++j)
{
if(result<matrix[i][j])
{
//Get new max element
result=matrix[i][j];
}
}
System.out.print(" "+result+"\n");
}
}
public static void main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int [][]matrix ={
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};
obj.row_max_element(matrix);
}
}
Output
3
4
1
5
/*
C# Program
Find maximum element of each row in a matrix
*/
using System;
public class MyMatrix {
//print max element in each row
public void row_max_element(int[,] matrix) {
int result = 0;
//Get the size of array
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i = 0; i < row; ++i) {
//Get first element of i row
result = matrix[i,0];
for (int j = 1; j < col; ++j) {
if (result < matrix[i,j]) {
//Get new max element
result = matrix[i,j];
}
}
Console.Write(" " + result + "\n");
}
}
public static void Main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int[,] matrix = {
{
1,
0,
2,
3
},
{
4,
1,
1,
4
},
{
0,
-3,
1,
0
},
{
2,
5,
1,
2
}
};
obj.row_max_element(matrix);
}
}
Output
3
4
1
5
<?php
/*
Php Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
public function row_max_element($matrix) {
$result = 0;
//Get the size of array
$row = count($matrix);
$col = count($matrix[0]);
for ($i = 0; $i < $row; ++$i) {
//Get first element of i row
$result = $matrix[$i][0];
for ($j = 1; $j < $col; ++$j) {
if ($result < $matrix[$i][$j]) {
//Get new max element
$result = $matrix[$i][$j];
}
}
echo(" ". $result ."\n");
}
}
};
function main() {
$obj = new MyMatrix();
//Define matrix
$matrix = array(array(1, 0, 2, 3),
array(4, 1, 1, 4),
array(0, -3, 1, 0),
array(2, 5, 1, 2));
$obj->row_max_element($matrix);
}
main();
Output
3
4
1
5
/*
Node Js Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
row_max_element(matrix) {
var result = 0;
//Get the size of array
var row = matrix.length;
var col = matrix[0].length;
for (var i = 0; i < row; ++i) {
//Get first element of i row
result = matrix[i][0];
for (var j = 1; j < col; ++j) {
if (result < matrix[i][j]) {
//Get new max element
result = matrix[i][j];
}
}
process.stdout.write(" " + result + "\n");
}
}
}
function main(args) {
var obj = new MyMatrix();
//Define matrix
var matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
];
obj.row_max_element(matrix);
}
main();
Output
3
4
1
5
# Python 3 Program
# Find maximum element of each row in a matrix
class MyMatrix :
# print max element in each row
def row_max_element(self, matrix) :
result = 0
row = len(matrix)
col = len(matrix[0])
i = 0
while (i < row) :
# Get first element of i row
result = matrix[i][0]
j = 1
while (j < col) :
if (result < matrix[i][j]) :
# Get new max element
result = matrix[i][j]
j += 1
print(" ", result ,"\n", end = "")
i += 1
def main() :
obj = MyMatrix()
matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
]
obj.row_max_element(matrix)
if __name__ == "__main__":
main()
Output
3
4
1
5
# Ruby Program
# Find maximum element of each row in a matrix
class MyMatrix
# print max element in each row
def row_max_element(matrix)
result = 0
row = matrix.length
col = matrix[0].length
i = 0
while (i < row)
# Get first element of i row
result = matrix[i][0]
j = 1
while (j < col)
if (result < matrix[i][j])
# Get new max element
result = matrix[i][j]
end
j += 1
end
print(" ", result ,"\n")
i += 1
end
end
end
def main()
obj = MyMatrix.new()
matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
]
obj.row_max_element(matrix)
end
main()
Output
3
4
1
5
/*
Scala Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
def row_max_element(matrix: Array[Array[Int]]): Unit = {
var result: Int = 0;
val row: Int = matrix.length;
val col: Int = matrix(0).length;
var i: Int = 0;
while (i < row) {
//Get first element of i row
result = matrix(i)(0);
var j: Int = 1;
while (j < col) {
if (result < matrix(i)(j)) {
//Get new max element
result = matrix(i)(j);
}
j += 1;
}
print(" " + result + "\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyMatrix = new MyMatrix();
val matrix: Array[Array[Int]] = Array(
Array(1, 0, 2, 3),
Array(4, 1, 1, 4),
Array(0, -3, 1, 0),
Array(2, 5, 1, 2));
obj.row_max_element(matrix);
}
}
Output
3
4
1
5
/*
Swift 4 Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
func row_max_element(_ matrix: [
[Int]
]) {
var result: Int = 0;
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < row) {
//Get first element of i row
result = matrix[i][0];
var j: Int = 1;
while (j < col) {
if (result < matrix[i][j]) {
//Get new max element
result = matrix[i][j];
}
j += 1;
}
print(" ", result ,"\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
];
obj.row_max_element(matrix);
}
main();
Output
3
4
1
5
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