Dark Mode On/Off

# C Program To Find the largest element in a row

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 row in an array.

But before moving forward if you are not familiar with the concept of array in C, then do check the article on Arrays in C.

Input : Enter the matrix elements:

1 2 3

4 5 6

7 8 9

Output:

Largest element in row 1 is 3

Largest element in row 2 is 6

Largest element in row 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 Row

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

### 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 rows.
5. Check each element for the row and find the maximum eleement.
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 row directly. Firstly, a 2D array is declared and then is initialized. Then, we will find the largest element in a row 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 < m)   //Check for the largest element in an array
{
for ( j = 0; j < n; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];
}
}
res[i] = max;
max = 0;
i++;
}
for(int i = 0; i < n; i++)      //Print the largest element in an array
{
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 9
Largest element in row 2 is 8

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

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

### Algorithm:

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

Below is the code for the same.

In the below program, 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 < m)
{
for ( j = 0; j < n; j++)
{
if (arr[i][j] > max)          //Check for the maximum element in the array
{
max = arr[i][j];           //Assign the largest element
}
}
res[i] = max;
max = 0;
i++;
}
for(int i = 0; i < n; i++)           //Print the largest element in each row
{
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: 9 7 8 6 5 7 4 3 8

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

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.