Finding Max Element in an Array using C++

Finding Max element in an Array using C++:

In this article, I am going to discuss the program for Finding the Max element in an Array using C++ Language with examples. Please read our previous articles, where we discussed the program for Calculating the Sum of all elements in an array using C++ Language with examples.

Finding Max element in an Array using C++:

Here, we will write a program for finding the maximum element in an Array using C++ Language.

program for Finding the Max element in an Array using C++ Language with examples

Here we have an Array with some elements from which we have to find out the maximum element.

Procedure for Finding Max element in an Array:

program for Finding the Max element in an Array using C++ Language with examples

Initialize the ‘max’ variable to the A[0] element of the array. From here we will compare every element with the ‘max’ and if (A[i] > max) then we will assign max = A[i].

Finding the Max element in an Array using C++ Language with examples

Now A[1] < max that is 5 < 12. So we will not modify max here.

Finding the Max element in an Array using C++ Language with examples

Here A[2] > max that is 25 > 12. So we will assign max to A[2] i.e. ‘max’ = 25.

Finding the Max element in an Array using C++ Language

Now A[3] < max that is 2 < 25. So we will not modify max here.

Finding the Max element in an Array using C++ Language

Here also A[4] < max that is 1 < 25. So we will not modify max here. After traversing the whole array, the value of ‘max’ is 25 which is the maximum element present in the array. This is a simple procedure of comparing all the elements with a single variable ‘max’. Now let us see this procedure in table form. First, we will initialize ‘max’ to A[0],

Finding the Max element in an Array using C++

Now we will compare max with A[1],

Finding the Max element in an Array using C++

‘max’ > A[1], so ‘max’ will remain unchanged. Then compare it with A[2],

Finding the Max element in an Array

A[2] is greater than max. So we will assign max to A[2] i.e. ‘max’ = 25. In the same manner, we will iterate through the array.

Finding the Max element in an Array

We were checking that if A[i] is greater than the maximum then change Max to A[i], so we have to scan all the elements and every time check with the max. So basically, we should start from the second value onwards. So, these are the repeating steps. Let us write a program for that one.

Program to find maximum element in an array using C++:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int n, max = 0;
    cout <<"Enter size of the array: ";
    cin >> n;
    int A[n];
    cout <<"Enter elements of the array:\n";
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
        if(i >= 1 && A[i] > max)
        {
              max = A[i];
        }
    }
    cout <<"Max element : " << max << endl;
    getch ();
}
Output:

In the next article, I am going to discuss Linear Search in C++ with examples. Here, in this article, I try to explain the program for Finding the Max element in an Array using C++ Language with examples. I hope you enjoy this Finding Max element in an Array using C++ Language with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

2 thoughts on “Finding Max Element in an Array using C++”

  1. The above code has an issue if the largest number is there at index 0 it will never be displayed.

    The corrected code:-

    #include
    #include
    using namespace std;
    int main()
    {
    int n, max = 0;
    cout <> n;
    int A[n];
    cout <<"Enter elements of the array:\n";
    for (int i = 0; i > A[i];
    if(i==0)
    max = A[i];
    else
    {
    if(i >= 1 && A[i] > max)
    {
    max = A[i];
    }
    }
    }
    cout <<"Max element : " << max << endl;
    getch ();
    }

    1. Muhammad Arham

      simple code to find maximum number
      #include
      using namespace std;

      int main()
      {
      int a[ ]={ 1,2,3,4,8,9,5,11};
      int max=a[0];
      for(int i=0;imax)
      max=a[i];
      }
      cout<<"The Maximum number is "<<max<<endl;
      return 0;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *