Guest User

Untitled

a guest
May 27th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. /*
  2. P33: Smallest, Largest, Sum, and Average with Random Numbers
  3. Write a program that generates X random integers Num.
  4. Num is a random number between 20 to 50.
  5. X is a random number between 10 to 15.
  6. Calculate and show the Smallest, Largest, Sum, and Average of those numbers.
  7.  
  8. Sample Run:
  9. Generating 11 random numbers (11 is a random number between 10 to 15)...
  10.  
  11. ...11 Random Numbers between 20 to 50: 26, 23, 48, 32, 44, 21, 32, 20, 49, 48, 34
  12.  
  13. Sum = 377
  14.  
  15. Average = 377 / 11 = 34.3
  16.  
  17. Smallest = 21
  18.  
  19. Largest = 49
  20. */
  21.  
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <iostream>
  25.  
  26. using namespace std;
  27.  
  28. int sum = 0;
  29. int x = 10+rand()%6;
  30. int num = 20+rand()%31;
  31. int max;
  32. int largest;
  33. int smallest;
  34.  
  35. int main(void)
  36. {
  37. srand(time(NULL));
  38. {
  39. while (true){
  40. int sum = 0;
  41. int x = 10+rand()%6;
  42. int num = 20+rand()%31;
  43. cout<<"Generating " << x << " random numbers (" << x << " is between 10 and 15)... n";
  44. int max;
  45. int largest;
  46. int smallest;
  47. cout << "..." << x << " Random Numbers between 20 to 50: n";
  48. largest = num;
  49. smallest = num;
  50. sum = sum + num;
  51.  
  52. while (x < max - 1)
  53. {
  54. cout << num;
  55. sum = sum + num;
  56. x++;
  57. if ( num > largest)
  58. largest = num;
  59. if ( num < smallest)
  60. smallest = num;
  61. }
  62. cout << "Largest = " << largest << endl;
  63. cout << "Smallest = " << smallest << endl;
  64. cout << "Sum = " << sum << endl;
  65. cout << "Average = " << sum / max << endl;
  66.  
  67. break;
  68. }
  69. return 0;
  70. }
  71. }
  72.  
  73. /*
  74. SAMPLE RUN
  75. ==========
  76. Generating 10 random numbers (10 is between 10 and 15)...
  77. ...10 Random Numbers between 20 to 50:
  78. Largest = 29
  79. Smallest = 29
  80. Sum = 29
  81. Average = -14
  82.  
  83. Process returned 0 (0x0) execution time : 0.172 s
  84. Press any key to continue.
  85.  
  86. */
  87.  
  88. #include <random>
  89. #include <iostream>
  90.  
  91. int main()
  92. {
  93. int range_min = 0;
  94. int range_max = 1000;
  95. int amount_numbers = 50;
  96.  
  97. std::random_device rd;
  98. std::uniform_int_distribution<int> distribution(range_min, range_max);
  99. for (int i = 0; i < amount_numbers; i++)
  100. {
  101. std::cout << distribution(rd) << "n";
  102. }
  103.  
  104. return 0;
  105. }
  106.  
  107. #include <random>
  108. #include <algorithm>
  109. #include <numeric>
  110. #include <functional>
  111. #include <iostream>
  112. #include <algorithm>
  113.  
  114. int main()
  115. {
  116. int range_min = 0;
  117. int range_max = 1000;
  118. int amount_numbers = 50;
  119.  
  120. std::random_device rd;
  121. std::uniform_int_distribution<int> distribution(range_min, range_max);
  122.  
  123. std::vector<int> numbers;
  124. for (int i = 0; i < amount_numbers; i++)
  125. {
  126. numbers.push_back(distribution(rd));
  127. }
  128.  
  129. std::sort(numbers.begin(), numbers.end());
  130.  
  131. int smallest = numbers.front();
  132. int biggest = numbers.back();
  133. int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
  134. float average = 1.0*sum / amount_numbers;
  135.  
  136. return 0;
  137. }
  138.  
  139. // Copyright (C) 2007-2014 Free Software Foundation, Inc.
  140. //
  141. // This file is part of the GNU ISO C++ Library. This library is free
  142. // software; you can redistribute it and/or modify it under the
  143. // terms of the GNU General Public License as published by the
  144. // Free Software Foundation; either version 3, or (at your option)
  145. // any later version.
  146.  
  147. // This library is distributed in the hope that it will be useful,
  148. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  149. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  150. // GNU General Public License for more details.
  151.  
  152. // Under Section 7 of GPL version 3, you are granted additional
  153. // permissions described in the GCC Runtime Library Exception, version
  154. // 3.1, as published by the Free Software Foundation.
  155.  
  156. // You should have received a copy of the GNU General Public License and
  157. // a copy of the GCC Runtime Library Exception along with this program;
  158. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  159. // <http://www.gnu.org/licenses/>.
  160.  
  161. /** @file bits/c++0x_warning.h
  162. * This is an internal header file, included by other library headers.
  163. * Do not attempt to use it directly. @headername{iosfwd}
  164. */
  165.  
  166. #ifndef _CXX0X_WARNING_H
  167. #define _CXX0X_WARNING_H 1
  168.  
  169. #if __cplusplus < 201103L
  170. #error This file requires compiler and library support for the
  171. ISO C++ 2011 standard. This support is currently experimental, and must be
  172. enabled with the -std=c++11 or -std=gnu++11 compiler options.
  173. #endif
  174.  
  175. #endif
Add Comment
Please, Sign In to add comment