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;
- 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] Raise a number n to the xth power.\n"
- << "[2] Display the sum of the squares for the first n terms of a series.\n"
- << "[3] Display the sum of the squares for the first n terms of a series, in reverse.\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 1:
- PrintExponentiatedValue();
- break;
- case 2:
- PrintSeries();
- break;
- case 3:
- PrintSeriesReverse();
- break;
- case 4:
- cout << "Exiting program.\n\n";
- break;
- default:
- cout << "Invalid menu option selected.\n";
- userMenuSelection = 1; //avoids repeating initial instructions
- break;
- }
- } while (userMenuSelection != 4);
- system("pause");
- return 0;
- }
- void PrintExponentiatedValue(void)
- {
- int userValue, userExponent;
- cout << "Enter a base value: ";
- cin >> userValue;
- cout << "Enter an exponent: ";
- cin >> userExponent;
- if (userExponent >= 0)
- {
- printf("%d to the power of %d: %d\n", userValue, userExponent, ExponentiateValue(userValue, userExponent));
- }
- else
- {
- printf("%d to the power of %d: %.2f\n", userValue, userExponent, 1.0 / ExponentiateValue(userValue, abs(userExponent)));
- }
- }
- 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?~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- }
- 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 || base == 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) /////make sure input is positive
- {
- int currentSum;
- if (currentVal == 1) //base case
- {
- cout << currentVal;
- return currentVal * currentVal;
- }
- 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