 C++ Tutorials

# C++ Tutorials

## Program to find the minimum or maximum element of an array in C++ using function

``````#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int MaxMinArray(int arr[] , int n){
int max = arr, min = arr;
for(int i = 1; i < n; i++){
if(max < arr[i])
max = arr[i];
if(min > arr[i])
min = arr[i];
}
cout<<"Maximum Element of array: = "<<max<<"\n";
cout<<"Minimum Element of array: = "<<min;

return 0;
}
int main(){
int arr = {1, 2, 3, 4, 5};
int N=5;
MaxMinArray(arr, N);
return 0;
}``````

## Find the minimum and maximum element of an Array in C++

``````#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int main ()
{
int arr, n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;

for (i = 0; i < n; i++){
cout <<"Enter "<<i+1<<" number of element array: ";
cin >> arr[i];

}

max = arr;
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr;
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element of array : " << max<<endl;
cout << "Smallest element of array : " << min<<endl;

return 0 ;
}``````

## C++ program to find the min and max element.

Learning how to find the maximum or minimum value of an array in C++ so what you’re looking at here I’ve already gone and defined and initialized some array that array is called values that’s the name it’s of type double it has five values in it and I’ve initialized these five values to be the five values.

So what we want to do is we’re going to use a for loop to basically figure out what the biggest value is first let’s try and calculate what the maximum value is so I’ll write our for loop here and what we want to do at the end of the day.

We’re going to run this as many times as we need to so probably five times ish and at the end of the day we only want to print out the maximum value of the whole array once so that means we’re going to be seeing out something that’s green outside of the leap is if we see it out inside the loop we we probably print five things to the screen and we don’t really want to do that so we’re gonna see out something now we’re seeing out the maximum value.

So we should define that maximum value somewhere as a variable now if we define a variable inside a for loop its scope is sort of limited to inside that for loop so we need to define a variable outside of for loop so let’s do that here so this will become the variable that will be storing or maximum value in so we’ll make a double we’ll call it max and for now let’s just store the very first element in there just as a placeholder.

So we’re going to store values zero now that’s a no values zero just like that okay so and then at the end of the day we’re going to run our loop which we haven’t written yet but we will in a second and then we’re just going to see out our max value just once to the user probably end the line and call it a day all right so what we’re going to do is we’re going to start with our first element

.We’re going to say this is the biggest element we’ve seen so far then we’re going to look at our second element and we’re going to check to see if this is bigger than the previous one if so this is our new biggest element then we’re going to do is when I look at the next one we’re going to say is this one bigger than the current biggest one if so that will become our biggest one and so on we’ll continue through that way.

So what we’re going to do by saying that actually element 0 with index number of 0 is our current Mac we’ve actually done our first iteration outside of the loop so when we come here to initialize our counter we’re actually going to start and I at 1 we would normally start this at 0 but like I said we’ve already checked to see is the first number the biggest number we’ve found.

So far yes because it’s the only number we found so far so starting this at 1 instead of 0 all right moving on our condition so we’re going to check to see is I we’re going to make sure that I is less than 5 and then we’re going to enter increment I plus plus ok so again we’ve always want I less than 5 because we talked about in earlier videos about arrays or we never really want to be calling a value if we never really want to be calling I values 5 like that because that’ll give us a bounds error because that would actually be the sixth element in a valley you know in an array of only 5 elements and just throws everything out the window you get a bunch of errors.

So I less than 5 I plus plus all right now what we need we definitely need an if statement because we’re checking these two variables against each other so we’ll put in our if statement so now we’ll say we’ll save out if values if values I is greater than max then do something and that’s something we’re just going to set max to values I equals values and this will effectively swap out you know give us the biggest number that we found.

So far so let’s think about why this works so we set double Max equal to value 0 so we’re setting it to the index number of 0 so remember our index numbers are 0 1 2 3 4 ok so 0 values 0 that is number 2 now we check in our if statement we check if values I is greater than max well values I this I is actually set to the 1 not 0 here and so values 1 we check our index number.

So 0 1 that’s or so basically if if 4 is greater than 2 then we’re going to set max to be that 4 and so that’s exactly what’s happening and then it’ll try check against then the next time you come through you’ll say is 4 greater than a or is 8 greater than 4 sorry and if yes it’ll update that to an 8

So on so when we go and build and run this program we should expect this to print us out on max once to the screen and that max will be 8 just like that so there you go we can easily find the minimum as well we could just maybe change this to min change that to min just so it makes sense for human users min and then all we’re going to do is we’re going to switch the order work actually to keep that one.

We’re going to switch the direction of this guy so we’ll be like that and then this exact same thing is going to happen we’re going to start min is going to equal 2 we’re always starting with the first element and then if values 1 so if this 4 is less than the 2 we’ll swap it out and that’s not going to happen until we get to this one here so build and run save that file and when we go and run boom prints out a 1 to the screen all right so there you go I imagine if you had an array with a hundred values you might want to be doing something like this with a for loop so that is how you find a maximum or minimum value of an array and C++.