In: Computer Science
Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++. Please use #include<iostream>, std::min_element, and std::max_element.
Note --> We are taking 100 integers from User and if user enter a value less than 1 or greater than 500 then program exits by printing a error message
Code is
#include <bits/stdc++.h>
#include<iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v (100);
// Take a Loop to push 50 characters in the
Vector
cout<<"Enter 100 integers range from 1
to 500 ";
// Use a loop to take 50 characters from user
for(int i = 0; i < v.size(); i++)
{
cin>>v[i];
if(v[i]<1 || v[i]>500)
{
// if entered value not in range then exit the program
cout<<"Integer value not in Range ";
exit(0);
}
}
// Find the min element
cout << "\nMin Element = "<<
*min_element(v.begin(), v.end());
// Find the max element
cout << "\nMax Element = " <<
*max_element(v.begin(), v.end());
return 0;
}
Code Screenshot
This is how you can
vector of 100 integers and find max and min out .
If u like the answer then Give it a Thumbs Up and have any doubt comment it