fork download
  1. #include <math.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6.  
  7. std::vector<int> GeneratorOfRandNums(int *userInput) {
  8. std::vector<int> tempVector;
  9. srand(time(0));
  10.  
  11. for (int i = 0; i < *userInput; i++) {
  12. tempVector.push_back(rand());
  13. }
  14. return tempVector;
  15. }
  16.  
  17. void PrintGeneratedVector(std::vector<int> &userInput) {
  18.  
  19. for (int i = 0; i < userInput.size(); i++) {
  20. std::cout << i << ": " << userInput[i] << std::endl;
  21. }
  22. }
  23.  
  24. void PrintPair(std::pair<int, int> &userInput) {
  25. std::cout << "Minimum value: " << userInput.first << std::endl;
  26. std::cout << "Maximum value: " << userInput.second << std::endl;
  27. }
  28.  
  29. std::pair<int, int> GetMinMaxValue(std::vector<int> &userInput) {
  30. std::pair <int, int> resultMinAndMaxOfVector;
  31.  
  32. int minValue = *std::min_element(userInput.begin(), userInput.end());
  33. int maxValue = *std::max_element(userInput.begin(), userInput.end());
  34.  
  35. resultMinAndMaxOfVector = std::make_pair(minValue, maxValue);
  36. return resultMinAndMaxOfVector;
  37. }
  38.  
  39. int main() {
  40.  
  41. int userInput;
  42. std::vector<int> vectorOfRandomNums;
  43. std::pair<int, int> pairOfMinAndMax;
  44.  
  45. std::cout << "Input vector length: " << std::endl;
  46. std::cin >> userInput;
  47.  
  48. vectorOfRandomNums = GeneratorOfRandNums(&userInput);
  49. if (!vectorOfRandomNums.empty()) {
  50.  
  51. pairOfMinAndMax = GetMinMaxValue(vectorOfRandomNums);
  52. PrintPair(pairOfMinAndMax);
  53. PrintGeneratedVector(vectorOfRandomNums);
  54. }
  55. else {
  56. std::cout << "Vector of generated numbers is null!\n";
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 15240KB
stdin
44
stdout
Input vector length: 
Minimum value: 18491118
Maximum value: 2094309981
0: 1034661523
1: 559159489
2: 860933526
3: 840232879
4: 1091849914
5: 2094309981
6: 1372014482
7: 956752960
8: 1998782651
9: 595548178
10: 842665541
11: 202680441
12: 1444836769
13: 1620495622
14: 1699625462
15: 82116791
16: 1535322487
17: 1218283964
18: 697816191
19: 2082251233
20: 1374819976
21: 138756295
22: 1422825007
23: 234772145
24: 18491118
25: 595992050
26: 1764794990
27: 1464012360
28: 1454793767
29: 174903943
30: 1041966523
31: 341971643
32: 734063432
33: 1902900050
34: 1182204522
35: 1825913347
36: 1849726383
37: 406735356
38: 635182659
39: 1701025386
40: 1002283535
41: 1477848201
42: 1903705827
43: 299636656