Question: Step 1: Type the following program and add it to the Lab9 project. See the function call example discussed in the class (maximum) for the

Step 1: Type the following program and add it to the Lab9 project. See the function call example discussed in the class (maximum) for the proper syntax.

12 /***** main *****/ 13 int main() 14 { int value = 2; 15 16 cout << "Hello from main. "; 17 printMessage(); 18 19 cout << " Value returned by tripleIt is " 20 << tripleIt(value) << endl; 21 cout << "In main value now is " 22 << value << endl << endl; 23 24 value = tripleIt(value); 25 cout << "In main value now is " 26 << value << endl; 27 28 value = tripleIt(value); 29 cout << "In main value now is " 30 << value << endl << endl; 31 32 cout << "Goodbye from main. "; 33 return 0; 34 } 35 36 /***** printMessage *****/ 37 void printMessage() 38 { 39 cout << "Hello from PrintMessage. "; 40 } 41 42 /***** tripleIt *****/ 43 int tripleIt(int someNum) 44 { 45 return someNum * someNum * someNum; 46 }

Expected Output

Observed Output

Step 2: Read the source code, paying special attention to the flow of control from main to the functions it calls and then back to main again. Notice what main passes to each function and what, if anything, the function return. Once you have done this, complete the Expected Output box in the table above, writing down what the program will display in the order it will be displayed.

Step 3: Now compile and run the above program, and look at the output it creates. If the actual output matches what you wrote down, just place a checkmark in the Observed Output box. If it is not the same, write down the actual output.

Lab 8: Functions

Lab Exercise: Please complete the following exercise during your lab session.

One of the problems that have been discussed in the class is to write a simple C++ program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following:

int main() 
{ 
 double radius; //the radius of the circle 
 double area; //the area of the circle 
 double circumference; //the circumference of the circle 
 
 //get the value of the radius from the user 
 radius = getRadius(); 
 
 //determine the area and circumference 
 area = findArea(radius); 
 circumference = findCircumference(radius); 
 
 //output the results 
 cout << "A circle of radius " << radius << " has an area of: " << area < 
 cout << "and a circumference of: "<< circumference << endl; 
 
 system("PAUSE"); 
 return 0; 
} 

Your job will be to write these three functions:

1)getRadius

This function should ask the user to enter the value for the radius and return that value.

2)findArea

This function should take the radius as a parameter and calculate and return the area.

(Hint: area = 3.14159*radius*radius)

3)findCircumference

This function should take the radius as a parameter and calculate and return the circumference.

(Hint: circumference = 2*3.14159*radius)

OPTIONAL: Use prototypes for your functions. Place the prototypes before main (but after the using namespace std; line) and place the actual functions after the main function.

A sample run of your program should look like this (bold bracketed text stands for user input):

Enter the radius of the circle: [3.5]

A circle of radius 3.5 has an area of: 38.4845

and a circumference of: 21.9911

Press any key to continue . . .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!