Skip to content

Instantly share code, notes, and snippets.

@JohnPhamous
Created November 21, 2017 18:29
Show Gist options
  • Save JohnPhamous/be5b0bc5d518e08d90e9cb9a49523845 to your computer and use it in GitHub Desktop.
Save JohnPhamous/be5b0bc5d518e08d90e9cb9a49523845 to your computer and use it in GitHub Desktop.
// Written by John Pham
// [email protected]
#include <iostream>
// to use rand() and srand()
#include <stdlib.h>
// to use vectors
#include <vector>
using namespace std;
vector<int> generate(int n, int a, int b) {
vector<int> response;
for (int i = 0; i < n; i++) {
response.push_back(rand() % b + a);
}
return response;
};
int getMax(const vector<int>& v) {
int max = 0;
for (int i = 0; i < v.size(); i++) {
if (v.at(i) > max) {
max = v.at(i);
}
}
return max;
};
void print(const vector<int>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v.at(i) << ", ";
}
cout << endl;
}
int main(int argc, char** argv) {
cout << "How many random numbers would you like to generate?" << endl;
int numOfNumbers = 0;
cin >> numOfNumbers;
cout << "What is the lower bound?" << endl;
int lowerBound = 0;
cin >> lowerBound;
cout << "What is the upper bound?" << endl;
int upperBound = 0;
cin >> upperBound;
// Instructor specified to use 5 as the seed
srand(5);
vector<int> randomNumbers;
randomNumbers = generate(numOfNumbers, lowerBound, upperBound);
cout << "The max number in the vector is: ";
int maxNum = getMax(randomNumbers);
cout << maxNum << endl;
cout << "The generated vector is: " << endl;
print(randomNumbers);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment