C++ Program to Find Largest and Smallest Element of an Array

This is a C++ Program to Find the Largest and Smallest Elements in an Array.

Problem Description

The program takes an array and prints the largest and smallest element in the array.

Problem Solution

1. The program takes an array of elements.
2. Using a for loop, the largest and smallest element is found.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find the Largest and Smallest Elements in an Array. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int arr[10], n, i, max, min;
  6.     cout << "Enter the size of the array : ";
  7.     cin >> n;
  8.     cout << "Enter the elements of the array : ";
  9.     for (i = 0; i < n; i++)
  10.         cin >> arr[i];
  11.     max = arr[0];
  12.     for (i = 0; i < n; i++)
  13.     {
  14.         if (max < arr[i])
  15.             max = arr[i];
  16.     }
  17.     min = arr[0];
  18.     for (i = 0; i < n; i++)
  19.     {
  20.         if (min > arr[i])
  21.             min = arr[i];
  22.     }
  23.     cout << "Largest element : " << max;
  24.     cout << "Smallest element : " << min;
  25.     return 0;
  26. }
Program Explanation

1. The user is initially asked to enter the size of the array and it is stored in the variable ‘n’.
2. An array ‘arr’ of data type integer is declared with size 10.
3. Elements of the array are asked to enter and stored in ‘arr’ using a for loop.
4. The value at index 0 of arr is assigned to the variable ‘max’.
5. Using a for loop and initializing ‘i’ as 0, the largest element is found.
6. If max is less than arr[i], then value of arr[i] is assigned to max. i is incremented in every iteration.
7. The loop continues till ‘i’ is less than ‘n’.
8. Similarly, the smallest element is found.
9. The value at index 0 of arr is assigned to the variable ‘min’.
10. Using a for loop the smallest element is assigned to min.
11. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter the size of the array : 5
Enter the elements of the array :  1 2 3 4 5
Largest element : 5
Smallest element : 1
 
Case 2 :
Enter the size of the array : 3
Enter the elements of the array : 36  136  0
Largest element : 136
Smallest element : 0
 
Case 3 :
Enter the size of the array : 10
Enter the elements of the array : 24  56  12  8  50  69  244  81  52  73
Largest element : 244
Smallest element : 8

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.