Find maximum element in each column of the matrix

Maximum elements of each row of matrix

Here given code implementation process.

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

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

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

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

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

      }
    }
    printf(" %d",result);
  }
  printf("\n");
}
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_max_element(arr);

  return 0;
}

Output

 4 8 6 7
/*
  C++ Program
  Find maximum element in each column of the matrix
*/
#include<iostream>
#define ROW 5
#define COL 4
using namespace std;

class MyMatrix {
	public:

		//Find maximum value in each column of a matrix
		void col_max_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 max element
						result = matrix[j][i];
					}
				}
				cout << " " << result;
			}
			cout << "\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_max_element(matrix);
	return 0;
}

Output

 4 8 6 7
/*
  Java Program
  Find maximum element in each column of the matrix
*/
public class MyMatrix {

  //Find maximum value in each column of a matrix
  public void col_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 < 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 max element
          result=matrix[j][i];

        }
      }
      System.out.print("  "+result);
    }
    System.out.print("\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_max_element(matrix);
  }
}

Output

 4 8 6 7
using System;

/*
  C# Program
  Find maximum element in each column of the matrix
*/

public class MyMatrix {
	//Find maximum value in each column of a matrix
	public void col_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 < 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 max element
					result = matrix[j,i];
				}
			}
			Console.Write(" " + result);
		}
		Console.Write("\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_max_element(matrix);
	}
}

Output

 4 8 6 7
<?php
/*
  Php Program
  Find maximum element in each column of the matrix
*/
class MyMatrix {
	//Find maximum value in each column of a matrix

	public 	function col_max_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
			$result = $matrix[0][$i];
			for ($j = 1; $j < $row; ++$j) {
				if ($result < $matrix[$j][$i]) {
					//Get new max element
					$result = $matrix[$j][$i];
				}
			}
			echo(" ". $result);
		}
		echo("\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_max_element($matrix);

}
main();

Output

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

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

		process.stdout.write("\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_max_element(matrix);
}

main();

Output

 4 8 6 7
# Python 3 Program
# Find maximum element in each column of the matrix
class MyMatrix :
	# Find maximum value in each column of a matrix
	def col_max_element(self, matrix) :
		result = 0
		# Get the size of matrix
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		j = 0
		while (i < col) :
			# Get first element of i col
			result = matrix[0][i]
			j = 1
			while (j < row) :
				if (result < matrix[j][i]) :
					# Get new max element
					result = matrix[j][i]
				
				j += 1
			
			print(" ", result, end = "")
			i += 1
		
		print("\n", end = "")
	

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_max_element(matrix)


if __name__ == "__main__":
	main()

Output

  4  8  6  7
# Ruby Program
# Find maximum element in each column of the matrix
class MyMatrix 
	 # Find maximum value in each column of a matrix
	def col_max_element(matrix) 
		result = 0
		 # Get the size of matrix
		row = matrix.length
		col = matrix[0].length
		i = 0
		j = 0
		while (i < col) 
			 # Get first element of i col
			result = matrix[0][i]
			j = 1
			while (j < row) 
				if (result < matrix[j][i]) 
					 # Get new max element
					result = matrix[j][i]
				end
				j += 1
			end
			print(" ", result)
			i += 1
		end
		print("\n")
	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_max_element(matrix)
end
main()

Output

 4 8 6 7
/*
  Scala Program
  Find maximum element in each column of the matrix
*/
class MyMatrix {
	//Find maximum value in each column of a matrix
	def col_max_element(matrix: Array[Array[Int]]): Unit = {
		var result: Int = 0;

		//Get the size of matrix
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var i: Int = 0;
		var j: Int = 0;
		while (i < col) {
			//Get first element of i col
			result = matrix(0)(i);
			j = 1;
			while (j < row) {
				if (result < matrix(j)(i)) {
					//Get new max element
					result = matrix(j)(i);
				}
				j += 1;
			}
			print(" " + result);
			i += 1;
		}
		print("\n");
	}
}
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_max_element(matrix);
	}
}

Output

 4 8 6 7
/*
  Swift Program
  Find maximum element in each column of the matrix
*/
class MyMatrix {
	//Find maximum value in each column of a matrix
	func col_max_element(_ matrix: [
		[Int]
	]) {
		var result: Int = 0;
		//Get the size of matrix
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		var j: Int = 0;
		while (i < col) {
			//Get first element of i col
			result = matrix[0][i];
			j = 1;
			while (j < row) {
				if (result < matrix[j][i]) {
					//Get new max element
					result = matrix[j][i];
				}
				j += 1;
			}
			print(" ", result, terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
}
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_max_element(matrix);
}
main();

Output

  4  8  6  7


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