fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. class EOF_Exception : public exception {};
  6.  
  7. // Input an integer in a loop until given a valid number
  8. int inputInteger(const char* message){
  9. int n;
  10.  
  11. while(1){
  12. cout << message;
  13. cin >> n;
  14.  
  15. // Throw exception if end-of-file (EOF) is given on input
  16. if(cin.eof()) throw EOF_Exception();
  17.  
  18. // Break this loop only the input is valid, otherwise print and error and redo.
  19. if(!cin.fail()) break;
  20. cout << "ERROR: Invalid integer input" << endl;
  21. }
  22.  
  23. return n;
  24. }
  25.  
  26. // A struct to hold the bounds and length() for the number of ..numbers.. in the range.
  27. typedef struct {
  28. int lower;
  29. int upper;
  30.  
  31. int length(){ return upper - lower + 1; }
  32. } Range;
  33.  
  34. // Inputs the bounds in a loop until valid bounds are given (lower <= upper)
  35. Range inputRange(){
  36. Range range;
  37.  
  38. while(1){
  39. range.lower = inputInteger("Enter lower limit: ");
  40. range.upper = inputInteger("Enter upper limit: ");
  41.  
  42. // Break this loop only the range is valid, otherwise print and error and redo.
  43. if(range.lower <= range.upper) break;
  44. cout << "ERROR:" "lower bound (" << range.lower << ") is greater than " \
  45. "upper bound (" << range.upper << ")" << endl;
  46. }
  47.  
  48. return range;
  49. }
  50.  
  51. // Iterates the given range and performs the summation + count of even numbers
  52. // NOTE: Calculating the even/odd numbers can be done directly from the bounds without a loop
  53. // which is probably better. I didn't bother, but you can lol =)
  54. // NOTE: Printing is also done here for simplicity, but it is preferable to avoid mixing
  55. // "user interface" operations (like printing to console) with "business logic" (our algorithm).
  56. void iterate_range(Range range){
  57. int sum = 0, num_odds = 0;
  58.  
  59. // NOTE: I thought of making a custom iterator to make this more elegant, but for
  60. // simplicity I've left it the way it is
  61. for(int n = range.lower; n <= range.upper; ++n){
  62. sum += n;
  63. if(sum % 2) ++num_odds;
  64.  
  65. // Printing the sum, spaces are added only in between (it annoys me otherwise)
  66. if(n != range.lower) cout << " ";
  67. cout << sum;
  68. }
  69. // Print a new line character at the end of the lsit of sums printed in the loop.
  70. cout << endl;
  71.  
  72. cout << "Even nubmers: " << range.length() - num_odds << endl;
  73. cout << "Odd numbers: " << num_odds << endl;
  74. }
  75.  
  76. // The entry point to the program.
  77. int main(int argc, char **argv){
  78. try{
  79. Range range = inputRange();
  80. iterate_range(range);
  81. }
  82. catch(EOF_Exception& e){
  83. return 1;
  84. }
  85.  
  86. return 0;
  87. }
  88.  
Success #stdin #stdout 0s 3420KB
stdin
3
15
stdout
Enter lower limit: Enter upper limit: 3 7 12 18 25 33 42 52 63 75 88 102 117
Even nubmers: 6
Odd numbers: 7