/* Name: Zach Hoggard Student No. 250611551 Email: zhoggard@uwo.ca Section: 004 Date: February 15, 2012 */ #include #include using namespace std; int main (void) { //Initialize variables double base(0), result(1), result_2(1); int x(0),y(0), h(1), product(1), exp(0), width(0); char choice (0), letter (0); //Display program discription, menu, and ask for the menu selection cout << "This program uses a switch-based menu system to select a series of options that use for-loops.\n" << "MENU\n" << "A. Use a For-Loop to calculate the factorial, x! (without using cmath)\nB. Use a For-Loop to calculate the power function (without using cmath) y = base^exp\nC. Use a Nested For-Loop to print a sine shape of characters.\nD. Exit\n\n"; //Input menu selection //Verify that the menu selection is valid do { cout << "Please enter your choice: \n"; cin >> choice; } while (!((choice >= 65 && choice <= 68) || (choice >= 97 && choice <= 100))); //Convert choice into integer values if (choice == 65 || choice == 97) { x = 1; } else { if (choice == 66 || choice == 98) { x = 2; } else { if (choice == 67 || choice == 99) { x = 3; } else { x = 4; } } } //Execute menu option selected switch (x) { //Display the discription of the case and ask for positive integer case 1: cout << "You have selected ' Calculate x! '\n Please enter a positive integer:\n"; //Input the factorial to be calculated cin >> product; //Verify thata the product is a positive integer while (!(product >= 0)) { cout << "Please enter a positive integer: \n"; cin >> product; } //Calculate the factorial for (y = product; y >= 1; y--) { result *= y; } //Display the result of the factorial cout << "The value for " << product << "! is: " << result; break; //Display the case discription and ask for base and exponent case 2: cout << "You have selected to calculate the power function y = base^exponent\nPlease enter the base: \n"; cin >> base; cout << "Please enter the exponent (Integer): \n"; cin >> exp; //Verify that the exponent is positive while (!(exp >= 0)) { cout << "The exponent should be positive. Please re-enter the exponent: \n"; cin >> exp; } //Calcuate the exponent for (h = 1; h <= exp ; h++) { result_2 *= base; } //Display the result cout << "The value of " << base << " ^ " << exp << " = " << result_2; break; //Display the case discription and ask for character type and width of sine case 3: cout << "You have selected to print a sine shape of characters.\nPlease enter the character you wish to use: "; cin >> letter; cout << "Please enter the width of the sine: "; cin >> width; //Make sine curve for (int i = 0; i <= width; i++) { cout << endl; for (int j = 0; j <= (width *sin(i*3.14159/width)); j++) { cout << letter; } } break; //End Program case 4: return 0; } }