Make your own free website on Tripod.com

Tutorial 7

Program-Defined Void Functions

 

The instructor’s notes for this tutorial will consist of the following:

Overview

In tutorial 6 the concepts involved with a value-returning function were covered. In this tutorial the concept and implementation of a void function will be covered. In addition, the concept of passing information to a function by reference will be introduced. Contrast will also be made with value-returning vs. void functions, as well as passing by value vs. passing by reference.

 

Objectives

After completing the tutorial, the student will be able to:

 

Lecture Notes

Defining a void function

When defining a void function you must code the function header and the function body. The function header is the first statement in the function definition. It specifies the name of the function, and any optional parameters the function may have. The following is the syntax for a void function header: void functionName(LEFTSQUAREparameterListRIGHTSQUARE);. Note that the rules for naming functions are the same as the rules for naming variables.

Note the difference between the function header of a value-returning function vs. the function header of a void function. The only difference between the two is that the header in a void function begins with the keyword void, where the header in a value-returning function begins with the return data type of the function.

After coding the function header the function body must be coded. The function body will contain the instructions necessary for the function to accomplish its task. The function body will begin with an opening brace ( { ) and end with a closing brace ( } ). Unlike a value-returning function, a void function does not contain a return statement. This is due to the fact that a void function does not return a value.

 

Review of Parameters

You once again probably noted the term parameter in the syntax for the function header. When you were calling built-in functions, you were including the actual arguments in the parentheses after the function name (see Tutorial 5 for review if necessary). Another term for the actual arguments could be actual parameters. When calling a built-in function you only needed to concern yourself with how many arguments (parameters) to pass to the function, because some other programmer has already defined the function. When this programmer defined the function, he/she defined it similar to the way we are now, and included a set of parameters (if any were needed) in his/her function header. These parameters correspond to the set of actual parameters when the function is called, and are called formal parameters.

When listing the actual parameters in the function call you need to only include the values (whether they are variables, named constants, literals, etc.) you wish to pass to the function, because these values have already been defined in the program. However when we are defining the function header, the formal parameters we are using (that match the actual parameters) have not yet been defined anywhere in the program. Therefore when listing them in the function header, we must also include their corresponding data type. This is coded similarly to declaring a variable where the data type is followed by the variable name (see page 190 for examples). If the function you are defining requires more than one formal parameter, then each formal parameter’s data type and name are separated by a comma (see page 190 for an example). Note that some functions will not require parameters. In this case an empty set of parentheses is coded.

One very important item to note with the relationship between actual and formal parameters is that they must match in three ways: number, type, and order. In other words, if there are three formal parameters, there must also be three actual parameters. If the first formal parameter is of the type int, then the last two are of the type float, then the corresponding actual parameters must be the same corresponding types. Finally, the computer does not understand your problem and cannot match parameters for you. Therefore if you code as the first formal parameter an id code, followed by a salary and tax amount as the second and third, you must list your actual parameters in same exact order.

Another important item to note with the relationship between actual and formal parameters is naming them. If one person were writing an entire program, and had declared and passed a single variable called age through a function call to a function called displayAge, would it be possible to name the corresponding formal parameter age also? The answer would be yes. While many situations may not lend itself to this situation, it would be quite desirable if you were writing the entire program, because you would not have two different names for the same value. However, if you asked a colleague to write the displayAge function for you, more than likely the formal parameter name chosen would not be the same as your actual parameter name in the main function.

Calling a program-defined void function

As stated previously, you call a program-defined function the same way you call a built-in function. When the computer processes a statement containing a program-defined function, the computer first locates the function’s code in the program. In C++ the code for all program-defined functions is usually placed after the main function. Remember from the previous discussion that if the program-defined function has any parameters (formal parameters), then the actual parameters included in the function call must match in three ways: number, type, and order.

Since in C++ you may not use functions until they are defined (built-in functions are defined when you issue the #include directive for the library containing the function), we must somehow define the program-defined function before the main function calls it. This is accomplished by placing a function prototype in the program. Function prototypes are usually placed at the beginning of the program, after the #include directives.

The main difference in calling a void function vs. calling a value-returning function is that a call to a void function is a statement by itself. Remember that a call to a value-returning function results in a value being passed back to the statement that calls the value-returning function. Since a void function does not return a value, a call to a void function is not part of another statement.

Function prototypes

You declare a program-defined function by using a function prototype. The function prototype for a void function contains the keyword void, the name of the function, and the data type of each of the formal parameters. The function prototype is usually placed at the beginning of the program, after the #include directives, but before the beginning of the main function.

The function prototype alerts the compiler that the function will be defined in its entirety later in the program, however satisfies the rule that a function must be declared before it is called. This way the compiler knows the basic information about a function (number of parameters, their types, name of the function, etc.), so when a function call is made it may have some information to compare to.

Note that the function prototype ends with a semicolon where the function header does not end with a semicolon.

Passing parameters by reference

You learned previously that the actual parameters that can be passed to a function’s corresponding formal parameters can be one or more variables, named constants, literal constants, or keywords. When the actual parameter is a variable, the variable can either be passed by value or passed by reference. When a variable is passed by value, only the value stored in the variable is passed to the function. When a variable is passed by reference, the address of the variable in memory is passed. Unless otherwise specified, C++ automatically passes variables by value.

You want to pass a variable by reference when you would like the receiving function to have access to the contents of the variable being passed, therefore possibly changing the value stored in the variable. To pass a variable by reference in C++, you simply include an ampersand (&), called the address-of operator, before the name of the corresponding formal parameter in the function header. The address-of operator will tell the computer to pass the variable’s address rather than its contents.

Note that if the function is defined after the main function in the program, which is usually the case, then the function prototype must also be modified. In order to modify the function prototype to correspond to the function definition, you must code an ampersand one space after the data type that corresponds to the formal parameter you wish to pass by reference. See page 233 of the text for an example.

  

Solutions to Questions

  1. c A void function cannot receive any items of information when it is called.
  2. d display Heading(companyName);
  3. a void calcEndingBalance(int, int);
  4. d void calcEndingInventory(int b, int s, int p, int &e)
  5. c calcEndingInventory(beginInventory, sales, purchases, endingInventory);
  6. d by value
  7. c by reference
  8. c function header, function prototype
  9. b The function header is considered a C++ statement, so it must end with a semicolon.
  10. d displayName();
  11. c void displayName();
  12. c immediately below the statement that called the function
  13. a True
  14. b local
  15. d To pass a variable by reference in C++, you place an ampersand (&) before the variable’s name
  16. in the statement that calls the function.

  17. c void calcNewPrice(float, float &);
  18. d calcNewPrice(oldPrice, newPrice);
  19. a The names of the formal parameters in the function header must be identical to the names of the

actual arguments in the function call.

 

Solutions to Concept Lesson Exercises

The answers to Exercises 1 through 6 may vary.

  1. void halveNumber(int num)
  2. {

    num / 2;

    cout << num << endl;

    } //end of halveNumber function

     

  3. void getChar(char &character)
  4. {

    cout << "Enter a character: ";

    cin >> character;

    } //end of getChar function

     

  5. void calcSumAndDiff(int n1, int n2, int &n3, int &n4)
  6. {

    n3 = n1 + n2;

    n4 = n1 – n2;

    } //end of calcSumAndDiff function

     

  7. void quotient(float n1, float n2, float &n3)
  8. {

    n3 = n1 / n2;

    } //end of quotient function

     

  9. void getName(string &name)
  10. {

    cout << "Enter a name: ";

    getline(cin, name);

    } //end of getName function

     

  11. void getRandomNumber(int low, int high, int &num)

{

num = low + rand() % (high – low + 1);

} //end of getRandomNumber function

 

Desk-check table after the first four instructions in the main function are processed:

 


begVal

purchase

sale

endVal

1000

500

200

0

 

Desk-check table after the calcEnd function’s ending = beg + pur – sale; statement is processed:

begVal

purchase

sale

 

endVal (main)

ending (calcEnd)

 

beg

pur

sale

1000

500

200

 

0

1300

1000

500

200

Desk-check table after the calcEnd function ends

 


begVal

purchase

sale

endVal

1000

500

200

0

1300

 

8.

//T7ConE08.cpp - displays the name entered by the user

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getName(string &);

int main()

{

//declare variable

string name = "";

//get name

getName(name);

//display name

cout << "You entered " << name << endl;

return 0;

} //end of main function

//*****program-defined functions*****

void getName(string &inputName)

{

cout << "Enter a name: ";

getline(cin, inputName);

} //end of getName function

 

9. Changes to the original code are shaded.

//T7ConE09.cpp - displays the name entered by the user

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getName(string &);

int main()

{

string name = "";

//enter name

getName(name);

//display name

cout << "You entered " << name << " as the name." << endl;

return 0;

} //end of main function

//*****program-defined functions*****

void getName(string &nameInput)

{

cout << "Customer name: ";

getline(cin, nameInput);

} //end of getName function

10. Changes made to the original code are shaded.

/T7ConE10.cpp - displays the sum of two numbers

#include <iostream>

using namespace std;

//function prototypes

void getNumber(int &);

void calcSum(int, int, int &);

int main()

{

int num1 = 0;

int num2 = 0;

int num3 = 0;

//enter input items

getNumber(num1);

getNumber(num2);

//calculate sum

calcSum(num1, num2, num3);

//display sum

cout << "Sum: " << num3 << endl;

return 0;

} //end of main function

//*****program-defined functions*****

void getNumber(int &num)

{

cout << "Enter a number: ";

cin >> num;

} //end of getNumber function

void calcSum(int n1, int n2, int &n3)

{

n3 = n1 + n2;

} //end of calcSum function

 

 

Solutions to Application Lesson Exercises

 

1.

//T7AppE01.cpp - displays a water bill

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, int &, int &);

void displayBill(string, int, float);

void calcGallons(int, int, int &);

void calcCharge(int, float, float &);

int main()

{

const float RATE = float(.00175);

string name = "";

int current = 0;

int previous = 0;

int gallons = 0;

float charge = 0.0;

//enter input items

getInput(name, current, previous);

//calculate gallons used and water charge

calcGallons(current, previous, gallons);

calcCharge(gallons, RATE, charge);

//display water bill

displayBill(name, gallons, charge);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &cust, int &cur, int &prev)

{

cout << "Customer name: ";

getline(cin, cust);

cout << "Current reading: ";

cin >> cur;

cout << "Previous reading: ";

cin >>prev;

} //end of getInput function

void displayBill(string cust, int used, float amtDue)

{

cout << fixed;

cout.precision(2);

cout << "Customer name: " << cust << endl;

cout << "Gallons used: " << used << endl;

cout << "Water charge: " << amtDue << endl;

} //end of displayBill function

 

 

void calcGallons(int cur, int prev, int &gal)

{

gal = cur - prev;

} //end of calcGallons function

void calcCharge(int gal, float waterRate, float &due)

{

due = gal * waterRate;

} //end of calcCharge function

 

2.

IPO charts:

main function

Input

Processing

Output

name

weekly salary

FWT rate (.2)

FICA rate (.08)

 

Processing items: none

Algorithm:

  1. getInput(name, weekly salary)
  2. calcFedTaxes(weekly salary, FWT rate, FICA rate, FWT, FICA)
  3. calcNetPay(weekly salary, FWT, FICA, weekly net pay)
  4. displayInfo(name, FWT, FICA, weekly net pay)

name

FWT

FICA

weekly net pay

getInput function

Input

Processing

Output

address of the name

address of the weekly salary

 

Processing items: none

Algorithm:

  1. enter the name and weekly salary

name

weekly salary

calcFedTaxes function

Input

Processing

Output

weekly salary

FWT rate

FICA rate

address of the FWT

address of the FICA

Processing items: none

Algorithm:

  1. calculate the FWT by multiplying the weekly salary by the FWT rate
  2. calculate the FICA by multiplying the weekly salary by the FICA rate

FWT

FICA

 

calcNetPay function

Input

Processing

Output

weekly salary

FWT

FICA

address of the weekly net pay

Processing items: none

Algorithm:

  1. calculate the weekly net pay by subtracting the FWT and FICA from the weekly salary

weekly net pay

 

displayInfo function

Input

Processing

Output

FWT

FICA

weekly net pay

Processing items: none

Algorithm:

  1. display the name, FWT, FICA, and weekly net pay

FWT

FICA

weekly net pay

 

Desk-check tables: Unless specified otherwise, variables belong to the main function only.

FWT_RATE

FICA_RATE

name (main)

employee (getInput)

salary (main)

weekPay (getInput)

.2

.2

.08

.08


Samuel Montez


Barbara Jacks

0.0

500.0

0.0

650.0

fwtTax (main)

fwt (calcFedTaxes)

ficaTax (main)

fica (calcFedTaxes)

netPay (main)

net (calcNetPay)

0.0

100.0

0.0

130.0

0.0

40.0

0.0

52.0

0.0

360.0

0.0

468.0

calcFedTaxes function only

weekPay

rate1

rate2

500.0

650.0

.2

.2

.08

.08

calcNetPay function only

weekPay

fwt

fica

500.0

650.0

100.0

130.0

40.0

52.0

displayInfo function only

emp

fwt

fica

net

Samuel Montez

Barbara Jacks

100.0

130.0

40.0

52.0

360.0

468.0

  

 

//T7AppE02.cpp - displays employee's name, FWT, FICA tax, and net pay

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, float &);

void calcFedTaxes(float, float, float, float &, float &);

void calcNetPay(float, float, float, float &);

void displayInfo(string, float, float, float);

int main()

{

const float FWT_RATE = float(.2);

const float FICA_RATE = float(.08);

string name = "";

float salary = 0.0;

float fwtTax = 0.0;

float ficaTax = 0.0;

float netPay = 0.0;

//enter input items

getInput(name, salary);

//calculate taxes

calcFedTaxes(salary, FWT_RATE, FICA_RATE, fwtTax, ficaTax);

//calculate net pay

calcNetPay(salary, fwtTax, ficaTax, netPay);

//display name, gross, taxes, and net

displayInfo(name, fwtTax, ficaTax, netPay);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &employee, float &weekPay)

{

cout << "Employee name: ";

getline(cin, employee);

cout << "Weekly salary: ";

cin >> weekPay;

} //end of getInput function

void calcFedTaxes(float weekPay, float rate1, float rate2, float &fwt, float &fica)

{

fwt = weekPay * rate1;

fica = weekPay * rate2;

} //end of calcFedTaxes function

void calcNetPay(float weekPay, float fwt, float fica, float &net)

{

net = weekPay - fwt - fica;

} //end of calcNetPay function

 

 

 

 

void displayInfo(string emp, float fwt, float fica, float net)

{

cout << "Name: " << emp << endl;

cout << "FWT: " << fwt << endl;

cout << "FICA: " << fica << endl;

cout << "Net: " << net << endl;

} //end of displayInfo function

 

3.

IPO charts:

main function

Input

Processing

Output

Fahrenheit temperature

 

Processing items: none

Algorithm:

  1. getFahrenheit( Fahrenheit temperature)
  2. calcCelsius(Fahrenheit temperature, Celsius temperature)
  3. displayCelsius(Celsius temperature)

Celsius temperature

getFahrenheit function

Input

Processing

Output

address of the Fahrenheit temperature

Processing items: none

Algorithm:

  1. enter the Fahrenheit temperature

Fahrenheit temperature

calcCelsius function

Input

Processing

Output

Fahrenheit temperature

address of the Celsius temperature

Processing items: none

Algorithm:

  1. calculate the Celsius temperature as follows: 5.0/9.0 * (Fahrenheit temperature – 32)

Celsius temperature

displayCelsius function

Input

Processing

Output

Celsius temperature

 

Processing items: none

Algorithm:

  1. display the Celsius temperature

Celsius temperature

Desk-check tables: Unless specified otherwise, variables belong to the main function only.

fahrenheit (main)

tempF (getFahrenheit)

celsius (main)

tempC (calcCelsius)

0

32

0

212

0.0

0.0

0.0

100.0

calcCelsius function displayCelsius function

tempFahrenheit

 

cel

32

212

 

0.0

100.0

 

//T7AppE03.cpp - displays a Celsius temperature

#include <iostream>

using namespace std;

//function prototypes

void getFahrenheit(int &);

void calcCelsius(int, float &);

void displayCelsius(float);

int main()

{

//declare variables

int fahrenheit = 0;

float celsius = 0.0;

//get input item

getFahrenheit(fahrenheit);

//calculate Celsius

calcCelsius(fahrenheit, celsius);

//display output item

displayCelsius(celsius);

return 0;

} //end of main function

//*****program-defined functions*****

void getFahrenheit(int &tempF)

{

cout << "Enter Fahrenheit temperature: ";

cin >> tempF;

} //end of getFahrenheit function

void calcCelsius(int tempFahrenheit, float &tempC)

{

tempC = 5.0 / 9.0 * (tempFahrenheit - 32);

} //end of calcCelsius function

void displayCelsius(float cel)

{

cout << fixed;

cout.precision(0);

cout << "Celsius: " << cel << endl;

} //end of displayCelsius function

 

4.

IPO charts:

main function

Input

Processing

Output

sales

bonus rate (.1)

Processing items: none

Algorithm:

  1. getSales(sales)
  2. calcBonus(sales, bonus rate, bonus)
  3. displayBonus(bonus)

bonus

getSales function

Input

Processing

Output

address of sales

Processing items: none

Algorithm:

  1. enter the sales

sales

calcBonus function

Input

Processing

Output

sales

bonus rate

address of bonus

Processing items: none

Algorithm:

  1. calculate the bonus by multiplying the sales by the bonus rate

bonus

displayBonus function

Input

Processing

Output

bonus

Processing items: none

Algorithm:

  1. display the bonus

bonus

Desk-check tables: Unless specified otherwise, variables belong to the main function only.

main function

RATE

sales (main)

saleAmt (getSales)

bonus (main)

bonusAmt (calcBonus)

.1

.1

0.0

24500.0

0.0

134780.0

0.0

2450.0

0.0

13478.0

calcBonus function displayBonus function

salesAmt

bonusRate

 

bonusAmt

24500.0

134780.0

.1

.1

 

2450.0

13478.0

//T7AppE04.cpp - displays a bonus

#include <iostream>

using namespace std;

//function prototypes

void getSales(float &);

void calcBonus(float, float, float &);

void displayBonus(float);

int main()

{

const float RATE = float(.1);

//declare variables

float sales = 0.0;

float bonus = 0.0;

//get input item

getSales(sales);

//calculate bonus

calcBonus(sales, RATE, bonus);

//display output item

displayBonus(bonus);

return 0;

} //end of main function

//*****program-defined functions*****

void getSales(float &saleAmt)

{

cout << "Enter a sales amount: ";

cin >> saleAmt;

} //end of getSales function

void calcBonus(float salesAmt, float bonusRate, float &bonusAmt)

{

bonusAmt = salesAmt * bonusRate;

} //end of calcBonus function

void displayBonus(float bonusAmt)

{

cout << fixed;

cout.precision(2);

cout << "Bonus: " << bonusAmt << endl;

} //end of displayBonus function

 

5.

//T7AppE05.cpp - displays the present value of a series of periodic payments

#include <iostream>

#include <cmath>

#include <fstream>

using namespace std;

//function prototypes

void getRate(float &);

void calcPrin(int, int, float, float &);

int main()

{

const int PAYMENT = 360;

const int TERM = 48;

float rate = 0.0;

float principal = 0.0;

ofstream outFile;

outFile.open("T7AppE05.dat");

//enter input item

getRate(rate);

//calculate present value (principal)

calcPrin(PAYMENT, TERM, rate, principal);

//display output items

cout << fixed;

cout.precision(0);

cout << "You can borrow up to $" << principal << endl;

outFile << fixed;

outFile.precision(0);

outFile << "You can borrow up to $" << principal << endl;

outFile.close();

return 0;

} //end of main function

//*****program-defined functions*****

void getRate(float &intRate)

{

cout << "Enter the annual interest rate: ";

cin >> intRate;

intRate = intRate / 12;

} //end of getRate function

void calcPrin(int pay, int months, float iRate, float &prin)

{

prin = pay * (1 - pow(iRate + 1, -months)) / iRate;

} //end of calcPrin function

 

Contents of the T7AppE05.dat file

You can borrow up to $15034

 

6.

IPO charts:

main function

Input

Processing

Output

score 1

score 2

score 3

Processing items: none

Algorithm:

  1. getTestScores(score 1, score 2, score 3)
  2. average = calcAverage(score 1, score 2, score 3)
  3. displayAverage(average)

average

getTestScores function

Input

Processing

Output

address of score 1

address of score 2

address of score 3

Processing items: none

Algorithm:

  1. enter score 1, score 2, score 3

score 1

score 2

score 3

calcAverage function

Input

Processing

Output

score 1

score 2

score 3

Processing items: none

Algorithm:

  1. calculate the average by adding together score 1, score 2, and score 3, and then dividing the sum by 3
  2. return the average

average

displayAverage function

Input

Processing

Output

average

Processing items: none

Algorithm:

  1. display the average

average

 

Desk-check tables: Unless specified otherwise, variables belong to the main function only.

main function

score1 (main)

s1 (getTestScores)

score2 (main)

s2 (getTestScores)

score3 (main)

s3 (getTestScores)

average

0.0

95.5

0.0

54.0

0.0

83.0

0.0

89.0

0.0

76.0

0.0

77.0

0.0

84.8

0.0

73.3

calcAverage function displayAverage function

s1

s2

s3

 

avg

95.5

54.0

83.0

89.0

76.0

77.0

 

84.8

73.3

 

//T7AppE06.cpp - displays the average of three numbers

#include <iostream>

using namespace std;

//function prototypes

void getTestScores(float &, float &, float &);

float calcAverage(float, float, float);

void displayAverage(float);

int main()

{

float score1 = 0.0;

float score2 = 0.0;

float score3 = 0.0;

float average = 0.0;

//enter scores

getTestScores(score1, score2, score3);

//calculate average

average = calcAverage(score1, score2, score3);

//display average

displayAverage(average);

return 0;

} //end of main function

//*****program-defined functions*****

void getTestScores(float &s1, float &s2, float &s3)

{

cout << "Enter score 1: ";

cin >> s1;

cout << "Enter score 2: ";

cin >> s2;

cout << "Enter score 3: ";

cin >> s3;

} //end of getTestScores function

float calcAverage(float s1, float s2, float s3)

{

return (s1 + s2 + s3) / 3;

} //end of calcAverage function

void displayAverage(float avg)

{

cout << fixed;

cout.precision(1);

cout << "Average: " << avg << endl;

} //end of displayAverage function

 

7.

//T7AppE07.cpp - displays a water bill

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, int &, int &);

int calcGallons(int, int);

float calcCharge(int, float);

void displayBill(string, int, float);

int main()

{

const float RATE = float(.00175);

string name = "";

int current = 0;

int previous = 0;

int gallons = 0;

float charge = 0.0;

//enter input items

getInput(name, current, previous);

//calculate gallons used

gallons = calcGallons(current, previous);

//calculate water charge

charge = calcCharge(gallons, RATE);

//display water bill

displayBill(name, gallons, charge);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &cust, int &cur, int &prev)

{

cout << "Customer name: ";

getline(cin, cust);

cout << "Current reading: ";

cin >> cur;

cout << "Previous reading: ";

cin >>prev;

} //end of getInput function

int calcGallons(int cur, int prev)

{

return cur - prev;

} //end of calcGallons function

float calcCharge(int gal, float waterRate)

{

return gal * waterRate;

} //end of calcCharge function

 

 

 

 

void displayBill(string cust, int used, float amtDue)

{

cout << fixed;

cout.precision(2);

cout << "Customer name: " << cust << endl;

cout << "Gallons used: " << used << endl;

cout << "Water charge: " << amtDue << endl;

} //end of displayBill function

8.

IPO charts:

main function

Input

Processing

Output

name

weekly salary

FWT rate (.2)

FICA rate (.08)

 

Processing items: none

Algorithm:

  1. getInput(name, weekly salary)
  2. FWT = calcFwt( weekly salary, FWT rate)
  3. FICA = calcFica(weekly salary, FICA rate)
  4. weekly net pay = calcNetPay(weekly salary, FWT, FICA)
  5. displayInfo(name, FWT, FICA, weekly net pay)

name

FWT

FICA

weekly net pay

getInput function

Input

Processing

Output

address of name

address of weekly salary

 

Processing items: none

Algorithm:

  1. get the name and weekly salary

name

weekly salary

calcFwt function

Input

Processing

Output

weekly salary

FWT rate

Processing items: none

Algorithm:

  1. calculate FWT by multiplying the weekly salary by the FWT rate
  2. return the FWT

FWT

 

calcFica function

Input

Processing

Output

weekly salary

FICA rate

Processing items: none

Algorithm:

  1. calculate the FICA by multiplying the weekly salary by the FICA rate
  2. return the FICA

FICA

calcNetPay function

Input

Processing

Output

weekly salary

FWT

FICA

Processing items: none

Algorithm:

  1. calculate the weekly net pay by subtracting the FWT and FICA from the weekly salary
  2. return the weekly net pay

weekly net pay

 

displayInfo function

Input

Processing

Output

name

FWT

FICA

weekly net pay

Processing items: none

Algorithm:

  1. display the name, FWT, FICA, and weekly net pay

name

FWT

FICA

weekly net pay

Desk-check tables: Unless specified otherwise, variables belong to the main function only.

main function

FWT_RATE

FICA_RATE

name (main)

employee (getInput)

salary (main)

weekPay (getInput)

.2

.2

.08

.08


Bonnie James


Drew Carlisle

0.0

350.0

0.0

700.0

fwtTax

ficaTax

netPay

0.0

70.0

0.0

140.0

0.0

28.0

0.0

56.0

0.0

252.0

0.0

504.0

calcFwt function

weekPay

rateFwt

fwt

350.0

700.0

.2

.2

0.0

70.0

0.0

140.0

calcFica function

weekPay

rateFica

fica

350.0

700.0

.08

.08

0.0

28.0

0.0

56.0

calcNetPay function

weekPay

fwt

fica

net

350.0

700.0

70.0

140.0

28.0

56.0

0.0

252.0

0.0

504.0

displayInfo function

emp

fwt

fica

net

Bonnie James

Drew Carlisle

70.0

140.0

28.0

56.0

252.0

504.0

 

//T7AppE08.cpp - displays employee's name, FWT, FICA tax, and net pay

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, float &);

float calcFwt(float, float);

float calcFica(float, float);

float calcNetPay(float, float, float);

void displayInfo(string, float, float, float);

int main()

{

const float FWT_RATE = float(.2);

const float FICA_RATE = float(.08);

string name = "";

float salary = 0.0;

float fwtTax = 0.0;

float ficaTax = 0.0;

float netPay = 0.0;

//enter input items

getInput(name, salary);

//calculate taxes

fwtTax = calcFwt(salary, FWT_RATE);

ficaTax = calcFica(salary, FICA_RATE);

//calculate net pay

netPay = calcNetPay(salary, fwtTax, ficaTax);

//display name, gross, taxes, and net

displayInfo(name, fwtTax, ficaTax, netPay);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &employee, float &weekPay)

{

cout << "Employee name: ";

getline(cin, employee);

cout << "Weekly salary: ";

cin >> weekPay;

} //end of getInput function

float calcFwt(float weekPay, float rateFwt)

{

float fwt = 0.0;

fwt = weekPay * rateFwt;

return fwt;

} //end of calcFwt function

float calcFica(float weekPay, float rateFica)

{

float fica = 0.0;

fica = weekPay * rateFica;

return fica;

} //end of calcFica function

float calcNetPay(float weekPay, float fwt, float fica)

{

float net = 0.0;

net = weekPay - fwt - fica;

return net;

} //end of calcNetPay function

void displayInfo(string emp, float fwt, float fica, float net)

{

cout << "Name: " << emp << endl;

cout << "FWT: " << fwt << endl;

cout << "FICA: " << fica << endl;

cout << "Net: " << net << endl;

} //end of displayInfo function

 

9.

//T7AppE09.cpp - displays a water bill

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, int &, int &);

void calculate(int, int, float, int &, float &);

void displayBill(string, int, float);

int main()

{

const float RATE = float(1.75);

string name = "";

int current = 0;

int previous = 0;

int gallons = 0;

float charge = 0.0;

//enter input items

getInput(name, current, previous);

//calculate gallons used and water charge

calculate(current, previous, RATE, gallons, charge);

//display water bill

displayBill(name, gallons, charge);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &cust, int &cur, int &prev)

{

cout << "Customer name: ";

getline(cin, cust);

cout << "Current reading: ";

cin >> cur;

cout << "Previous reading: ";

cin >>prev;

} //end of getInput function

void calculate(int c, int p, float r, int &gal, float &due)

{

gal = c - p; //calculate gallons used

due = gal / 1000.0 * r; //calculate water charge

} //end of calculate function

void displayBill(string cust, int used, float amtDue)

{

cout << fixed;

cout.precision(2);

cout << "Customer name: " << cust << endl;

cout << "Gallons used: " << used << endl;

cout << "Water charge: " << amtDue << endl;

} //end of displayBill function

 

 

10.

//T7AppE10.cpp - displays a water bill

#include <iostream>

#include <string>

using namespace std;

//function prototypes

void getInput(string &, int &, int &);

void calculate(int, int, const float, int &, float &);

void displayBill(string, int, float);

int main()

{

const float RATE = float(.00175);

string name = "";

int current = 0;

int previous = 0;

int gallons = 0;

float charge = 0.0;

//enter input items

getInput(name, current, previous);

//calculate gallons used and water charge

calculate(current, previous, RATE, gallons, charge);

//display water bill

displayBill(name, gallons, charge);

return 0;

} //end of main function

//*****program-defined functions*****

void getInput(string &cust, int &cur, int &prev)

{

cout << "Customer name: ";

getline(cin, cust);

cout << "Current reading: ";

cin >> cur;

cout << "Previous reading: ";

cin >>prev;

} //end of getInput function

void calculate(int c, int p, const float r, int &gal, float &due)

{

gal = c - p; //calculate gallons used

due = gal * r; //calculate water charge

} //end of calculate function

void displayBill(string cust, int used, float amtDue)

{

cout << fixed;

cout.precision(2);

cout << "Customer name: " << cust << endl;

cout << "Gallons used: " << used << endl;

cout << "Water charge: " << amtDue << endl;

} //end of displayBill function

 

11. Changes made to the original code are shaded.

/T7AppE11.cpp - displays an employee's gross pay

#include <iostream>

using namespace std;

//function prototypes

void enterItems(float &, float &);

void calcGross(float, float, float &);

void displayGross(float);

int main()

{

//declare variables

float hours = 0.0;

float payRate = 0.0;

float gross = 0.0;

//enter input values

enterItems(hours, payRate);

//calculate gross pay

calcGross(hours, payRate, gross);

//display output item

displayGross(gross);

return 0;

} //end of main function

 

//*****program-defined functions*****

void enterItems(float &hrs, float &rate)

{

cout << "Enter hours: ";

cin >> hrs;

cout << "Enter pay rate: ";

cin >> rate;

} //end of enterItems function

void calcGross(float hrs, float rate, float &grossPay)

{

grossPay = hrs * rate;

} //end of calcGross function

void displayGross(float grossPay)

{

cout << "Gross Pay: " << grossPay << endl;

} //end of displayGross function

 

12. Changes made to the original code are shaded.

//T7AppE12.cpp - displays a bonus

#include <iostream>

using namespace std;

//function prototypes

void enterItems(float &, float &);

void calcAndDisplayBonus(float, float);

int main()

{

//declare variables

float sales = 0.0;

float bonusRate = 0.0;

//enter input values

enterItems(sales, bonusRate);

//calculate and display bonus

calcAndDisplayBonus(sales, bonusRate);

return 0;

} //end of main function

 

//*****program-defined functions*****

void enterItems(float &salesAmt, float &rate)

{

cout << "Enter sales: ";

cin >> salesAmt;

cout << "Enter bonus rate: ";

cin >> rate;

} //end of enterItems function

void calcAndDisplayBonus(float salesAmt, float rate)

{

cout << "Bonus: " << salesAmt * rate << endl;

} //end of calcAndDisplayBonus function