Lab-5 - Writing and Calling Functions


Part-1 Follow along on the big screen to learn about functions. Write your programs in your 2010/5/ Odin directory. Follow along with Gordon.
Part-2 Start with your menu.cpp program from week-3 lab. Copy the program to your 2010/5 directory as menu2.cpp Refactor your program so that each of your menu items calls a function. Your menu code will be simplified, and your program modular. Examples: 1. calculate area of a rectangle -------------------------------- suggested function prototype float get_rectangle_area(float width, float height); Display the result from your menu, not from the function. 2. calculate area of a triangle ------------------------------- possible function prototype float get_triangle_area(float base, float height); Display the result from your menu, not from the function. 3. calculate surface area of a sphere ------------------------------------- You do this one. 4. calculate interest earned ---------------------------- sample function prototype double get_savings_balance(double principal, double rate, int T); Display the result from your menu, not from the function. 5. calculate monthly payments ----------------------------- possible function prototype double get_monthly_payments(double rate, double L, int N); Display the result from your menu, not from the function. 6. Your chosen menu item from the book -------------------------------------- 7. You may add to your menu... ------------------------------ Your order3 program. -------------------- Send your 3 numbers to your function. Display the output from the function. Your pyramid program. --------------------- Send the height to your function. Display the output from the function. Your summation program. ----------------------- Send n to your function. Display the result from the menu. 8. Print your menu text from a void function -------------------------------------------- Call this function from main. It will print your menu to the screen.
Extra points for writing this program yourself Name this program: twopys.cpp Your program will draw two pyramids side-by-side. Ask the user for the pyramid heights. Program output looks like:
How high are your pyramids? 6 * * *** *** ***** ***** ******* ******* ********* ********* *********** ***********
Hint: Define the following variables int height; int left_gap = height - 1; int mid_gap = height * 2; int stars = 1; Enter height. Write an outer for-loop that iterates height times Write 4 inner for-loops to draw the spaces and stars. Update the variable values just after the inner loops. The number of spaces changes, and the number of stars changes.