Write a C++ Program to Find Largest Element of an Array

In C++, an array is a collection of elements of the same data type that are stored in a contiguous block of memory.

To find the largest element of an array, we need to traverse through each element of the array and compare it with the current maximum value.

Here’s an example program that finds the largest element of an array in C++:

#include <iostream>
using namespace std;

int main() {
  int arr[5] = {10, 20, 30, 40, 50};
  int max = arr[0];

  for (int i = 1; i < 5; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }

  cout << "The largest element in the array is: " << max << endl;
  
  return 0;
}

In this program, we have initialized an array of 5 integers with some values.

We have also initialized a variable max with the first element of the array.

We then use a for loop to traverse through each element of the array, starting from the second element, and compare it with the current value of max.

If the current element is greater than max, we update the value of max to the current element.

Finally, we print the value of max as the largest element of the array.

This program can be modified to work with arrays of any size and any data type by changing the size of the array and the data type of the elements.

Further, we can also take the size of the array as input from the user to make the program more flexible.