Find minimum element of each row in a matrix

Find minimum element in matrix rows

Here given code implementation process.

//C Program 
//Find minimum element of each row in a matrix
#include<stdio.h>

//Matrix size
#define ROW 5
#define COL 4

//Find minimum value in each row of a matrix
void row_min_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 min element
        result=arr[i][j];

      }
    }
    printf(" %d\n",result);
  }
}
int main()
{


  int arr[][COL]= { 
    {1, -1, 2, 3},  
    {4, 8, 1, 4},  
    {3, 4, 2, 0},  
    {2, 5, 3, 3},
    {6, 3, 5, 7}
  };  
  
  row_min_element(arr);

  return 0;
}

Output

 -1
 1
 0
 2
 3
#include<iostream>
#define ROW 5
#define COL 4
using namespace std;

/*
  C++ Program
  Find minimum element of each row in a matrix
*/
class MyMatrix {
	public:

		//Find minimum value in each row of a matrix
		void row_min_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 min element
						result = matrix[i][j];
					}
				}
				cout << " " << result << "\n";
			}
		}
};
int main() {
	MyMatrix obj = MyMatrix();
	int matrix[ROW][COL] = {
		{
			1,
			-1,
			2,
			3
		},
		{
			4,
			8,
			1,
			4
		},
		{
			3,
			4,
			2,
			0
		},
		{
			2,
			5,
			3,
			3
		},
		{
			6,
			3,
			5,
			7
		}
	};
	obj.row_min_element(matrix);
	return 0;
}

Output

 -1
 1
 0
 2
 3
/*
  Java Program
  Find minimum element of each row in a matrix
*/
public class MyMatrix {

  //Find minimum value in each row of a matrix
  public void row_min_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 min 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, -1, 2, 3},  
      {4, 8, 1, 4},  
      {3, 4, 2, 0},  
      {2, 5, 3, 3},
      {6, 3, 5, 7}
    };  
    obj.row_min_element(matrix);
  }
}

Output

 -1
 1
 0
 2
 3
/*
  C# Program
  Find minimum element of each row in a matrix
*/

using System;

public class MyMatrix {
	//Find minimum value in each row of a matrix
	public void row_min_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 min element
					result = matrix[i,j];
				}
			}
			Console.Write(" " + result + "\n");
		}
	}
	public static void Main(String[] args) {
		MyMatrix obj = new MyMatrix();
		//Define matrix 	
      	int[,] matrix = {
			{
				1,
				-1,
				2,
				3
			},
			{
				4,
				8,
				1,
				4
			},
			{
				3,
				4,
				2,
				0
			},
			{
				2,
				5,
				3,
				3
			},
			{
				6,
				3,
				5,
				7
			}
		};
		obj.row_min_element(matrix);
	}
}

Output

 -1
 1
 0
 2
 3
<?php
/*
  Php Program
  Find minimum element of each row in a matrix
*/
class MyMatrix {
	//Find minimum value in each row of a matrix

	public 	function row_min_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 min element
					$result = $matrix[$i][$j];
				}
			}
			echo(" ". $result ."\n");
		}
	}
}

function main() {
	$obj = new MyMatrix();
	//Define matrix 
	$matrix = array(
      array(1, -1, 2, 3), 
      array(4, 8, 1, 4), 
      array(3, 4, 2, 0), 
      array(2, 5, 3, 3), 
      array(6, 3, 5, 7)
    );
	$obj->row_min_element($matrix);

}
main();

Output

 -1
 1
 0
 2
 3
/*
  Node Js Program
  Find minimum element of each row in a matrix
*/
class MyMatrix {
	//Find minimum value in each row of a matrix
	row_min_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 min element
					result = matrix[i][j];
				}
			}

			process.stdout.write(" " + result + "\n");
		}
	}
}

function main(args) {
	var obj = new MyMatrix();
	//Define matrix 
	var matrix = [
		[1, -1, 2, 3],
		[4, 8, 1, 4],
		[3, 4, 2, 0],
		[2, 5, 3, 3],
		[6, 3, 5, 7]
	];
	obj.row_min_element(matrix);
}

main();

Output

 -1
 1
 0
 2
 3
#   Python 3 Program
#   Find minimum element of each row in a matrix
class MyMatrix :
	# Find minimum value in each row of a matrix
	def row_min_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 min element
					result = matrix[i][j]
				
				j += 1
			
			print(" ", result ,"\n", end = "")
			i += 1
		
	

def main() :
	obj = MyMatrix()
	# Define matrix 
	matrix = [
		[1, -1, 2, 3],
		[4, 8, 1, 4],
		[3, 4, 2, 0],
		[2, 5, 3, 3],
		[6, 3, 5, 7]
	]
	obj.row_min_element(matrix)


if __name__ == "__main__":
	main()

Output

  -1
  1
  0
  2
  3
#  Ruby Program
#  Find minimum element of each row in a matrix
class MyMatrix 
	 # Find minimum value in each row of a matrix
	def row_min_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 min element
					result = matrix[i][j]
				end
				j += 1
			end
			print(" ", result ,"\n")
			i += 1
		end
	end
end
def main() 
	obj = MyMatrix.new()
	 # Define matrix 
	matrix = [
		[1, -1, 2, 3],
		[4, 8, 1, 4],
		[3, 4, 2, 0],
		[2, 5, 3, 3],
		[6, 3, 5, 7]
	]
	obj.row_min_element(matrix)
end
main()

Output

 -1
 1
 0
 2
 3
/*
  Scala Program
  Find minimum element of each row in a matrix
*/
class MyMatrix {
	//Find minimum value in each row of a matrix
	def row_min_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 min 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();

		//Define matrix 
		val matrix: Array[Array[Int]] = Array(
			Array(1, -1, 2, 3),
			Array(4, 8, 1, 4),
			Array(3, 4, 2, 0),
			Array(2, 5, 3, 3),
			Array(6, 3, 5, 7));
		obj.row_min_element(matrix);
	}
}

Output

 -1
 1
 0
 2
 3
/*
  Swift Program
  Find minimum element of each row in a matrix
*/
class MyMatrix {
	//Find minimum value in each row of a matrix
	func row_min_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 min element
					result = matrix[i][j];
				}
				j += 1;
			}
			print(" ", result );
			i += 1;
		}
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	//Define matrix 
	let matrix: [
		[Int]
	] = [
		[1, -1, 2, 3],
		[4, 8, 1, 4],
		[3, 4, 2, 0],
		[2, 5, 3, 3],
		[6, 3, 5, 7]
	];
	obj.row_min_element(matrix);
}
main();

Output

  -1
  1
  0
  2
  3


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







© 2021, kalkicode.com, All rights reserved