The following steps will incrementally build a program using a void function and
ID: 3538852 • 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 correct the 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
//POSTING BOTH PROGRAMS BELOW
// FIRST PROGRAM, ACCORDING TO INSTRUCTIONS
#include <iostream>
#include <string>
using namespace std;
//This is a function prototype
void printMessage();
double averageThree(int, int, int);
int main() {
//substitute your name here in place of FirstName LastName
cout<<"Functions Lab by FirstName LastName"<<endl;
//This is a call to the void function printMessage
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;
//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;
//OK, now this is the last call to averageThree
//answer = averageThree(10, 20, 30.5);
//CORRECT STATEMENT, above sends a double (30.5) to an integer
answer = averageThree(10, 20, 30);
system("pause");
return 0;
}
//This is a function definition
void printMessage( )
{
cout << "This message is brought to you" << endl;
cout << "by the printMessage function." << endl << endl;
}
//Value-returning function definition
double averageThree(int x, int y, int z)
{
double ave;
ave = (x + y + z) / 3.0;
return ave;
}
//-----------------------------------------------------------------------------------------------------------------
//SECOND PROGRAM labgpa.cpp
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//This is a function prototype
double calculateGPA(double,int);
void printGPA (string, double);
int main() {
//substitute your name here in place of FirstName LastName
cout<<"labgpa.cpp by FirstName LastName"<<endl;
string name1,name2;
int cred1,cred2;
double qlty1,qlty2;
//get values from user
cout<<"Enter details for student 1"<<endl;
cout<<"Enter name: ";
getline(cin,name1);
cout<<"Enter total credits: ";
cin>>cred1;
cout<<"Enter total quality points: ";
cin>>qlty1;
fflush(stdin);
cout<<"Enter details for student 2"<<endl;
cout<<"Enter name: ";
getline(cin,name2);
cout<<"Enter total credits: ";
cin>>cred2;
cout<<"Enter total quality points: ";
cin>>qlty2;
//output details by using functions
printGPA(name1,calculateGPA(qlty1,cred1));
cout<<endl;
printGPA(name2,calculateGPA(qlty2,cred2));
system("pause");
return 0;
}
//This is a function definition
double calculateGPA(double quality,int credits)
{
return (quality/credits) ;
}
//print formatted output
void printGPA (string name, double gpa)
{
cout<<fixed<<"Name: "<<name<<" 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.