irmantas_radavicius

Untitled

Mar 5th, 2026 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // demo: sum of elements in range, validation
  2. // 2026-03-05
  3. // tested on Win with g++
  4.  
  5.  
  6.  
  7. // oldschool
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <ctime>
  11. // error handling
  12. #include <exception>
  13. #include <stdexcept>
  14. // I/O handling
  15. #include <iostream>
  16. #include <sstream>
  17. // containers
  18. #include <vector>
  19.  
  20.  
  21. // configuration
  22. #define LOW 10
  23. #define HIGH 99
  24. #define MAX_SIZE 1000
  25.  
  26. #define COLUMNS 20
  27.  
  28. using namespace std;
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. // value in [low; high] or invalid_argument thrown
  36. void validateRange(int value, int low, int high){
  37.     if(value < low || value > high)
  38.         throw invalid_argument(string("Value in [") + to_string(low) + "; " + to_string(high) + "] required");
  39. }
  40.  
  41. // stream clean or invalid_argument thrown
  42. void validateStream(istream &in){
  43.     if(in.fail()){
  44.         in.clear();
  45.         in.ignore(256, '\n');
  46.         throw invalid_argument("Bad input!");
  47.     }
  48. }
  49.  
  50.  
  51.  
  52. // vector with n random elements in [LOW; HIGH]
  53. vector<int> generate(unsigned n){
  54.     validateRange(n, 1, MAX_SIZE);
  55.     vector<int> v;
  56.     for(int i = 0; i < n; ++i)
  57.         v.push_back(LOW+rand()%(HIGH-LOW+1));
  58.     return v;
  59. }
  60.  
  61. // data to stdout
  62. void print(vector<int> data){
  63.     cout << "Data: ";
  64.     for(int i = 0; i < data.size(); ++i){
  65.         if(i % COLUMNS == 0)
  66.             cout << endl;
  67.         cout << data[i] << " ";
  68.     }
  69.     cout << endl;
  70. }
  71.  
  72.  
  73.  
  74. // sum all from data[a] to data[b]
  75. int computeSum(vector<int> data, int a, int b){
  76.     validateRange(a, 1, data.size());
  77.     validateRange(b, 1, data.size());
  78.     int sum = 0;
  79.     for(int i = a; i <= b; ++i){
  80.         sum += data[i-1];
  81.     }
  82.     return sum;
  83. }
  84.  
  85.  
  86.  
  87. int main(){
  88.     try {
  89.         // init
  90.         srand(time(NULL));
  91.  
  92.         // continue computations
  93.         while(1){
  94.             try {
  95.                 // input size and range for sum
  96.                 int n = 0, a = 0, b = 0;
  97.                 cout << "Enter size n and range [a; b] all in [1; " << MAX_SIZE << "]: ";
  98.                 cin >> n >> a >> b;
  99.                 //cout << cin.good() << cin.fail() << cin.bad() << cin.eof() << endl;
  100.                 validateStream(cin); // throws if invalid
  101.  
  102.                 // proceed to computations
  103.                 vector<int> v = generate(n);
  104.                 print(v);
  105.                 int sum = computeSum(v, a, b); // may throw, try before printing
  106.                 cout << "Sum: " << sum << endl;
  107.  
  108.             } catch (invalid_argument& e){
  109.                 cout << e.what() << endl;
  110.             }
  111.         }
  112.     } catch(...){ // should never happen
  113.         cout << "Uncaught exception!" << endl;
  114.     }
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment