In: Computer Science
You MUST use VECTORS in this lab. Do NOT use ARRAYS.
Write code in C++ with //comments . Please include a screen shot of the output
Part 1: Largest and Smallest Vector Values
Specifications:
Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector.
Create 3 functions in addition to your main function.
Your main function should call these functions and display the vector, the largest value and the smallest value.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
vector<int> generate_vector() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(50 + (rand() % 51));
}
return v;
}
int minimum(vector<int> v) {
int min = v[0];
for (int i = 0; i < v.size(); ++i) {
if (v[i] < min)
min = v[i];
}
return min;
}
int maximum(vector<int> v) {
int max = v[0];
for (int i = 0; i < v.size(); ++i) {
if (v[i] > max)
max = v[i];
}
return max;
}
int main() {
srand(time(NULL));
vector<int> v = generate_vector();
cout << "Vector is: ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
cout << "Minimum: " << minimum(v) << endl;
cout << "Maximum: " << maximum(v) << endl;
return 0;
}
