C++ Program to Find Largest Element of an Array


January 19, 2023, Learn eTutorial
1201

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

What is an Array?

The array is a data structure in C++, which is used to store some elements of the same data type in a sequence of memory locations. It can be also defined as a collection of values of the same type which can be accessed by an index variable. Array[index]. Always array index starts with the zeroth position as array[0] is the first position.

For example, we have the names of some students and we can save that using an array structure.

char arrayName[arraySize];

For example,
char x[6];
Here,

  • int - a type of element to be stored
  • x - the name of the array
  • 6 - the size of the array.

How do you find the largest number in an array?

Assume an array 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. Finally, we get the largest element of the array in arr[0].

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. For that, declare another for loop to find the largest one and store the largest to arr[0]. It will access the elements from the array from its beginning. For performing the comparison by using a conditional statement.

If  arr[0] < arr [i] then store 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 uses 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