C++ Program to Find Largest Number in an Array

In this article, you will learn and get code to find and print the largest element or number in an array provided by user (at run-time) in C++. Here are the list of programs:

  • Find Largest Number in an Array without Function
  • Using user-defined Function

Find Largest Element in an Array without Function

To find the largest element in an array in C++ programming, you have to ask from user to enter the size for array and again to enter elements of given size. Then find and print the largest number or element from given list as shown in the program given below.

The question is, write a program in C++ to find and print largest number in an array. Here is its answer:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, larg, i;
    cout<<"Enter the Size (max. 100): ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    larg = arr[0];
    for(i=1; i<tot; i++)
    {
        if(larg<arr[i])
            larg = arr[i];
    }
    cout<<"\nLargest Number = "<<larg;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program find largest element in array

Now enter the size for array say 6. And then enter any 6 numbers or elements. After supplying all the inputs, press ENTER key to find and print the largest number from the given list of numbers as shown in the snapshot given below:

find largest number in array c++

The statement:

larg = arr[0];

states that, it is assumed that the largest element is present at very first index (that is, 0th) of the array. Or it is supposed that the largest element is the first element of array. From next index, we have compared the value of larg with each and every value present at rest indexes. While comparing, if the value of larg is found lesser, then the new value (value which is greater than larg's value) gets initialized to it (larg variable).

The dry run of above program with user input 6 as size, and 1, 2, 3, 6, 5, 4 as 6 array elements, goes like:

  • Because array size is entered as 6. So tot=6
  • And when user enters 6 array elements, then it gets stored in arr[], in this way:
    • arr[0]=1
    • arr[1]=2
    • arr[2]=3
    • arr[3]=6
    • arr[4]=5
    • arr[5]=4
  • Now value at 0th index, that is 1 gets initialized to larg. So larg=1
  • The execution of for loop starts. That is its initialization part gets executed at first and only at once. So i=1
  • With i=1, the condition, i<tot or 1<6 evaluates to be true, therefore program flow goes inside the loop
  • Inside the loop, the condition larg<arr[i] or 1<arr[1] or 1<2 evaluates to be true, therefore program flow goes inside the if's body and the value of arr[i] gets initialized to larg. So larg=2
  • Now the program flow goes to update part of for loop and increments the value of i, evaluates the condition again.
  • That is, i=2 and the condition, i<tot or 2<6 again evaluates to be true, therefore program flow again goes inside the loop
  • This process continues, until the condition evaluates to be false
  • After exiting from the loop, the variable larg holds the largest element from given array
  • So print the value of larg as largest number/element.

Find Largest Element in an Array using Function

Let's create the same purpose program using a user-defined function, findLarge(). This function takes two argument. The first argument is the array, and second is its size. The largest elements gets returned by this function

#include<iostream>
using namespace std;
int findLarge(int [], int);
int main()
{
    int arr[100], tot, larg, i;
    cout<<"Enter the Size (max. 100): ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    larg = findLarge(arr, tot);
    cout<<"\nLargest Number = "<<larg;
    cout<<endl;
    return 0;
}
int findLarge(int a[], int s)
{
    int i, m;
    m = a[0];
    for(i=1; i<s; i++)
    {
        if(m<a[i])
            m = a[i];
    }
    return m;
}

This program produces the same output as of previous program.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »