Professional Documents
Culture Documents
IRM Chapter 6e 05
IRM Chapter 6e 05
One of the more important things in programming is planning, even for the simplest
program. If the planning is thorough, the coding will be easy, and the only errors likely to
be encountered are syntax errors, usually caused by either typing errors, a boundary
condition problem (frequently, an off by one error), or (we hope not) lack of knowledge of
the language details.
1 Convert Time
Task: Convert 24 hour time notation to 12 hour AM/PM
notation.
General comments: The student should note that:
a) The convert function has boundary cases that require careful attention.
b) ALL of this commentary and planning should be done PRIOR to beginning to write the
program. Once this is done the program is almost written. The sooner coding begins, the
longer the program will take to do correctly.
c) When testing for equality, as in
if (12 == hours)
put the constant first. The compiler will catch errors such as if (12= hours) which are hard
to see otherwise. I made many errors of this type while coding this problem.
1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
//file: ch5prog1.cc
#include <iostream>
using namespace std;
2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
int main()
{
int hours, minutes;
char AMPM, ans;
do
{
input( hours, minutes );
convert ( hours, AMPM );
output( hours, minutes, AMPM );
cout << "Enter Y or y to continue,"
<< " anything else quits."
3
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
<< endl;
cin >> ans;
} while ( 'Y'== ans || 'y' == ans );
return 0;
}
4
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
11:30 AM
Enter Y or y to continue, anything else quits.
y
Enter 24 hour time in the format HH:MM
12:30
Time in 12 hour format:
12:30 PM
Enter Y or y to continue, anything else quits.
y
Enter 24 hour time in the format HH:MM
23:59
2. Time
// Waiting time
// Problem 2, Savitch, Programming and Problem Solving with C++ 5th
//
// file ch5.2.cpp
// Program input: current time and a waiting time
// each time is number of hours and a number of minutes.
// Program output is is the time the waiting period completes.
// Use 24 hour time. Allow user repeat.
// Notes: The 24 hour boundary, i.e., when the time wraps to the
// next day is important here.
//
// Known Bugs: If the completion time would be in a day later
// than the next day after the start, this program gives incorrect
// results.
//
#include <iostream>
6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
using std::cout;
using std::cin;
using std::endl;
char colon;
cout << "Enter 24 hour time in the format HH:MM "
<< endl;
cin >> hours24 >> colon >> minutes;
}
int main()
{
using std::cout;
using std::cin;
using std::endl;
cout << "Compute completion time from current time and waiting
period\n";
7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
finishMinutes%= 60;
return 0;
}
/*
Typical run
y
Current time:
Enter 24 hour time in the format HH:MM
8:30
Waiting time:
Enter 24 hour time in the format HH:MM
15:10
Completion Time in 24 hour format:
23:40
n
Press any key to continue
*/
8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
This problem is different from #2 only in the details of managing 12 hour time. The wait
time interval could be any number of hours and minutes, so the output should provide the
number of days that elapse from the start time until completion.
You may want an input routine that verifies that you have entered
legitimate 12 hour time data, i.e. hours betwee 1 and 12, minutes between 0
and 59, and includes either an A for AM or a P for PM.
Write code to convert the 12 hour start time to 24 hour time and use the code from #3 to
computer the finish time.
Decide on how to handle finish times that fall in some later day, then convert 24 hour time
to 12 hour time and output that using code from #2.
4. Statistics
Compute average and standard deviation of 4 entries.
General remarks are in the code file which I present here:
// file ch5prob4.cc
#include <iostream>
using namespace std;
/*
Task: Write a function that computes average (I will call
this the arithmetic mean or simply the mean) and standard
deviation of four scores. The average or mean, avg, is
computed as
avg = ( s1 + s2 + s3 + s4 ) / 4
9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
( s1 − a) + ( s 2 − a ) + ( s3 − a)
std_deviation =
4
Input: scores s1 s2 s3 s4
Output: standard deviation and mean.
Required: The function is to have 6 parameters. This function
calls two others that compute the mean and the std deviation.
A driver with a loop should be written to test the function
at the user's option.
*/
10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
void statistics( double s1, double s2, double s3, double s4,
double& avg, double& stdDev );
int main()
{
double s1, s2, s3, s4, avg, stdDev;
char ans;
do
{
cout << "Enter 4 decimal numbers, "
<< "I will give you the mean "
11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
<< endl
<< "and standard deviation of the data " << endl;
cin >> s1 >> s2 >> s3 >> s4;
statistics( s1, s2, s3, s4, avg, stdDev);
cout << "mean of " << s1 << " " << s2 << " " << s3
<< " " << s4 << " is " << avg << endl
<< "the standard deviation of "
<< "these numbers is " << stdDev << endl;
cout << "y or Y continues, any other terminates" << endl;
cin >> ans;
} while ( 'Y' == ans || 'y' == ans );
return 0;
}
12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
A greedy algorithm works for this problem. A greedy algorithm makes locally optimal
choices at each point in the sequence of points in the solution, in the hope that the locally
optimal choices will result in a globally optimal solution. This works surprisingly often,
including this problem. It is interesting to me that the greedy algorithm fails for some
national coinage.
I suggest that the student write code to choose the largest number of coins of the largest
denomination from the list of coin not yet used. Repeat this on the remaining amount of
money for decreasing denominations until no more coins are left.
6. Conversion 1
Conversion of feet/inches to meters:
//File: Ch4Prob6.cc
//Task: Convert feet/inches to meters
//Input:a length in feet and inches, with possible decimal
//part of inches
//Output: a length in meters, with 2 decimal places, which
//are the 'centimeters' specified in the problem.
13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
int main()
{
int feet;
double inches, meters;
char ans;
do
{
input ( feet, inches );
convert ( feet, inches, meters );
output ( feet, inches, meters );
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;
} while ( 'Y' == ans || 'y' == ans );
return 0;
}
15
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
{
meters = METERS_PER_FOOT * (feet +
inches/INCHES_PER_FOOT);
}
cout.setf( ios::showpoint );
cout.setf( ios::fixed );
cout.precision(2);
cout << "the value of feet, inches" << feet << ","
<< inches << endl
<< " converted to meters, centimeters is "
<< meters << endl;
}
/*
A typical run follows:
06:59:16:~/AW$ a.out
Enter feet as an integer: 5
Enter inches as a double: 7
the value of feet, inches5,7.00
converted to meters, centimeters is 1.70
Y or y continues, any other character quits
y
Enter feet as an integer: 245
Enter inches as a double: 0
the value of feet, inches245,0.00
converted to meters, centimeters is 74.68
16
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
7. Conversion 2
Conversion of meters back to centimeters.
//file: ch5prob7.cc
//Task: Convert meters with centimeters (just the decimal
//part of meters)to feet/inches
//
//Input: a length in feet and inches, with possible decimal
//part of inches
//Output: A length measured in feet with any decimal fraction
//converted to inches by multiplying by 12. Fractions of an
//inch are represented by 2 decimal places.
//
//Required: functions for input, computation, and output.
//Include a loop to repeat the calculation at the user's
//option.
//
//Remark: The computation is a simple conversion from meters
//to feet, inches, where inches has a decimal part.
//Output is restricted to 2 decimal places
//Comment: Please see Problem 4 for discussion of 'meters and
//centimeters'
#include <iostream>
using namespace std;
17
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
//Postcondition:
//Prompt given to the user for input of a number of meters as
//a double. input of a double for meters has been accepted
int main()
{
int feet;
double inches, meters;
char ans;
do
{
input ( meters );
convert ( feet, inches, meters );
output ( feet, inches, meters );
cout << "Y or y continues, any other character quits "
18
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
<< endl;
cin >> ans;
} while ( 'Y' == ans || 'y' == ans );
return 0;
}
void input ( double& meters )
{
cout << "Enter a number of meters as a double \n";
cin >> meters;
}
cout.setf( ios::showpoint );
cout.setf( ios::fixed );
cout.precision(2);
cout << "The value of meters, centimeters " << endl
19
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
/*
A typical run follows:
07:56:28:~/AW$ a.out
Enter a number of meters as a double
6.0
The value of meters, centimeters
6.00 meters
converted to English measure is
19 feet, 8.22 inches
Y or y continues, any other character quits
y
Enter a number of meters as a double
75
The value of meters, centimeters
75.00 meters
converted to English measure is
246 feet, 0.76 inches
Y or y continues, any other character quits
q
07:56:40:~/AW$
*/
8. Conversion 3
This exercise combines the two previous exercises. Convert between feet/inches and
meters. The direction is the user's option.
20
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
// file: ch5prob8.cc
//Task: Convert between meters and feet/inches at user's
//option Within conversion, allow repeated calculation
//after end of either conversion, allow choice of another
//conversion.
#include <iostream>
using namespace std;
21
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
//Postcondition:
//Prompt given to the user for input in the format FF II,
//where FF is an int number of feet and II is a double number
//of inches feet and inches are returned as entered by the
//user.
22
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
//places
void MetricToEnglish();
// request metric measure, converts to English, outputs both
int main()
{
char ans;
do
{
int which;
cout << "Enter 1 for English to Metric or " << endl
<< "Enter 2 for Metric to English conversion"
<< endl;
cin >> which;
if ( 1 == which )
EnglishToMetric();
else
MetricToEnglish();
void MetricToEnglish()
{
23
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
int feet;
double inches, meters;
char ans;
do
{
inputM ( meters );
convertMtoE ( feet, inches, meters );
output ( feet, inches, meters );
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;
} while ( 'Y' == ans || 'y' == ans );
}
void EnglishToMetric ()
{
int feet;
double inches, meters;
char ans;
do
{
inputE ( feet, inches );
convertEtoM ( feet, inches, meters );
output ( feet, inches, meters );
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;
} while ( 'Y' == ans || 'y' == ans );
}
24
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
{
cout << "Enter feet as an integer: " << flush;
cin >> feet;
cout << "Enter inches as a double: " << flush;
cin >> inches;
}
25
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
/*
A typical run follows
08:59:38:~/AW$ a.out
Enter 1 for English to Metric or
Enter 2 for Metric to English conversion
2
Enter a number of meters as a double
75
75.00 meters corresponds to 246 feet, 0.76 inches
Y or y continues, any other character quits
n
Y or y allows another choice of conversion. any other quits
y
Enter 1 for English to Metric or
Enter 2 for Metric to English conversion
1
26
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
s = (a + b + c)/2
area = sqrt(s(s - a)(s - b)(s - c))
It is necessary to test whether edges of lengths a, b and c actually form a triangle. The
test for values of a, b and c to form a triangle is that each of the following inequalities
be satisfied.
a + b > c
and
1
Hero, Heron, or Hron, was a mathematician conjectured to have lived between the 3rd and 2nd centuries
BCE. He lived in Alexandria, but wrote in Greek. This formula is ascribed to him as is Hero’s engine, where
steam recoil rotates a sphere or wheel.
27
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
b+ c > a
and
a+c > b
A question to ask the student might be: Do two of these imply the third?
The answer, (un?)fortunately, is no. All three conditions for legimate data must be
checked. The student should be able to produce an example such as a = 1, b = 1, c
= 2 that shows that the assertion, “two of these conditions implies the third”, isn’t true for
all a, b and c.
The program should confirm that the entered values of a, b, and c satisfy these
condtions. If the test shows a triangle is possible, then the area and perimeter are computed
and reported. If the test fails, the program reports this fact and exits.
28
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
0.16 0.16
w = 13.12 + 0.6215 * t – 11.37 * v + 0.3965 * t * v ;
// File: ch5.14.cpp
// Windchill Index
// Wind Chill
#include <iostream>
#include <cmath>
#include <cstdlib>
//using namespace std;
29
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
int main()
{
using std::cout;
using std::cin;
//using std::pow; // quirk of MSVC++
using std::endl;
double t, v;
char ans;
do
{
cout << "Enter the Celcius termperature (<= 10
degrees)\n";
cin >> t;
cout << "Enter wind speed in meters/second: \n";
cin >> v;
/*
Typical run:
30
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
http://www.msc.ec.gc.ca/education/windchill/windchill_calcula
tor_e.cfm
*/
15.
//
***********************************************************************
// Ch5Proj15abc.cpp
//
// This program simulates the duel between Aaron, Bob, and Charlie.
// It answers parts a-c of project 15.
31
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
//
***********************************************************************
#include <iostream>
#include <cstdlib> // Needed for random numbers
// Function prototypes
void shoot(bool& targetAlive, double accuracy);
int startDuel();
// Constants
const double AARONACCURACY = 1.0/3;
const double BOBACCURRACY = 0.5;
const double CHARLIEACCURACY = 1.0;
const int NUMDUELS = 1000;
// ======================
// shoot:
// Simulates shooting at a live target,
// referenced by targetAlive, with a given accuracy.
// ======================
void shoot(bool& targetAlive, double accuracy)
{
double r;
// Only continue if the target is alive
if (targetAlive == true)
{
r = rand() % 100; // Random number from 0-99
if (r < (accuracy * 100))
{
// Hit target
targetAlive = false;
}
}
}
// ======================
// startDuel:
// Simulates a duel where Aaron shoots first and each
// contestant shoots at the best shooter still alive.
//
// Returns 0 if Aaron wins, 1 if Bob wins, 2 if Charlie wins
// ======================
int startDuel()
{
bool aaronAlive = true, bobAlive = true, charlieAlive = true;
32
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
// Aaron's turn
if (aaronAlive)
{
if (charlieAlive)
{
shoot(charlieAlive, AARONACCURACY);
}
else if (bobAlive)
{
shoot(bobAlive, AARONACCURACY);
}
}
// Bobs's turn
if (bobAlive)
{
if (charlieAlive)
{
shoot(charlieAlive, BOBACCURRACY);
}
else if (aaronAlive)
{
shoot(aaronAlive, BOBACCURRACY);
}
}
// Charlie's turn
if (charlieAlive)
{
if (bobAlive)
{
shoot(bobAlive, CHARLIEACCURACY);
}
else if (aaronAlive)
{
shoot(aaronAlive, CHARLIEACCURACY);
}
}
}
if (aaronAlive) return 0;
else if (bobAlive) return 1;
else if (charlieAlive) return 2;
}
// ====================
// main function
// ====================
int main()
{
int i;
int winner;
double aaronWins=0, bobWins=0, charlieWins=0;
33
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
cout << "Using the strategy of shooting at the best shooter alive:" <<
endl;
cout << "Aaron win %: " << (aaronWins/NUMDUELS) << endl;
cout << "Bob win %: " << (bobWins/NUMDUELS) << endl;
cout << "Charlie win %: " << (charlieWins/NUMDUELS) << endl;
return 0;
}
//
***********************************************************************
// Ch5Proj15d.cpp
//
// This program simulates the duel between Aaron, Bob, and Charlie.
// It answers part d of project 15, where Aaron intentionally misses his
// first shot.
//
***********************************************************************
#include <iostream>
#include <cstdlib> // Needed for random numbers
// Function prototypes
void shoot(bool& targetAlive, double accuracy);
int startDuel();
// Constants
const double AARONACCURACY = 1.0/3;
const double BOBACCURRACY = 0.5;
const double CHARLIEACCURACY = 1.0;
const int NUMDUELS = 1000;
// ======================
// shoot:
34
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
// ======================
// startDuel:
// Simulates a duel where Aaron shoots first and each
// contestant shoots at the best shooter still alive.
// To simulate Aaron missing on his first shot, Aaron is moved
// to the end of the cycle (i.e. Bob shoots first, which is equivalent
// to Aaron missing first).
//
// Returns 0 if Aaron wins, 1 if Bob wins, 2 if Charlie wins
// ======================
int startDuel()
{
bool aaronAlive = true, bobAlive = true, charlieAlive = true;
// Charlie's turn
if (charlieAlive)
{
if (bobAlive)
35
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
{
shoot(bobAlive, CHARLIEACCURACY);
}
else if (aaronAlive)
{
shoot(aaronAlive, CHARLIEACCURACY);
}
}
// ====================
// main function
// ====================
int main()
{
int i;
int winner;
double aaronWins=0, bobWins=0, charlieWins=0;
36
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
cout << "Using the strategy of shooting at the best shooter alive, but
Aaron intentionally misses on the first shot:" << endl;
cout << "Aaron win %: " << (aaronWins/NUMDUELS) << endl;
cout << "Bob win %: " << (bobWins/NUMDUELS) << endl;
cout << "Charlie win %: " << (charlieWins/NUMDUELS) << endl;
return 0;
}
2. Outline of Topics
5.1 void-Functions
Definitions of void Functions
return Statements in void Functions
5.2 Call-by-Reference Parameters
A First View of Call-by-Reference
Call-by-Reference in Detail
Mixed Parameter Lists
5.3 Using Procedural Abstraction
Functions Calling Functions
Preconditions and Postconditions
5.4 Testing and Debugging Functions
Stubs and Drivers
37
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
Read the sections 7.2 and 7.4 of Ellis and Stroustrup, The Annotated C++ Reference
Manual, Addison Wesley, 1991, reprinted with corrections 1994, ISBN 0-201-5149-1, and
sections 11.3.1-11.3.3 in Stroustrup, The design and Evolution of C++ Addison Wesley,
1994 reprinted with corrections, May 1994, or Stroustrup, The C++ Programming
Language, 3rd edition Addison Wesley Longman, 1997. for details.
2
“automagically” is a neologism which means automatically, as if by magic.
38
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
Notice that the syntactic distinction between call-by-reference and call-by-value is the
ampersand sign, &, which is between the type-name and the parameter namein the
parameter list. This distinction allows us to mix value and reference parameter passing
mechanisms. There is no C++ reason for placing the ampersand adjacent to the type, but
this is typically C++ programming style, found in most C++ texts and treatises.
It is important to note that the ampersand for a reference parameter must be put both into
the declaration (prototype) and into the definition of a function using pass-by-reference.
One further warning: overloading based on a distinction between a reference parameter
and a value parameter was not supported in many early compilers. This has been corrected
in more recent compilers. GNU g++ complains about the following code.
//file: test1.cc
//to check overloading based on distinction between
//value parameter and reference parameter of same type.
#include <iostream>
using namespace std;
void f( int &i, int j) { cout << "f(int&, int)" << endl; }
void f( int i, int& j){ cout << "f(int, int&)" << endl; }
int main()
{
int i = 1, j = 2;
f(i, 1);
f(1, j);
f(1, 1);
f(i, j);
return 0;
}
The error messages are:
39
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
It should be reiterated that omission of an ampersand & for a variable that should be a
reference-parameter is a an error that can be quite difficult to find.
1) having no abnormal termination of the program for data that meets the preconditions on
the input, that is, the program should run to a normal successful completion for correct
data, and
2) for correct input, the program should generate correct output - output that meets the post
conditions for that input data.
3) desirable behavior in response to incorrect data is that the program should behave in a
reasonable way. This may mean summary termination in the face of error, or detecting
40
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
errors and giving the user another chance, or behaving in some other fail-safe way (an
elevator control, perhaps?)
The pre- and post- conditions should be machine testable. Accomplishing this can be
difficult in some instances. This is a goal to be sought in writing pre- and post- conditions.
Failure to meet this goal means increased difficulty in demonstrating that the program is
correct.
How?
The main program can be written, then tested independently of the subprograms by
writing 'stub' subprograms - small subprograms that do little more than return fictitious
data that the calling function needs to be able to continue, and (at least in my
programming) emits a message that lets the tester know that the subprogram has been
encountered. These stub subprograms should simple enough that correctness is not a
problem.
This technique allows part of a program to be tested before the rest of it has been tested, or
perhaps before the subprograms have been written.
The other technique is bottom up: test the functions independently of the calling program.
How? Write a driver - a main program or calling program substitute - that exercises the
subprogram with data that a correct calling program might present to the subprogram
during execution. In fact, incorrect data should also be used to test the robustness of the
subprogram. Proper selection of data is essential to the success of this testing technique.
41
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 6e Chapter 5
These testing techniques are useful to the extent that the client program author and the
subprogram program author use procedural abstraction. This means that the specifications
of the subprogram is all that is known to the client, and, beyond the specifications, any use
to which the subprogram in put is not known to the subprogram author.
Dividing a program into separate pieces, which are tested apart from each other, is a useful
technique independently of pre and post- conditions. However, the testing is limited if
exact conditions are not known. Clearly, a specification of input, output and action for all
functions is essential to the ability to full use of this testing technique.
42
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley