Find the Maximum Element in a Matrix

You are given a matrix of size R*C containing integers. Write a program to find the maximum element present in the matrix.

Note: Make sure to read the input matrix correctly and find the maximum element in the matrix as specified in the question.

Boundary Condition(s):
2 <= R, C <= 50
1 <= Each integer value in the matrix <= 1000

Input Format:
The first line contains two space-separated integers R and C, representing the number of rows and columns in the matrix.
The next R lines contain C integers each, representing the elements of the matrix.

Output Format:
The program should output a single integer, which is the maximum element found in the matrix.

Example Input/Output 1:
Input:
3 4
5 10 25 8
12 9 7 6
11 14 18 22

Output:
25

Explanation:
In this example, the input specifies a matrix of size 3×4 (R = 3 rows and C = 4 columns) containing integers.
To find the maximum element in this matrix, we need to go through each element and keep track of the largest element encountered so far.

Example Input/Output 2:
Input:
4 3
30 40 5
16 28 35
55 90 15
10 25 60

Output:
50

Algorithm:

  1. Initialize a variable max_element with the value of the first element in the matrix.
  2. Iterate through each row i from 0 to R-1 and for each row: a. Iterate through each column j from 0 to C-1 and for each column:
    • If the value of the current element matrix[i][j] is greater than max_element, update max_element with matrix[i][j].
  3. After the loop completes, max_element will hold the maximum value found in the matrix.
  4. Print the value of max_element as the output.

This algorithm iterates through each element in the matrix and updates the maximum element as it encounters larger values. After processing all elements, it will have found the maximum element in the matrix.

def find_max_element(matrix):
    # Initialize max_element with the value of the first element in the matrix
    max_element = matrix[0][0]

    # Iterate through each row
    for row in matrix:
        # Iterate through each element in the row
        for num in row:
            # Update max_element if the current element is greater
            max_element = max(max_element, num)

    return max_element

if __name__ == "__main__":
    # Read the number of rows (R) and columns (C)
    R, C = map(int, input().split())

    # Read the matrix elements
    matrix = [list(map(int, input().split())) for _ in range(R)]

    # Call the function to find the maximum element in the matrix
    result = find_max_element(matrix)

    # Print the result (maximum element)
    print(result)
#include <iostream>
#include <vector>
using namespace std;

int findMaxElement(vector<vector<int>>& matrix) {
    // Initialize maxElement with the value of the first element in the matrix
    int maxElement = matrix[0][0];

    // Iterate through each row
    for (const vector<int>& row : matrix) {
        // Iterate through each element in the row
        for (int num : row) {
            // Update maxElement if the current element is greater
            maxElement = max(maxElement, num);
        }
    }

    return maxElement;
}

int main() {
    int R, C;
    cin >> R >> C;

    // Create a 2D vector to store the matrix elements
    vector<vector<int>> matrix(R, vector<int>(C));

    // Read the matrix elements
    for (int i = 0; i < R; ++i) {
        for (int j = 0; j < C; ++j) {
            cin >> matrix[i][j];
        }
    }

    // Call the function to find the maximum element in the matrix
    int result = findMaxElement(matrix);

    // Print the result (maximum element)
    cout << result << endl;

    return 0;
}
#include <stdio.h>

// Function to find the maximum element in a matrix
int findMaxElement(int matrix[][50], int R, int C) {
    // Initialize maxElement with the value of the first element in the matrix
    int maxElement = matrix[0][0];

    // Iterate through each row
    for (int i = 0; i < R; i++) {
        // Iterate through each element in the row
        for (int j = 0; j < C; j++) {
            // Update maxElement if the current element is greater
            if (matrix[i][j] > maxElement) {
                maxElement = matrix[i][j];
            }
        }
    }

    return maxElement;
}

int main() {
    int R, C;
    scanf("%d %d", &R, &C);

    // Declare a 2D array to store the matrix elements
    int matrix[50][50];

    // Read the matrix elements
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Call the function to find the maximum element in the matrix
    int result = findMaxElement(matrix, R, C);

    // Print the result (maximum element)
    printf("%dn", result);

    return 0;
}
import java.util.Scanner;

public class Main {
    public static int findMaxElement(int[][] matrix) {
        // Initialize maxElement with the value of the first element in the matrix
        int maxElement = matrix[0][0];

        // Iterate through each row
        for (int[] row : matrix) {
            // Iterate through each element in the row
            for (int num : row) {
                // Update maxElement if the current element is greater
                maxElement = Math.max(maxElement, num);
            }
        }

        return maxElement;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int R = scanner.nextInt();
        int C = scanner.nextInt();

        // Create a 2D array to store the matrix elements
        int[][] matrix = new int[R][C];

        // Read the matrix elements
        for (int i = 0; i < R; ++i) {
            for (int j = 0; j < C; ++j) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        // Call the function to find the maximum element in the matrix
        int result = findMaxElement(matrix);

        // Print the result (maximum element)
        System.out.println(result);
        scanner.close();
    }
}
using System;

class Program {
    static int FindMaxElement(int[,] matrix) {
        // Initialize maxElement with the value of the first element in the matrix
        int maxElement = matrix[0, 0];
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        // Iterate through each row
        for (int i = 0; i < rows; i++) {
            // Iterate through each element in the row
            for (int j = 0; j < cols; j++) {
                // Update maxElement if the current element is greater
                maxElement = Math.Max(maxElement, matrix[i, j]);
            }
        }

        return maxElement;
    }

    static void Main(string[] args) {
        // Read the number of rows (R) and columns (C)
        string[] input = Console.ReadLine().Split();
        int R = int.Parse(input[0]);
        int C = int.Parse(input[1]);

        // Create a 2D array to store the matrix elements
        int[,] matrix = new int[R, C];

        // Read the matrix elements
        for (int i = 0; i < R; i++) {
            input = Console.ReadLine().Split();
            for (int j = 0; j < C; j++) {
                matrix[i, j] = int.Parse(input[j]);
            }
        }

        // Call the function to find the maximum element in the matrix
        int result = FindMaxElement(matrix);

        // Print the result (maximum element)
        Console.WriteLine(result);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.