Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- P33: Smallest, Largest, Sum, and Average with Random Numbers
- Write a program that generates X random integers Num.
- Num is a random number between 20 to 50.
- X is a random number between 10 to 15.
- Calculate and show the Smallest, Largest, Sum, and Average of those numbers.
- Sample Run:
- Generating 11 random numbers (11 is a random number between 10 to 15)...
- ...11 Random Numbers between 20 to 50: 26, 23, 48, 32, 44, 21, 32, 20, 49, 48, 34
- Sum = 377
- Average = 377 / 11 = 34.3
- Smallest = 21
- Largest = 49
- */
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- using namespace std;
- int sum = 0;
- int x = 10+rand()%6;
- int num = 20+rand()%31;
- int max;
- int largest;
- int smallest;
- int main(void)
- {
- srand(time(NULL));
- {
- while (true){
- int sum = 0;
- int x = 10+rand()%6;
- int num = 20+rand()%31;
- cout<<"Generating " << x << " random numbers (" << x << " is between 10 and 15)... n";
- int max;
- int largest;
- int smallest;
- cout << "..." << x << " Random Numbers between 20 to 50: n";
- largest = num;
- smallest = num;
- sum = sum + num;
- while (x < max - 1)
- {
- cout << num;
- sum = sum + num;
- x++;
- if ( num > largest)
- largest = num;
- if ( num < smallest)
- smallest = num;
- }
- cout << "Largest = " << largest << endl;
- cout << "Smallest = " << smallest << endl;
- cout << "Sum = " << sum << endl;
- cout << "Average = " << sum / max << endl;
- break;
- }
- return 0;
- }
- }
- /*
- SAMPLE RUN
- ==========
- Generating 10 random numbers (10 is between 10 and 15)...
- ...10 Random Numbers between 20 to 50:
- Largest = 29
- Smallest = 29
- Sum = 29
- Average = -14
- Process returned 0 (0x0) execution time : 0.172 s
- Press any key to continue.
- */
- #include <random>
- #include <iostream>
- int main()
- {
- int range_min = 0;
- int range_max = 1000;
- int amount_numbers = 50;
- std::random_device rd;
- std::uniform_int_distribution<int> distribution(range_min, range_max);
- for (int i = 0; i < amount_numbers; i++)
- {
- std::cout << distribution(rd) << "n";
- }
- return 0;
- }
- #include <random>
- #include <algorithm>
- #include <numeric>
- #include <functional>
- #include <iostream>
- #include <algorithm>
- int main()
- {
- int range_min = 0;
- int range_max = 1000;
- int amount_numbers = 50;
- std::random_device rd;
- std::uniform_int_distribution<int> distribution(range_min, range_max);
- std::vector<int> numbers;
- for (int i = 0; i < amount_numbers; i++)
- {
- numbers.push_back(distribution(rd));
- }
- std::sort(numbers.begin(), numbers.end());
- int smallest = numbers.front();
- int biggest = numbers.back();
- int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
- float average = 1.0*sum / amount_numbers;
- return 0;
- }
- // Copyright (C) 2007-2014 Free Software Foundation, Inc.
- //
- // This file is part of the GNU ISO C++ Library. This library is free
- // software; you can redistribute it and/or modify it under the
- // terms of the GNU General Public License as published by the
- // Free Software Foundation; either version 3, or (at your option)
- // any later version.
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // Under Section 7 of GPL version 3, you are granted additional
- // permissions described in the GCC Runtime Library Exception, version
- // 3.1, as published by the Free Software Foundation.
- // You should have received a copy of the GNU General Public License and
- // a copy of the GCC Runtime Library Exception along with this program;
- // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
- // <http://www.gnu.org/licenses/>.
- /** @file bits/c++0x_warning.h
- * This is an internal header file, included by other library headers.
- * Do not attempt to use it directly. @headername{iosfwd}
- */
- #ifndef _CXX0X_WARNING_H
- #define _CXX0X_WARNING_H 1
- #if __cplusplus < 201103L
- #error This file requires compiler and library support for the
- ISO C++ 2011 standard. This support is currently experimental, and must be
- enabled with the -std=c++11 or -std=gnu++11 compiler options.
- #endif
- #endif
Add Comment
Please, Sign In to add comment