C++ Program to Find Largest Element of an Array

Here in this program, we will learn to access array elements in C++ programming and find out the largest element within the given array.

What is an Array?

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate variables, we can simply create an array.
dataType arrayName[arraySize];
For example,
int x[6];
Here,
•    int - a type of element to be stored
•    x - the name of the array
•    6 - the size of the array.

How to access elements in a C++ array?

In C++, each element in an array is associated with a number. The number is known as an array index. We can access elements of an array by using those indices.
array[index];

How do you find the largest number in an array?

Assume an array is with n number of elements. To find the largest element, check the first two elements of the array and store the largest of these two elements in the first index, arr[0]. Then check the third element with the first and place the largest among these two in arr[0]. Continue the process till the last element.

How to find the largest element of an array in C++?

Take the number of elements from the user from 1 – 100 and read the value to the integer type variable n. Store the values entered by the user into the array by using a for loop.
Set the value of Initialization variable i to 0 and check for the condition i and update i by an increment. Read the entering number to the array from the ith indices. Now our loop will be like
for (i = ;i < n; ++i)
{ cin >> arr [i] }

Now the user entered n elements are stored within the array. We have to find the largest element among the entered elements. Declare another for loop to find the largest one and store the largest to arr[0].
Set the value of Initialization variable i to 1 and check for the condition i and update i by an increment. This will access the elements from the array from its beginning. For performing the comparison make a conditional statement.
If  arr[0] < arr [i] then store the the largest one in arr[0]. After completing the for loop the largest among the entered elements can be accessed from arr[0].

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables i, n; and float type array arr[100];

Step 5: Ask the user to enter the total number of elements ( 1 to 100 ).

Step 6: Read the value to the variable n;

Step 7: Ask the user to enter the elements into the array.

Step 8: Read the entered elements into the array using a for loop.

Step 9: Acess the first two elements of the array.

Step 10: Compare the first two elements and store the largest element in the array                            indices arr[0].
Step 11: Compare the third element and the element in arr[0] and store the largest on                       arr[0].
Step 12: Continue the process until the final element using a for loop.

Step 13: Get the largest from the arr[0].

Step 14: Exit. 

 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {

  int i, n;
  float arr[100];

  cout << "Enter total number of elements(1 to 100): ";
  cin >> n;
  cout << endl;

  // Store number entered by the user
  for(i = 0; i < n; ++i) {
    cout << "Enter Number " << i + 1 << " : ";
    cin >> arr[i];
  }

  // Loop to store largest number to arr[0]
  for(i = 1;i < n; ++i) {

    // Change < to > if you want to find the smallest element
    if(arr[0] < arr[i])
      arr[0] = arr[i];
  }

  cout << endl << "Largest element = " << arr[0];

  return 0;
}
                                      

OUTPUT

Enter total number of elements(1 to 100): 6
Enter Number 1 : 56
Enter Number 2 : 25
Enter Number 3 : 2
Enter Number 4 : 63
Enter Number 5 : 21
Enter Number 6 : 6
Largest element = 63