In this problem here I am going to solve maximum element of each row in a matrix.
Suppose there is a matrix, and find out maximum element in each row.
Examples:
Input : [10, 20, 3]
[12, 4, 94]
[6, 3, 210]
Output :
3
4
3
Input : [10, 20, 3, 21]
[16, 1, 65, 9]
[17, 57, 3, 20]
Output :
3
1
2
Then Approach: Here approach is very simple.
The idea is to run the loop for no_of_rows basis.
Check each element inside the each row and find the maximum element.
Finally, we print the element.
Now, see the below implementation:
The Maximum Element Of Each Row In A Matrix In C++
// C++ program to find maximum // element of each row in a matrix #include<bits/stdc++.h> using namespace std; const int N = 4; // Print array element void printArray(int result[], int no_of_rows) { for (int i = 0; i < no_of_rows; i++) { cout<< result[i]<<"\n"; } } // Function to get max element void maxelement(int no_of_rows, int arr[][N]) { int i = 0; // Initialize max to 0 at beginning // of finding max element of each row int max = 0; int result[no_of_rows]; while (i < no_of_rows) { for (int j = 0; j < N; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max = 0; i++; } printArray(result,no_of_rows); } // Driver code int main() { int arr[][N] = { {2, 40, 1, 80}, {17, 4, 90, 11}, {96, 34, 22, 10}, {3, 2, 7, 5} }; // Calling the function maxelement(4, arr); } // This code is contributed by Rajput-Ji
Output:
1
4
22
2
The Maximum Element Of Each Row In A Matrix In Java
// C++ program to find maximum // element of each row in a matrix #include<bits/stdc++.h> using namespace std; const int N = 4; // Print array element void printArray(int result[], int no_of_rows) { for (int i = 0; i < no_of_rows; i++) { cout<< result[i]<<"\n"; } } // Function to get max element void maxelement(int no_of_rows, int arr[][N]) { int i = 0; // Initialize max to 0 at beginning // of finding max element of each row int max = 0; int result[no_of_rows]; while (i < no_of_rows) { for (int j = 0; j < N; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max = 0; i++; } printArray(result,no_of_rows); } // Driver code int main() { int arr[][N] = { {2, 40, 1, 80}, {17, 4, 90, 11}, {96, 34, 22, 10}, {3, 2, 7, 5} }; // Calling the function maxelement(4, arr); } // This code is contributed by Rajput-Ji
Output:
1
4
22
2
The Maximum Element Of Each Row In A Matrix In Python
# Python program to find maximum # element of each row in a matrix # importing numpy import numpy # Function to get max element def maxelement(arr): # get number of rows and columns no_of_rows = len(arr) no_of_column = len(arr[0]) for i in range(no_of_rows): # Initialize max1 to 0 at beginning # of finding max element of each row max1 = 0 for j in range(no_of_column): if arr[i][j] > max1 : max1 = arr[i][j] # print maximum element of each row print(max1) # Driver Code arr = [[2, 40, 1, 80], [17, 4, 90, 11], [96, 34, 22, 10], [3, 2, 7, 5] ] # Calling the function maxelement(arr)
Output:
1
4
22
2
The Maximum Element Of Each Row In A Matrix In C#
// C# program to find maximum // element of each row in a matrix using System; class GFG { // Function to get max element public static void maxelement(int no_of_rows, int[][] arr) { int i = 0; // Initialize max to 0 at beginning // of finding max element of each row int max = 0; int[] result = new int[no_of_rows]; while (i < no_of_rows) { for (int j = 0; j < arr[i].Length; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max = 0; i++; } printArray(result); } // Print array element private static void printArray(int[] result) { for (int i = 0; i < result.Length;i++) { Console.WriteLine(result[i]); } } // Driver code public static void Main(string[] args) { int[][] arr = new int[][] { new int[] {2, 40, 1, 80}, new int[] {17, 4, 90, 11}, new int[] {96, 34, 22, 10}, new int[] {3, 2, 7, 5} }; // Calling the function maxelement(4, arr); } } // This code is contributed by Shrikant13
Output:
1
4
22
2
The Maximum Element Of Each Row In A Matrix In PHP
<?php // PHP program to find maximum // element of each row in a matrix $N = 4; // Print array element function printArray($result, $no_of_rows) { for ($i = 0; $i < $no_of_rows; $i++) { echo $result[$i]."\n"; } } // Function to get max element function maxelement($no_of_rows, $arr) { global $N; $i = 0; // Initialize max to 0 at beginning // of finding max element of each row $max = 0; $result=array_fill(0,$no_of_rows,0); while ($i < $no_of_rows) { for ($j = 0; $j < $N; $j++) { if ($arr[$i][$j] > $max) { $max = $arr[$i][$j]; } } $result[$i] = $max; $max = 0; $i++; } printArray($result,$no_of_rows); } // Driver code $arr = array(array(2, 40, 1, 80), array(17, 4, 90, 11), array(96, 34, 22, 10), array(3, 2, 7, 5)); // Calling the function maxelement(4, $arr); // This code is contributed by mits ?>
Output:
1
4
22
2
The Maximum Element Of Each Row In A Matrix In JavaScript
<script> // javascript program to find maximum // element of each row in a matrix // Function to get max element function maxelement(no_of_rows, arr) { var i = 0; // Initialize max to 0 at beginning // of finding max element of each row var max = 0; var result = Array.from({length: no_of_rows}, (_, i) => 0); while (i < no_of_rows) { for (var j = 0; j < arr[i].length; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max = 0; i++; } printArray(result); } // Prvar array element function printArray(result) { for (var i = 0; i < result.length; i++) { document.write(result[i]+"<br>"); } } // Driver code var arr = [[2, 40, 1, 80], [17, 4, 90, 11], [96, 34, 22, 10], [3, 2, 7, 5] ]; // Calling the function maxelement(4, arr); // This code is contributed by 29AjayKumar </script>
Output:
1
4
22
2