Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab 2 Wilson, Drayden T Th
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- enum MenuOption { INVLAID = 0, EXPONENTIATE, SERIES, SERIES_REVERSE, EXIT };
- void PrintExponentiatedValue(void);
- void PrintSeries(void);
- void PrintSeriesReverse(void);
- int ExponentiateValue(int base, int exponent);
- int PrintSumOfSquaresSeries(int currentVal, int initialVal);
- int PrintSumOfSquaresSeriesReverse(int currentVal, int initialVal);
- int main(void)
- {
- int userMenuSelection = 0;
- do
- {
- cout << "\n [1] Exponentiate\n"
- << " [2] Series\n"
- << " [3] Series Reversed\n"
- << " [4] Exit.\n\n";
- if (userMenuSelection)
- {
- cout << " Select another option: ";
- }
- else
- {
- cout << " Select an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection;
- switch (userMenuSelection)
- {
- case EXPONENTIATE:
- PrintExponentiatedValue();
- break;
- case SERIES:
- PrintSeries();
- break;
- case SERIES_REVERSE:
- PrintSeriesReverse();
- break;
- case EXIT:
- cout << " Exiting program.\n\n";
- break;
- default:
- cout << " Invalid menu option selected.\n";
- userMenuSelection = 1; // 1 == !INVALID
- break;
- }
- } while (userMenuSelection != EXIT);
- system("pause");
- return 0;
- }
- // Menu Option 1 [EXPONENTIATE]
- void PrintExponentiatedValue(void)
- {
- int userBase, userExponent;
- cout << " Enter a base value: ";
- cin >> userBase;
- cout << " Enter an exponent: ";
- cin >> userExponent;
- if (userExponent >= 0) // positive exponents
- {
- printf(" %d to the power of %d: %d\n", userBase, userExponent
- , ExponentiateValue(userBase, userExponent));
- }
- else if (userBase != 0) // negative exponents, nonzero base
- {
- printf(" %d to the power of %d: %f\n", userBase, userExponent
- , 1.0 / ExponentiateValue(userBase, abs(userExponent)));
- }
- else // negative exponents, zero base
- {
- printf(" %d to the power of %d: undefined\n", userBase, userExponent);
- }
- }
- // Menu Option 2 [SERIES]
- void PrintSeries(void)
- {
- int userValue;
- cout << " Enter the desired length for the series: ";
- cin >> userValue;
- if (userValue < 1)
- {
- cout << " Value must be positive.\n";
- }
- else
- {
- printf(" First %d elements of the series of squares: ", userValue);
- int dummy = PrintSumOfSquaresSeries(userValue, userValue);
- // is function meant to handle ENTIRE output?~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- }
- // Menu Option 3 [SERIES_REVERSE]
- void PrintSeriesReverse(void)
- {
- int userValue;
- cout << " Enter the desired length for the series: ";
- cin >> userValue;
- if (userValue < 1)
- {
- cout << " Value must be positive.\n";
- }
- else
- {
- printf(" First %d elements of the series of squares in reverse: ", userValue);
- int dummy = PrintSumOfSquaresSeriesReverse(1, userValue);
- // is function meant to handle ENTIRE output?~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- }
- // Calculates base^exponent
- // pre: base and exponent are defined; exponent is positive
- // post: base^exponent is returned
- int ExponentiateValue(int base, int exponent)
- {
- if (exponent == 0)
- {
- return 1;
- }
- else
- {
- return base * ExponentiateValue(base, exponent - 1);
- }
- }
- // Prints the series of squares up to a given number of elements and the sum of said series
- // pre: currentVal and actualVal are defined, positive, and equivalent
- // post: Series and its sum have been printed
- int PrintSumOfSquaresSeries(int currentVal, int initialVal)
- {
- int currentSum;
- if (currentVal == 1) // base case
- {
- cout << currentVal;
- return 1;
- }
- else if (currentVal == initialVal) // initial case
- {
- currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeries(currentVal - 1, initialVal);
- printf("+(%d*%d)=%d\n", currentVal, currentVal, currentSum);
- return currentSum;
- }
- else // general case
- {
- currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeries(currentVal - 1, initialVal);
- printf("+(%d*%d)", currentVal, currentVal);
- return currentSum;
- }
- }
- // Prints the series of squares up to a given number of elements and the sum of said series, in reverse
- // pre: currentVal is 1; actualVal is defined and positive
- // post: Series and its sum have been printed
- int PrintSumOfSquaresSeriesReverse(int currentVal, int initialVal)
- {
- int currentSum;
- if (currentVal == initialVal) // base case
- {
- printf("(%d*%d)", currentVal, currentVal);
- return currentVal * currentVal;
- }
- else if (currentVal == 1) // initial case
- {
- currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeriesReverse(currentVal + 1, initialVal);
- printf("+%d=%d\n", currentVal, currentSum);
- return currentSum;
- }
- else // general case
- {
- currentSum = (currentVal * currentVal) + PrintSumOfSquaresSeriesReverse(currentVal + 1, initialVal);
- printf("+(%d*%d)", currentVal, currentVal);
- return currentSum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement