Maximum element in an array in C++


Q.Write a C++ program to find maximum element in an array.

#include <iostream>   
using namespace std;
  
int main()
{  
    int a[100], size, i, max;
    cout << "Enter the number of elements in array: ";
    cin >> size;
    cout <<"Enter the integers: ";
    for (i = 0; i < size; i++)
    {
        cin >>a[i];
    }
    max = a[0];
    for (i = 1; i < size; i++)
    {
        if (a[i] > max)
        {
            max  = a[i];
        }
    }
    cout << "Maximum element in an array is " <<  max;
   	return 0;    
}   

Q. Write an algorithm to find maximum element in an array.

1. Start
2. Read the integer `size` (number of elements).
3. Initialize an array `a` of length `size`.
4. For `i` from `0` to `size - 1`, do:
   - Read the element `a[i]`.
5. Initialize `max` as the first element of the array, `a[0]`.
6. For `i` from `1` to `size - 1`, do:
   - If `a[i]` is greater than `max`,
   - Update `max` to be `a[i]`.
7. Output the `max` value as the maximum element in the array.
8. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.