Advertisement
COSCI539

CS136 Lab2 [4]

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