Topics:
We will start today's lab with a discussion of how a function is called. Then we will use the CodeWarrior Debugger to trace the execution of a function call. Copy the code sample below (lab6Mathfuns.cpp) into a file (yourLastNameLab6Part1) and add it to a CodeWarrior project. Enable the debugger and run the program. Trace the code for each of the menu choices.
Things to note while using the debugger:
How a function is pushed on the stack when called and removed from the stack when execution is complete.
functions may have their own variables that are local to the procedure
function variables may have the same name as variables in main (have different scope)
functions may return a value
a function gets it's own copy of any parameters sent
/*******************************************************/ //Written by R.A. Hillyard //lab6Mathfuns.cpp //Last Modified 10/01/2001 //program to use built in math functions /*******************************************************/ #include<iostream> //needed for cin - cout #include<cmath> //needed for sqrt, pow #include<cstdlib> //needed for abs #include<cctype> //needed for toupper using namespace std; int main() { char choice; bool flag = true; int a,b; double x,y,z; while(flag) { //print menu cout << "\nWelcome to the math function program\n"; cout << "------------------------------------\n"; cout << "Choose one of the following\n"; cout << " S Calculate the square root\n"; cout << " P Calculate x to the y power\n"; cout << " A Calculate the absolute value for integer\n"; cout << " R Generate a random number\n"; cout << " Q To quit\n\n"; cout << "Enter your choice: "; cin >> choice; choice = tolower(choice); //convert choice to lower case //do user request switch(choice) { case 's' : cout << "Enter a number to calculate the square root: "; cin >> x; y = sqrt(x); cout << "The square root of " << x << " is " << y <<endl; break; case 'p' : cout << "Enter x and y to calculate x to the y power: "; cin >> x >> y; z = pow(x,y); cout << x << " to the " << y << " power is " << z <<endl; break; case 'a' : cout << "Enter an integer to calculate the absolute value: "; cin >> a; b = abs(a); cout << "the absolute value of " << a << " is " << b <<endl; break; case 'r' : a = rand(); cout << "Random number = : " << a <<endl; break; case 'q' : cout << "bye for now\n"; flag = false; break; default : cout << choice << " is undefined\n"; }//end switch if(flag) { cout << "Do you want to try again? [Y/N]: "; cin >> choice; if(toupper(choice) == 'N') flag = false; //break would also work } }//end while flag }//end main /***********************************************************/
Write a C++ program (yourLastNameLab6Part2) that displays the following checkerboard pattern:
*********** -*********= --*******== ---*****=== ----***==== -----*=====
Your program may use only four output statements, ONE OF EACH of the following:
cout << "*";
cout << "-";
cout << "=";
cout << endl; or cout <<"\n";
HINT: You will need a outer for loop that prints each line. You'll need a for loop to print the "-"s, another for loop to print the "*"s, and a third for loop to print the "="s. Note that there are the same number of "-"s as "="s on each line, so those two loops will look very similar. You'll need to experiment with the indices. See lab 5 part 2 for a similar problem.
a.) Write a C++ program (yourLastNameLab6Part3) that will compute the area of various shapes or compute the
hypotenuse of a triangle (use the built in functions in cmath
library). Your program should present
the user with a menu of single character choices, get any necessary input to compute the area (or hypotenuse),
do the computation, and report the results to the user. The program should loop until the user chooses to quit.
Use a switch statement to execute the users choice. Allow for upper and lower case menu choices (use the built
in char functions in the cctype
library). Use the program from part one as a sample.
Shape | Formula |
square | area = a2 |
circle | area = pr2 |
sphere | area = (4/3)pr3 |
hypotenuse | c = (a2 + b2)1/2 |
p = 3.141593 |
Sample run: (user input in bold)
Choose one of the following: c for area of circle g for area of sphere s for area of square h for hypotenuse of triangle q to quit
choice > h
enter side a and b > 3 4
hypotenuse of triangle with sides 3 and 4 is 5;
continue [Y/N]> Y
Choose one of the following: c for area of circle g for area of sphere s for area of square h for hypotenuse of triangle q to quit
choice > g
enter radius of sphere > 2.9 the area of a sphere with radius = 2.9 is 102.16
continue [Y/N]> N