
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find Largest Element of an Array
An array contains multiple elements and the largest element in an array is the one that is greater than other elements.
For example.
5 | 1 | 7 | 2 | 4 |
In the above array, 7 is the largest element and it is at index 2.
A program to find the largest element of an array is given as follows.
Example
#include <iostream> using namespace std; int main() { int a[] = {4, 9, 1, 3, 8}; int largest, i, pos; largest = a[0]; for(i=1; i<5; i++) { if(a[i]>largest) { largest = a[i]; pos = i; } } cout<<"The largest element in the array is "<<largest<<" and it is at index "<<pos; return 0; }
Output
The largest element in the array is 9 and it is at index 1
In the above program, a[] is the array that contains 5 elements. The variable largest will store the largest element of the array.
Initially largest stores the first element of the array. Then a for loop is started which runs from the index 1 to n. For each iteration of the loop, the value of largest is compared with a[i]. If a[i] is greater than largest, then that value is stored in largest. And the corresponding value of i is stored in pos.
This is demonstrated by the following code snippet.
for(i=1; i<5; i++) { if(a[i]>largest) { largest = a[i]; pos = i; } }
After this, the value of the largest element in the array and its position is printed.
This is shown as follows −
cout<<"The largest element in the array is "<<largest<<" and it is at index "<<pos;
- Related Articles
- Python Program to find largest element in an array
- C# Program to find the largest element from an array
- Python Program to find the largest element in an array
- Program to find largest element in an array in C++
- Golang Program to Find the Largest Element in an Array
- C# Program to find the largest element from an array using Lambda Expressions
- C++ Program to find the second largest element from the array
- Kth Largest Element in an Array
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Java program to find the largest number in an array
- Program to find out the index in an array where the largest element is situated in Python
- Java program to find the 3rd largest number in an array
- Java program to find the 2nd largest number in an array
- 8085 Assembly language program to find largest number in an array
- Recursive program to find an element in an array linearly.
