Dark Mode On/Off

# C Programs To Find the largest element in a column

A 2D array is of the form M*N where M stands for the number of rows and N stands for the number of column. Given a 2D array and our task is to find the largest element in a column in an array. But before moving forward, if you are not familiar with the concept of array then, do check the article on Arrays in C.

Input: 1 2 3

4 5 6

7 8 9

Output:

Largest element in column 1 is 7

Largest element in column 2 is 8

Largest element in column 3 is 9 This problem can be solved in the following ways:

Method 1: Without using Functions

Method 2: Using Functions

Let us look at each of the methods separately.

## Program 1: Find the Largest Element in a Column

In this method, we will directly find the largest element in a column. Firstly, we declare a 2-D array and then initialize it. Then, we find the largest element in the column.

### Algorithm:

1. Start
2. Declare a 2D array.
3. Initialize the 2D array.
4. The idea is to run the loop for the total number of columns.
5. Check each element for the column and find the maximum element.
6. Now print the elements.
7. Stop.

Below is the code for the same.

In the below program, we will find the largest element in a column directly. Firstly, a 2D array is declared and then is initialized. Then, we will find the largest element in a column directly.

``````#include <stdio.h>

int main()
{
int m,n;                 //Matrix Size Declaration
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);   //Matrix Size Initialization
int arr;        //Matrix Size Declaration
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)    //Matrix Initialization
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++)     //Print the matrix
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
int i = 0, j;
int max = 0;
int res[m];
while (i < n)   //Check for the largest element in an array
{
for ( j = 0; j < m; j++)
{
if (arr[j][i] > max)      //Check if the element is greater than the maximum element of the column and replace it
{
max = arr[j][i];
}
}
res[i] = max;
max = 0;
i++;
}
for(int i = 0; i < n; i++)      //Print thee largest element
{
printf("Largest element in row %d is %d \n", i, res[i]);
}

return 0;
}``````

Enter the number of rows and column: 3 3

Enter the elements of the matrix: 5 7 6 3 4 9 7 8 2

The elements in the matrix are:
5 7 6
3 4 9
7 8 2
Largest element in row 0 is 7
Largest element in row 1 is 8
Largest element in row 2 is 9

## Program 2: Find the Largest Element in a Column

In this method, we will call another function to find the largest element in a column. Firstly, we declare a 2-D array and then initialize it. Then, we call a function to find the largest element in the column.

### Algorithm:

1. Start
2. Declare a 2D array.
3. Initialize the 2D array.
4. Now call a function that will find the maximum element in a column.
5. The idea here is to run the loop for the total number of columns.
6. Check each element for the column and find the maximum element.
7. Now print the elements.
8. Stop.

Below is the code for the same.

The largest element in a row is found out directly by using functions.

``````#include <stdio.h>
void maxElement(int arr,int m,int n)    //Function Definition
{
int i = 0, j;
int max = 0;
int res[m];
while (i < n)   //Check for the largest element in an array
{
for ( j = 0; j < m; j++)
{
if (arr[j][i] > max)      //Check if the element is greater than the maximum element of the column and replace it
{
max = arr[j][i];
}
}
res[i] = max;
max = 0;
i++;
}
for(int i = 0; i < n; i++)      //Print thee largest element
{
printf("Largest element in row %d is %d \n", i, res[i]);
}

}
int main()
{
int m,n;                 //Matrix Size Declaration
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);   //Matrix Size Initialization
int arr;        //Matrix Size Declaration
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)    //Matrix Initialization
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++)     //Print the matrix
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
maxElement(arr,m,n);   //Function Call
return 0;
}``````

Enter the number of rows and column: 3 3

Enter the elements of the matrix: 4 7 5 3 4 9 6 8 2

The elements in the matrix are:
4 7 5
3 4 9
6 8 2
Largest element in row 0 is 6
Largest element in row 1 is 8
Largest element in row 2 is 9