Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. // Philip Fernandez
  2. // Lab01 Question 1
  3. // 8/21/2017
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. // Function declarations
  10. void getInput(int &, int &);
  11. void outputOdd(int, int);
  12. void outputSumOfEven(int, int);
  13. void outputSquare(int, int);
  14. void outputSumOfSquaresOfOdd(int, int);
  15.  
  16.  
  17. int main() {
  18.     // Declare ints for two numbers to be input
  19.     int firstNum, secondNum;
  20.     // Function calls
  21.     getInput(firstNum, secondNum);
  22.     outputOdd(firstNum, secondNum);
  23.     outputSumOfEven(firstNum, secondNum);
  24.     outputSquare(firstNum, secondNum);
  25.     outputSumOfSquaresOfOdd(firstNum, secondNum);
  26.     return 0;
  27. }
  28.  
  29. // getInput takes two arguments by reference.
  30. // This is so we can return values for firstNum and secondNum
  31. // this function also provides a do while loop to validate
  32. // that firstNum < secondNum
  33. void getInput(int &num1, int &num2) {
  34.     cout << "Enter an integer: ";
  35.     cin >> num1;
  36.     do {
  37.         cout << "\nEnter an integer that is greater than " << num1 << ": ";
  38.         cin >> num2;
  39.     } while(num1 >= num2);
  40. }
  41.  
  42. // outputOdd takes two arguments.
  43. // this function displays all of the odd
  44. // integers between firstNum and secondNum inclusive
  45. void outputOdd(int num1, int num2) {
  46.     cout << "\nAll of the odd integers between " <<
  47.         num1 << " and " << num2 << ": ";
  48.     for(int i = num1; i <= num2; i++) {
  49.         if(i % 2 != 0) {
  50.             cout << i << " ";
  51.         }
  52.     }
  53.     cout << "\n";
  54. }
  55.  
  56. // outputSumOfEven takes two arguments.
  57. // this function calculates and displays
  58. // the sum of all even numbers between
  59. // firstNum and secondNum inclusive
  60. void outputSumOfEven(int num1, int num2) {
  61.     int sumOfEven = 0;
  62.     for(int i = num1; i <= num2; i++) {
  63.         if (i % 2 == 0) {
  64.             sumOfEven += i;
  65.         }
  66.     }
  67.     cout << "\nSum of all even integers between " <<
  68.         num1 << " and " << num2 << ": " << sumOfEven << "\n";
  69. }
  70.  
  71. // outputSquare takes two arguments
  72. // this function displays firstNum and every
  73. // consecutive integer after firstNum up to
  74. // 10 integers past firstNum. The integer
  75. // squared is also displayed.
  76. void outputSquare(int num1, int num2) {
  77.     cout << "\n";
  78.     for(int i = num1; (i <= num2) && (i < num1 + 10); i++) {
  79.         cout << i << ": " << i*i << " ";
  80.     }
  81.     cout << "\n";
  82. }
  83.  
  84. // outputSumOfSquaresOfOdd takes two arguments.
  85. // this functions calculates and displays
  86. // the sum of all of the odd numbers squared
  87. // between firstNum and secondNum inclusive
  88. void outputSumOfSquaresOfOdd(int num1, int num2) {
  89.     int sumOfSquaresOfOdd = 0;
  90.     for(int i = num1; i <= num2; i++) {
  91.         if(i % 2 != 0) {
  92.             sumOfSquaresOfOdd += (i*i);
  93.         }
  94.     }
  95.     cout << "\nSum of the square of all odd integers " <<
  96.         "between " << num1 << " and " << num2 << ": " << sumOfSquaresOfOdd << "\n";
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement