Find minimum element in each column of the matrix

Find minimum element in each column

Here given code implementation process.

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

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

//Find minimum value in each column of a matrix
void col_min_element(int arr[][COL])
{
  int result=0;

  for (int i = 0; i < COL; ++i)
  {
    //Get first element of i col
    result=arr[0][i];

    for (int j = 1; j < ROW; ++j)
    {
      if(result > arr[j][i])
      {
        //Get new min element
        result=arr[j][i];

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


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

  return 0;
}

Output

 0
 -1
 2
 0
/*
  C++ Program
  Find minimum element in each column of the matrix
*/
#include<iostream>
#define ROW 5
#define COL 4

using namespace std;

class MyMatrix {
	public:

    //Find minimum value in each column of a matrix
    void col_min_element(int matrix[][COL]) {
      int result = 0;
     
      for (int i = 0; i < COL; ++i) {
        //Get first element of i col
        result = matrix[0][i];
        for (int j = 1; j < ROW; ++j) {
          if (result > matrix[j][i]) {
            //Get new min element
            result = matrix[j][i];
          }
        }
        cout << " " << result << "\n";
      }
    }
};
int main() {
	MyMatrix obj = MyMatrix();
	int matrix[][COL] = {
		{
			1,
			-1,
			6,
			3
		},
		{
			4,
			8,
			5,
			4
		},
		{
			3,
			4,
			5,
			0
		},
		{
			2,
			3,
			2,
			3
		},
		{
			0,
			3,
			5,
			7
		}
	};
	obj.col_min_element(matrix);
	return 0;
}

Output

 0
 -1
 2
 0
/*
  Java Program
  Find minimum element in each column of the matrix
*/
public class MyMatrix {

  //Find minimum value in each column of a matrix
  public void col_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 < col; ++i)
    {
      //Get first element of i col and first row
      result=matrix[0][i];

      for (int j = 1; j < row; ++j)
      {
        if(result > matrix[j][i])
        {
          //Get new min element
          result=matrix[j][i];

        }
      }
      System.out.print("  "+result+"\n");
    }
  }
  public static void main(String[] args) {
    MyMatrix obj = new MyMatrix();
    //Define matrix 
    int [][]matrix = { 
      {1, -1, 6, 3},  
      {4, 8, 5, 4},  
      {3, 4, 5, 0},  
      {2, 3, 2, 3},
      {0, 3, 5, 7}
    };  
    obj.col_min_element(matrix);
  }
}

Output

 0
 -1
 2
 0
/*
  C# Program
  Find minimum element in each column of the matrix
*/
using System;

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

Output

 0
 -1
 2
 0
<?php
/*
  Php Program
  Find minimum element in each column of the matrix
*/
class MyMatrix {
	//Find minimum value in each column of a matrix
	public 	function col_min_element($matrix) {
		$result = 0;
		//Get the size of array
		$row = count($matrix);
		$col = count($matrix[0]);
		for ($i = 0; $i < $col; ++$i) {
			//Get first element of i col and first row
			$result = $matrix[0][$i];
			for ($j = 1; $j < $row; ++$j) {
				if ($result > $matrix[$j][$i]) {
					//Get new min element
					$result = $matrix[$j][$i];
				}
			}
			echo(" ". $result ."\n");
		}
	}
}

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

}
main();

Output

 0
 -1
 2
 0
/*
  Node Js Program
  Find minimum element in each column of the matrix
*/
class MyMatrix {
	//Find minimum value in each column of a matrix
	col_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 < col; ++i) {
			//Get first element of i col and first row
			result = matrix[0][i];
			for (var j = 1; j < row; ++j) {
				if (result > matrix[j][i]) {
					//Get new min element
					result = matrix[j][i];
				}
			}

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

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

main();

Output

 0
 -1
 2
 0
# Python 3 Program
# Find minimum element in each column of the matrix
class MyMatrix :
	# Find minimum value in each column of a matrix
	def col_min_element(self, matrix) :
		result = 0
		# Get the size
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		while (i < col) :
			# Get first element of i col and first row
			result = matrix[0][i]
			j = 1
			while (j < row) :
				if (result > matrix[j][i]) :
					# Get new min element
					result = matrix[j][i]
				
				j += 1
			
			print(" ", result ,"\n", end = "")
			i += 1
		
	

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


if __name__ == "__main__":
	main()

Output

  0
  -1
  2
  0
# Ruby Program
# Find minimum element in each column of the matrix
class MyMatrix 
	 # Find minimum value in each column of a matrix
	def col_min_element(matrix) 
		result = 0
		 # Get the size
		row = matrix.length
		col = matrix[0].length
		i = 0
		while (i < col) 
			 # Get first element of i col and first row
			result = matrix[0][i]
			j = 1
			while (j < row) 
				if (result > matrix[j][i]) 
					 # Get new min element
					result = matrix[j][i]
				end
				j += 1
			end
			print(" ", result ,"\n")
			i += 1
		end
	end
end
def main() 
	obj = MyMatrix.new()
	matrix = [
		[1, -1, 6, 3],
		[4, 8, 5, 4],
		[3, 4, 5, 0],
		[2, 3, 2, 3],
		[0, 3, 5, 7]
	]
	obj.col_min_element(matrix)
end
main()

Output

 0
 -1
 2
 0
/*
  Scala Program
  Find minimum element in each column of the matrix
*/
class MyMatrix {
	//Find minimum value in each column of a matrix
	def col_min_element(matrix: Array[Array[Int]]): Unit = {
		var result: Int = 0;
		//Get the size
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var i: Int = 0;
		while (i < col) {
			//Get first element of i col and first row
			result = matrix(0)(i);
			var j: Int = 1;
			while (j < row) {
				if (result > matrix(j)(i)) {
					//Get new min element
					result = matrix(j)(i);
				}
				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, -1, 6, 3),
			Array(4, 8, 5, 4),
			Array(3, 4, 5, 0),
			Array(2, 3, 2, 3),
			Array(0, 3, 5, 7));
		obj.col_min_element(matrix);
	}
}

Output

 0
 -1
 2
 0
/*
  Swift Program
  Find minimum element in each column of the matrix
*/
class MyMatrix {
	//Find minimum value in each column of a matrix
	func col_min_element(_ matrix: [
		[Int]
	]) {
		var result: Int = 0;
		//Get the size
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		while (i < col) {
			//Get first element of i col and first row
			result = matrix[0][i];
			var j: Int = 1;
			while (j < row) {
				if (result > matrix[j][i]) {
					//Get new min element
					result = matrix[j][i];
				}
				j += 1;
			}
			print(" ", result ,"\n", terminator: "");
			i += 1;
		}
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	let matrix: [
		[Int]
	] = [
		[1, -1, 6, 3],
		[4, 8, 5, 4],
		[3, 4, 5, 0],
		[2, 3, 2, 3],
		[0, 3, 5, 7]
	];
	obj.col_min_element(matrix);
}
main();

Output

  0
  -1
  2
  0

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