# Find min and max in array c++

Welcome dear readers! we are going to discuss a c++ program to find min and max elements in the array. Basically, it is a practice program that will help us understand different variations of array traversal for different purposes. Let’s suppose you have an array containing the salaries of employees. This program can help us find the min and max salaries. We will solve this practice program through a stepwise approach. I will discuss the iterative method to find min and max in an array in c++. Let’s learn how to do it.

## How to find max value in array c++?

• First of all, declare an array and read array size from the user.
• Read input at all array indexes from the user.
• Now it’s time to find the maximum value in the array.
•  Take a variable` max` and initialize it to the first array index `Arr[0]`.
•  Now run a for loop till the size of the array to traverse all array elements.
•  If `max` is smaller than the current array element, update `max` to the new maximum value.
•   This condition, present inside the loop, will help us compare `max `from the` start` until the end of the array. If any value greater than `max` is found, it will be store in `max` to update it.
```int Arr[50];
int x;
cin >> x;

for (int i = 0; i < x; i++)
cin >> Arr[i];

int max = Arr[0];

for (int i = 0; i < x; i++)
if (max < Arr[i])
max = Arr[i];

cout << max << endl;
```

Now, this is the simple code of all the above-mentioned points.

## How to find min value in array c++?

We have already found the max value in the array. Therefore, understanding this code won’t be difficult for us. In case of finding the min value in an array, we will also traverse the whole array.

• Again we will take an array and read array size.
• We will ask the user to enter input in the array as usual.
•  Now we will declare a variable `min` to initialize it with the first array index value.
•  Again, we will run a for loop less than array size times to traverse all elements of the array.
•  We will update `min` to the new minimum value if  `min` is larger than the next array index .i.e. (`min > Arr[i]`).
•  This condition is written inside the for loop and is the core part of this program.
``` int Arr[50];
int x;
cin >> x;

for (int i = 0; i < x; i++)
cin >> Arr[i];

int min = Arr[0];
for (int i = 0; i < x; i++)

if (min > Arr[i])
min = Arr[i];

cout << min <<  endl;```

Here is the simple and easy-to-understand code for all the above-explained steps.

## Program to find min and max in array c++?

We have already learned how to find min and max values in an array in c++ separately. Now let’s combine the above two concepts together to find min and max in a single c++ program.

•  Take two variables `min` and `max` and initialize both with the value at the first index of the array.
•  Run a for loop for the traversal of the whole array and based on conditions, update the value of `min` and `max`.
•  If `max` is smaller than the next array element, update `max` to the new maximum value.
•  In the same way, if  `min` is greater than the next array element, update it to the new minimum value.
```int max = Arr[0];
int min = Arr[0];
for (int i = 0; i < x; i++) {

if (max < Arr[i]) {

max = Arr[i];

} else if (min > Arr[i]) {

min = Arr[i];
}

}
﻿```

Now let’s see the fully formatted code of a complete program. In this program, the user is first asked to enter array size and then array elements. Then, the array is traversed to find the min and max values in array in c++. In the end, maximum and minimum values are printed to the user.

```#include<iostream>

using namespace std;
int main() {
int Arr[50];
int x;
cout << "Enter Array size:" << endl;
cin >> x;

for (int i = 0; i < x; i++) {

cout << "Enter the number at index " << i << endl;
cin >> Arr[i];
}

int max = Arr[0];
int min = Arr[0];
for (int i = 0; i < x; i++) {

if (max < Arr[i]) {

max = Arr[i];

} else if (min > Arr[i]) {

min = Arr[i];
}

}

cout << max << " is the Maximum and " << min << " is the minimum number" << endl;
return 0;
}
﻿```

Output of program:

That’s all about finding min and max in arrays in c++. Now it’s time to wind up and conclude the article.  I hope you liked my effort and the content that I shared in this article. I am pretty sure you are now clear about finding min and max in arrays. Thanks for reading and supporting us.

### 3 thoughts on “Find min and max in array c++”

1. This is nice. Now, I wanted the explaination of iteration method.
Is the program above written using iteration method?

Scroll to Top