Next: Summary
Up: Further User-defined functions in
Previous: Call-by-reference parameters
Details of an invoice are available as follows:
The number of items on the invoice - n
For each item
an item code (6 digits), a quantity and
a unit cost (pounds,pence)
Thus a typical set of input values for an invoice might be:3 161432 5 6 50 543289 10 2 25 876234 2 10 75indicating that there are three items on the invoice, the first item having an item code of 161432, an order quantity of 5 and a unit price of six pounds fifty pence. Assume that the days date will also be entered.
Write a C++ program to enter details of such an invoice and to output an invoice which indicates the total cost of each item and the total cost of the invoice together with full details. The above details might produce an invoice as follows:
Invoice date: 10/6/96
Item quantity unit price total price
161432 5 6.50 32.50
543289 10 2.25 22.50
876234 2 10.75 21.50
Total 76.50
A first attempt at an algorithm might be:
initialise.
enter date.
enter number of items into n.
output headings.
for n items do
{
enter a record.
calculate total cost of item.
accumulate total invoice cost.
write line of invoice.
}
output total cost.
Assume that there are four programmers available to implement this
program. There are four major operations inside the for loop
hence each programmer could be given one operation to implement. The
best way to do this is to use a function for each operation. Each
programmer can then be given a precise definition of a function.
For example consider the operation enter a record. This
function must read in the integer quantities item number, quantity and
unit price in pounds and pence. Hence it has no input parameters and
four output parameters. A definition of the function could then be
written as follows:
Function name: dataentry
Operation: Enter a record
Description: Enters four integers from the current
input line and returns their values.
Parameters: Output parameter int itemno
Output parameter int quantity
Output parameter int unitpounds
Output parameter int unitpence
Similarly the other functions could be specified as follows:
Function name : calccost
Operation : Calculates the cost for a single item.
Description : Given the unit price of an item in
pounds and pence and the quantity of
the item calculates the total cost in
pounds and pence.
Parameters : Input parameter int quantity
input parameter int unitpounds
input parameter int unitpence
output parameter int totalpound
output parameter int totalpence
Function name : acctotal
Operation : Accumulates the total cost of invoice
Description : Given current total invoice cost and
the total cost of an invoice item
calculates the new total invoice cost.
Parameters : input parameter int totalpound
input parameter int totalpence
input & output parameter int invpound
input & output parameter int invpence
Function name : writeline
Operation : Writes a line of the invoice.
Description : Given the item reference number, the
quantity, the unit price and total
price of an item outputs a line of
the invoice.
Parameters : input parameter int itemno
input parameter int quantity
input parameter int unitpounds
input parameter int unitpence
input parameter int totalpound
input parameter int totalpence
In terms of these functions the main program could be written as follows:
void main()
{
int i, // control variable
n, // number of items
itemno, // item reference number
quantity, // quantity of item
unitpounds,
unitpence, // unit item price
totalpound,
totalpence, // total item price
invpound,
invpence; // total invoice price
// initialise
invpound = 0; // total value of invoice has to be
invpence = 0; // set to zero initially
// Enter number of items
cout << "Enter number of items on invoice: ";
cin >> n;
// Headings
cout << " Item quantity unit price total price"
<< endl << endl;
// For n items
for (i=1; i<=n; i++)
{
dataentry(itemno, quantity, unitpounds, unitpence);
calccost(quantity, unitpounds, unitpence, totalpound,
totalpence);
acctotal(totalpound, totalpence, invpound, invpence);
writeline(itemno, quantity, unitpounds, unitpence,
totalpound, totalpence);
}
// write total line
cout << " Total "
<< invpound
<< "."
<< invpence << endl;
}
Download program.
Download sample data.
Using the function specifications above the functions can now be
written and tested separately. For example calccost could be
written as follows:
void calccost(int q, int ul, int up,
int& totl, int& totp)
// Calculates the quantity q times the unit cost in
// pounds and pence in ul and up and places the
// result in pounds and pence in totl and totp
{
int p;
p = q * up;
totp = p % 100;
totl = q * ul + p/100;
}
Download program.
To test this function on its own a driver program would have to be written. A driver program is a simple program that allows the programmer to enter values for the parameters of the function to be tested and outputs the results. A suitable driver program to test the above function could be:
// IEA 1996
// Driver program to test calccost
#include <iostream.h>
// function prototype
void calccost(int, int, int, int&, int&);
void main()
{
int quant, unitl, unitp, totall, totalp;
// stop on negative quantity
cout << "Enter quantity: ";
cin >> quant;
while (quant >= 0)
{
cout << "Enter unit cost (pounds pence): ";
cin >> unitl >> unitp;
calccost(quant, unitl, unitp, totall, totalp);
cout << endl
<< quant << " times "
<< unitl << " pounds "
<< unitp << " pence "
<< " is "
<< totall << " pounds "
<< totalp << " pence ";
cout << endl << "Enter quantity: ";
cin >> quant;
}
}
// function definition here
Download program.
When testing functions try to use one example of each of the cases
that can occur. For example using the above driver program to show
that calccost works for 7 times 1.10 is not a complete test
since it does not generate any `carry' from the pence to the pounds.
An additional test on say 6 times 1.73 checks that the carry works.
Choosing a set of test data to adequately validate a function requires
much thought.