C Online Compiler
Example: Maximum Element in Column - Function Approach
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Maximum Element in Column - Function Approach #include
// Function to find the maximum element in a specified column // Parameters: // matrix: The 2D array (matrix) // rows: Number of rows in the matrix // cols: Number of columns in the matrix // target_column: The 0-indexed column to search // Returns: The maximum element in the target_column int findMaxInColumn(int matrix[][4], int rows, int cols, int target_column) { // Initialize max_element with the first element of the target column // Assumes target_column is valid and rows > 0. int max_element = matrix[0][target_column]; // Iterate through the rows to find the maximum for (int i = 1; i < rows; i++) { if (matrix[i][target_column] > max_element) { max_element = matrix[i][target_column]; } } return max_element; } int main() { // Step 1: Define the matrix and its dimensions int matrix[3][4] = { {10, 20, 15, 30}, {25, 12, 18, 22}, {14, 35, 28, 16} }; int rows = 3; int cols = 4; int target_column_1 = 1; // Max in second column int target_column_2 = 3; // Max in fourth column // Step 2: Call the function for target_column_1 int max_in_col1 = findMaxInColumn(matrix, rows, cols, target_column_1); // Step 3: Call the function for target_column_2 int max_in_col2 = findMaxInColumn(matrix, rows, cols, target_column_2); // Step 4: Print the results printf("Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%4d", matrix[i][j]); } printf("\n"); } printf("\nMaximum element in column %d is: %d\n", target_column_1, max_in_col1); printf("Maximum element in column %d is: %d\n", target_column_2, max_in_col2); return 0; }
Output
Clear
ADVERTISEMENTS