C Online Compiler
Example: Maximum Element in Column - Direct Iteration
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Maximum Element in Column - Direct Iteration #include
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; // We want to find the max in column 1 (0-indexed, second column) // Step 2: Initialize max_element with the first element of the target column // It's good practice to initialize with the smallest possible integer // or the first element of the column itself to handle all cases correctly. int max_element = matrix[0][target_column]; // Step 3: Iterate through the rows to find the maximum in the target column for (int i = 1; i < rows; i++) { if (matrix[i][target_column] > max_element) { max_element = matrix[i][target_column]; } } // Step 4: Print the result 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, max_element); return 0; }
Output
Clear
ADVERTISEMENTS