codescracker
c++

C++ Program to Find Largest Number in an Array



« Previous Program Next Program »

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 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:

// C++ Program to Find Largest Number in an Array
// ----codescracker.com----

#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:

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

// Find Largest Element using Function
// ----codescracker.com----

#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 »



© Copyright 2021. All Rights Reserved.

CodesCracker