The following steps will incrementally build a program using a void function and
ID: 3538820 • Letter: T
Question
The following steps will incrementally build a program using a void function and a value-returning function.
1.
Create a main function that simply prints one line of output in the console window - this line should read "Functions Lab by FirstName LastName". Remember the pause statement, and compile and run this code.
2.
After your main function (after the last curly brace), add the following void function. This is a simple function with no parameters that prints a message on the screen:
//This is a function definition
void printMessage( )
{
cout << "This message is brought to you" << endl;
cout << "by the printMessage function." << endl << endl;
}
3.
Next, add the following statements before the pause statement in your main function:
//This is a call to the void function printMessage
printMessage() ;
4. Compile your code as it exists. You should get a compile error! Why? Because your function definition is placed after the main function, the compiler doesn't understand what "printMessage" means when compiling your main. How should you fix this? Put a prototype of the printMessage function before your main function. The prototype is just the heading of the function, followed by a semi-colon, such as:
//This is a function prototype
void printMessage( );
Make this change, recompile, and see your void function work!
5. Now, create a value-returning function that accepts 3 integer values as arguments. This function will find the average of these values and return this average as a double value. Add this code at the end of your source file, after the last curly brace :
//Value-returning function definition
double averageThree(int x, int y, int z)
{
double ave;
ave = (x + y + z) / 3.0;
return ave;
}
6. Compile this code to make sure you do not have any syntax errors. Now, add a call to this function in your main, just after your printMessage call:
//Call to value-returning function averageThree
double answer;
answer = averageThree(10, 5, 6);
cout << "Average of 10, 5, and 6 is " << answer;
Compile this code again. Got a problem? You should again have an error, because we forgot to put the prototype for this function! Go ahead and put this prototype where it belongs, just below your prototype for the printMessage function. Compile and run your code.
7. To demonstrate using variables as arguments when calling your function, add the following code to your main method:
//Call to value-returning function averageThree, using variables as arguments
int num1 = 15;
int num2 = 16;
int num3 = 17;
answer = averageThree(num1, num2, num3);
cout << "Average of " << num1 << ", " << num2 << ", and" << num3 << " is " << answer;
cout << endl << endl;
Again, compile and run this code. Notice that the actual parameters that you send to the function do not have the same name as the formal parameters in the averageThree function definition. This is important - the formal parameters are "placeholders" for the values that are passed, and the names of these are only relevant inside of the averageThree function.
8. You can also send expressions to a function. Add the following code:
//Call to averageThree again, using different arguments
answer = averageThree(100, num1 * 2, num2 + num3);
cout <<"Average of weird arguments is " << answer << endl << endl;
Compile and run this again, and verify that your average is correct.
9. Add this last call to the averageThree function to your code:
//Last call to averageThree
answer = averageThree(10, 20);
Compile this code - you should again get an error with this statement. Why? Because this function requires 3 integer parameters, and you only gave it 2! Just for fun, you can also try:
//OK, now this is the last call to averageThree
answer = averageThree(10, 20, 30.5);
Can you guess what the issue is here?
10. Once you've finished these steps, compile and run your program. You can either correctthe problem statements you entered in step #9, or comment them out.
11. As your final step for this lab (and in lieu of answering a bunch of questions!), create a new cpp file named labgpa.cpp. Add documentation, of course. For this program, create the following functions:
Remember to document each major section of your code. You'll also need to remember your prototypes! Compile and run your code.
Explanation / Answer
1-10.
#include <iostream>
using namespace std;
//4.
///////////////////////////////
void printMessage();
///////////////////////////////
double averageThree(int x, int y, int z);
////1.
///////////////////////////////
int main(){
//2.
///////////////////////////////
printMessage();
///////////////////////////////
//Call to value-returning function averageThree
double answer;
answer = averageThree(10, 5, 6);
cout << "Average of 10, 5, and 6 is " << answer;
cout << endl << endl;
//Call to value-returning function averageThree, using variables as arguments
int num1 = 15;
int num2 = 16;
int num3 = 17;
answer = averageThree(num1, num2, num3);
cout << "Average of " << num1 << ", " << num2 << ", and" << num3 << " is " << answer;
cout << endl << endl;
//Call to averageThree again, using different arguments
answer = averageThree(100, num1 * 2, num2 + num3);
cout <<"Average of weird arguments is " << answer << endl << endl;
cout << endl << endl;
//OK, now this is the last call to averageThree
answer = averageThree(10, 20, 30.5);
/// print statement was not included
cout <<"Step 9 Average " << answer << endl << endl;
cout << " Functions Lab by Firstname Lastname" << endl;
return 0;
}
///////////////////////////////
//This is a function definition
//2.
///////////////////////////////
void printMessage()
{
cout << "This message is brought to you" << endl;
cout << "by the printMessage function." << endl << endl;
}
////////////////////////////////
//5.
/////////////////////////////////////////////
//Value-returning function definition
double averageThree(int x, int y, int z)
{
double ave;
ave = (x + y + z) / 3.0;
return ave;
}
///////////////////////////////////////////////
11.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double calculateGPA(double totalquality, int totalcredit);
void printGPA(string lastname, double gpa);
int main(){
string student1;
string student2;
int totalcredits1;
int totalcredits2;
double totalquality1;
double totalquality2;
double gpa;
cout << "What is student #1 lastname: " << endl;
cin >> student1;
cout << "What is student #1 total credit hours: " << endl;
cin >> totalcredits1;
cout << "What is student #1 total quality points: " << endl;
cin >> totalquality1;
cout << "What is student #2 lastname: " << endl;
cin >> student2;
cout << "What is student #2 total credit hours: " << endl;
cin >> totalcredits2;
cout << "What is student #2 total quality points: " << endl;
cin >> totalquality2;
gpa = calculateGPA(totalquality1, totalcredits1);
printGPA(student1, gpa);
gpa = calculateGPA(totalquality2, totalcredits2);
printGPA(student2, gpa);
}
double calculateGPA(double totalquality, int totalcredit)
{
double gpa = totalquality/totalcredit;
return gpa;
}
void printGPA(string lastname, double gpa)
{
cout << "Student: " << lastname << " " << endl;
cout << "GPA " << setprecision(2) << gpa << " " << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.