Advertisement
COSCI539

CS136 lab2 [Turn in]

Mar 5th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. // Lab 2 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. enum MenuOption { INVLAID = 0, EXPONENTIATE, SERIES, SERIES_REVERSE, EXIT };
  10.  
  11. void PrintExponentiatedValue(void);
  12. void PrintSeries(void);
  13. void PrintSeriesReverse(void);
  14. int ExponentiateValue(int base, int exponent);
  15. int PrintSumOfSquaresSeries(int currentVal, int initialVal);
  16. int PrintSumOfSquaresSeriesReverse(int currentVal, int initialVal);
  17.  
  18. int main(void)
  19. {
  20.     int userMenuSelection = 0;
  21.  
  22.     do
  23.     {
  24.         cout << "\n [1] Exponentiate\n"
  25.              << " [2] Series\n"
  26.              << " [3] Series Reversed\n"
  27.              << " [4] Exit.\n\n";
  28.  
  29.         if (userMenuSelection)
  30.         {
  31.             cout << " Select another option: ";
  32.         }
  33.         else
  34.         {
  35.             cout << " Select an option by entering its corresponding number: ";
  36.         }
  37.  
  38.         cin >> userMenuSelection;
  39.  
  40.         switch (userMenuSelection)
  41.         {
  42.         case EXPONENTIATE:
  43.                 PrintExponentiatedValue();
  44.                 break;
  45.         case SERIES:
  46.                 PrintSeries();
  47.                 break;
  48.         case SERIES_REVERSE:
  49.                 PrintSeriesReverse();
  50.                 break;
  51.         case EXIT:
  52.                 cout << " Exiting program.\n\n";
  53.                 break;
  54.         default:
  55.                 cout << " Invalid menu option selected.\n";
  56.                 userMenuSelection = 1; // 1 == !INVALID
  57.                 break;
  58.         }
  59.     } while (userMenuSelection != EXIT);
  60.  
  61.     system("pause");
  62.     return 0;
  63. }
  64.  
  65. // Menu Option 1 [EXPONENTIATE]
  66. void PrintExponentiatedValue(void)
  67. {
  68.     int userBase, userExponent;
  69.  
  70.     cout << " Enter a base value: ";
  71.     cin  >> userBase;
  72.     cout << " Enter an exponent: ";
  73.     cin  >> userExponent;
  74.  
  75.     if (userExponent >= 0) // positive exponents
  76.     {
  77.         printf(" %d to the power of %d: %d\n", userBase, userExponent
  78.                 , ExponentiateValue(userBase, userExponent));
  79.     }
  80.    
  81.     else if (userBase != 0) // negative exponents, nonzero base
  82.     {
  83.         printf(" %d to the power of %d: %f\n", userBase, userExponent
  84.                 , 1.0 / ExponentiateValue(userBase, abs(userExponent)));
  85.     }
  86.     else // negative exponents, zero base
  87.     {
  88.         printf(" %d to the power of %d: undefined\n", userBase, userExponent);
  89.     }
  90. }
  91.  
  92. // Menu Option 2 [SERIES]
  93. void PrintSeries(void)
  94. {
  95.     int userValue;
  96.  
  97.     cout << " Enter the desired length for the series: ";
  98.     cin  >> userValue;
  99.  
  100.     if (userValue < 1)
  101.     {
  102.         cout << " Value must be positive.\n";
  103.     }
  104.     else
  105.     {
  106.         printf(" First %d elements of the series of squares: ", userValue);
  107.         int dummy = PrintSumOfSquaresSeries(userValue, userValue);
  108.         // is function meant to handle ENTIRE output?~~~~~~~~~~~~~~~~~~~~~~~~~~
  109.     }
  110. }
  111.  
  112. // Menu Option 3 [SERIES_REVERSE]
  113. void PrintSeriesReverse(void)
  114. {
  115.     int userValue;
  116.  
  117.     cout << " Enter the desired length for the series: ";
  118.     cin  >> userValue;
  119.  
  120.     if (userValue < 1)
  121.     {
  122.         cout << " Value must be positive.\n";
  123.     }
  124.     else
  125.     {
  126.         printf(" First %d elements of the series of squares in reverse: ", userValue);
  127.         int dummy = PrintSumOfSquaresSeriesReverse(1, userValue);
  128.         // is function meant to handle ENTIRE output?~~~~~~~~~~~~~~~~~~~~~~~~~
  129.     }
  130. }
  131.  
  132. // Calculates base^exponent
  133. // pre: base and exponent are defined; exponent is positive
  134. // post: base^exponent is returned
  135. int ExponentiateValue(int base, int exponent)
  136. {
  137.     if (exponent == 0)
  138.     {
  139.         return 1;
  140.     }
  141.     else
  142.     {
  143.         return base * ExponentiateValue(base, exponent - 1);
  144.     }
  145. }
  146.  
  147. // Prints the series of squares up to a given number of elements and the sum of said series
  148. // pre: currentVal and actualVal are defined, positive, and equivalent
  149. // post: Series and its sum have been printed
  150. int PrintSumOfSquaresSeries(int currentVal, int initialVal)
  151. {
  152.     int currentSum;
  153.  
  154.     if (currentVal == 1) // base case
  155.     {
  156.         cout << currentVal;
  157.         return 1;
  158.     }
  159.     else if (currentVal == initialVal) // initial case
  160.     {
  161.         currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeries(currentVal - 1, initialVal);
  162.         printf("+(%d*%d)=%d\n", currentVal, currentVal, currentSum);
  163.         return currentSum;
  164.     }
  165.     else // general case
  166.     {
  167.         currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeries(currentVal - 1, initialVal);
  168.         printf("+(%d*%d)", currentVal, currentVal);
  169.         return currentSum;
  170.     }
  171. }
  172.  
  173. // Prints the series of squares up to a given number of elements and the sum of said series, in reverse
  174. // pre: currentVal is 1; actualVal is defined and positive
  175. // post: Series and its sum have been printed
  176. int PrintSumOfSquaresSeriesReverse(int currentVal, int initialVal)
  177. {
  178.     int currentSum;
  179.  
  180.     if (currentVal == initialVal) // base case
  181.     {
  182.         printf("(%d*%d)", currentVal, currentVal);
  183.         return currentVal * currentVal;
  184.     }
  185.     else if (currentVal == 1) // initial case
  186.     {
  187.         currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeriesReverse(currentVal + 1, initialVal);
  188.         printf("+%d=%d\n", currentVal, currentSum);
  189.         return currentSum;
  190.     }
  191.     else // general case
  192.     {
  193.         currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeriesReverse(currentVal + 1, initialVal);
  194.         printf("+(%d*%d)", currentVal, currentVal);
  195.         return currentSum;
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement