Write ac program to find the largest element from each row of a two dimensional matrix

#include <stdio.h> int main() { int n; double arr[100]; printf("Enter the number of elements (1 to 100): "); scanf("%d", &n); for (int i = 0; i < n; ++i) { printf("Enter number%d: ", i + 1); scanf("%lf", &arr[i]); } // storing the largest number to arr[0] for (int i = 1; i < n; ++i) { if (arr[0] < arr[i]) { arr[0] = arr[i]; } } printf("Largest element = %.2lf", arr[0]); return 0; }

Output

Enter the number of elements (1 to 100): 5 Enter number1: 34.5 Enter number2: 2.4 Enter number3: -35.5 Enter number4: 38.7 Enter number5: 24.5 Largest element = 38.70

This program takes n number of elements from the user and stores it in the arr array.

To find the largest element,

  • the first two elements of array are checked and the largest of these two elements are placed in arr[0]
  • the first and third elements are checked and largest of these two elements is placed in arr[0].
  • this process continues until the first and last elements are checked
  • the largest number will be stored in the arr[0] position
// storing the largest number at arr[0] for (int i = 1; i < n; ++i) { if (arr[0] < arr[i]) { arr[0] = arr[i]; } }

Program description:- Write a C program to find the largest and smallest in a 2d array of numbers with their position or location. Take an array of numbers as input, find the largest and smallest element among them, and display the result.

To write this program, first, we need to take the largest and smallest variable, which will be used to compare with all array elements. We can initialize them with 0, but it will be valid only if the array contains only positive numbers.

// wrong largest = 0; smallest = 0;

If we are initializing smallest variable with 0 and array contains only positive numbers then 0 always will be the smallest among them, and we won’t find the correct smallest element. Similarly if largest is initialized with 0 and array contains only -ve numbers then we will never find the correct largest numbers.

// assume first element is // largest and smallest largest = arr[0][0]; smallest = arr[0][0];

Due to above problem, better to assign first element of array to the smallest and largest variable then compare it with remaining elements of the array. Now, let us develop the program.

Largest Smallest in 2d Array using C

#include<stdio.h> int main() { int m, n, largest, smallest; int largrowloc, largcolumnloc, smallrowloc, smallcolumnloc; // take number of rows and columns printf("Enter number of row and column: "); scanf("%d %d",&m,&n); // declare array by given number of rows and columns int arr[m][n], i, j; // take array elements as input for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("Enter arr[%d][%d]: ",i,j); scanf("%d", &arr[i][j]); } printf("\n"); } // display array (optional) printf("Entered 2D Array:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",arr[i][j]); } printf("\n"); } // assume first element is // largest and smallest largest=arr[0][0]; smallest=arr[0][0]; // compare with all elements for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(largest<arr[i][j]) { largest=arr[i][j]; largrowloc=i; //row location of largest element largcolumnloc=j; //column location of largest element } if(smallest>arr[i][j]) { smallest=arr[i][j]; smallrowloc=i; //row location of smallest element smallcolumnloc=j; //column location of smallest element } } } // display results printf("\n"); // new line printf("Largest element in array is %d in location arr[%d][%d]\n", largest, largrowloc, largcolumnloc); printf("Smallest element in array is %d in location arr[%d][%d]\n", smallest, smallrowloc, smallcolumnloc); return 0; }

Output:-

Enter number of row and column: 3,3Enter arr[0][0]: 4Enter arr[0][1]: 5Enter arr[0][2]: 6Enter arr[1][0]: 9Enter arr[1][1]: 8Enter arr[1][2]: 7Enter arr[2][0]: 2Enter arr[2][1]: 3Enter arr[2][2]: 5Entered 2D Array:4 5 69 8 72 3 5Largest element in array is 9 in location arr[1][0]

Smallest element in array is 2 in location arr[2][0]

In this program, after taking the array elements as input, we are displaying the array. It is optional part, if you don’t want to display then remove that part of the code.

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Examples on multidimensional Array in C

Given a matrix, the task is to find the maximum element of each row.
Examples: 
 

Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56

Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element.   Below is the implementation : 

    void printArray(int result[], int no_of_rows) {

        for (int i = 0; i < no_of_rows; i++) {

    void maxelement(int no_of_rows, int arr[][N]) {

            for (int j = 0; j < N; j++) {

        printArray(result,no_of_rows);

        int arr[][N] = { {3, 4, 1, 8},

    public static void maxelement(int no_of_rows, int[][] arr) {

        int[] result = new int[no_of_rows];

            for (int j = 0; j < arr[i].length; j++) {

    private static void printArray(int[] result) {

        for (int i =0; i<result.length;i++) {

            System.out.println(result[i]);

    public static void main(String[] args) {

        int[][] arr = new int[][] { {3, 4, 1, 8},

    no_of_column = len(arr[0])

    for i in range(no_of_rows):

        for j in range(no_of_column):

public static void maxelement(int no_of_rows,

    int[] result = new int[no_of_rows];

private static void printArray(int[] result)

    for (int i = 0; i < result.Length;i++)

        Console.WriteLine(result[i]);

public static void Main(string[] args)

    int[][] arr = new int[][]

        new int[] {76, 34, 21, 1},

function printArray($result, $no_of_rows)

    for ($i = 0; $i < $no_of_rows; $i++)

function maxelement($no_of_rows, $arr)

    $result=array_fill(0,$no_of_rows,0);

        for ($j = 0; $j < $N; $j++)

    printArray($result,$no_of_rows);

$arr = array(array(3, 4, 1, 8),

function maxelement(no_of_rows, arr)

    var result = Array.from({length: no_of_rows}, (_, i) => 0);

        for (var j = 0; j < arr[i].length; j++)

function printArray(result)

    for (var i = 0; i < result.length; i++)

        document.write(result[i]+"<br>");

Output :

8 11 76 5

Time Complexity: O(n*m) (where, n refers to no. of rows and m refers to no. of columns)

Auxiliary Space: O(n) (where, n refers to no. of rows)