Chapter 7
FUNCTIONS
Programming Warm-Up Exercises
1.
void PrintMax( /* in */ int number1,/* in */ int number2 )
3.
void GetMeanOf( /* in */ int howMany,/* out */ float& avg )
// Precondition:
// howMany > 0
// Postcondition:
// avg == average of howMany values read from standard input device
{
int loopCount; // Loop control variable
float sum; // Sum of the input values
float inputValue; // One input value
sum = 0.0;
loopCount = 1;
while (loopCount <= howMany)
{
cin >> inputValue;
sum = sum + inputValue;
loopCount++;
}
avg = sum / float(howMany);
}
4.
void Halve( /* inout */ int& firstNumber,/* inout */ int& secondNumber )
{
firstNumber = firstNumber / 2;
secondNumber = secondNumber / 2;
}
6
void ComputeMiles( /* in */ int measurementNumber,
/* inout */ float& totalMiles )
// Precondition:
// 1 <= measurementNumber <= 4
// && totalMiles is assigned
// Postcondition:
// Distance in miles for measurement number measurementNumber
// has been output
// && totalMiles == totalMiles@entry + miles for measurement
// number measurementNumber
{
float distance; // Distance as measured
float miles; // Rounded distance in miles
if (measurementNumber == 1)
distance = DISTANCE1;
else if (measurementNumber == 2)
distance = DISTANCE2;
else if (measurementNumber == 3)
distance = DISTANCE3;
else
distance = DISTANCE4;
miles = float(int(distance * SCALE * 10.0 + 0.5)) / 10.0;
cout << "For a measurement of " << setprecision(1) << distance;
if (measurementNumber == 1)
cout << " the first ";
else if (measurementNumber == 2)
cout << " the second ";
else if (measurementNumber == 3)
cout << " the third ";
else
cout << " the fourth ";
cout << "distance is " << miles << " mile(s) long." << endl;
totalMiles = totalMiles + miles;
}
b.
ComputeMiles(1, totMiles);ComputeMiles(2, totMiles);
ComputeMiles(3, totMiles);
ComputeMiles(4, totMiles);
9.
#include <iostream.h>void Rotate( int&, int&, int&, int& );
int main()
{
int int1; // First input value
int int2; // Second input value
int int3; // Third input value
int int4; // Fourth input value
cout << "Enter four values: ";
cin >> int1 >> int2 >> int3 >> int4;
cout << "Before: " << int1 << ' ' << int2 << ' '
<< int3 << ' ' << int4 << endl;
Rotate(int1, int2, int3, int4);
cout << "After: " << int1 << ' ' << int2 << ' '
<< int3 << ' ' << int4 << endl;
return 0;
}
//******************************************************************
void Rotate( /* inout */ int& firstValue,
/* inout */ int& secondValue,
/* inout */ int& thirdValue,
/* inout */ int& fourthValue )
// Precondition:
// All parameters are assigned
// Postcondition:
// firstValue == secondValue@entry
// && secondValue == thirdValue@entry
// && thirdValue == fourthValue@entry
// && fourthValue == firstValue@entry
{
int temp; // Temporary holding variable
// Save value of first parameter
temp = firstValue;
// Shift values of next three parameters
firstValue = secondValue;
secondValue = thirdValue;
thirdValue = fourthValue;
// Replace value of final parameter with saved value
fourthValue = temp;
}
10.
In the following code, the expression ch >= 'A' && ch <= 'Z' works properly using the ASCII character set but not EBCDIC. Chapter 8 discusses a solution to this difficulty.void CountUpper( /* out */ int& upCount )
// Precondition:
// Reading marker for standard input stream is at beginning of
// a line terminated by '\n'
// Postcondition:
// upCount == number of uppercase letters in the input line
{
char ch; // An input character
upCount = 0;
cin.get(ch);
while (ch != '\n')
{
if (ch >= 'A' && ch <= 'Z')
upCount++;
cin.get(ch);
}
}
11.
void AddTime( /* inout */ int& hours,/* inout */ int& minutes,
/* in */ int elapsedTime )
// Precondition:
// hours >= 0 && 0 <= minutes <= 59
// && elapsedTime >= 0
// Postcondition:
// hours and minutes represent the time obtained by adding
// elapsedTime to the starting time (hours@entry and minutes@entry)
{
minutes = minutes + elapsedTime;
hours = hours + minutes / 60;
minutes = minutes % 60;
}
12.
void GetNonBlank( /* out */ char& ch )// Precondition:
// At least one nonblank character exists in the
// remaining input stream
// Postcondition:
// Any blank input characters have been skipped
// && ch == first nonblank character
{
cin.get(ch);
while (ch == ' ')
cin.get(ch);
}
13.
void SkipToBlank( /* out */ char& ch )// Precondition:
// At least one blank exists in the remaining input stream
// Postcondition:
// Any nonblank input characters have been skipped
// && ch == first blank character
{
cin.get(ch);
while (ch != ' ')
cin.get(ch);
}
14.
void SkipToBlank( /* out */ char& ch,/* out */ int& skipped )
// Precondition:
// At least one blank exists in the remaining input stream
// Postcondition:
// Any nonblank input characters have been skipped
// && ch == first blank character
// && skipped == number of nonblank characters skipped
{
skipped = 0;
cin.get(ch);
while (ch != ' ')
{
skipped++;
cin.get(ch);
}
}
15.
void PrintBarGraph( /* in */ float deptSales )// Precondition:
// 0.0 <= deptSales <= 25000.0
// Postcondition:
// A line of the bar chart has been printed with one * for
// each $500 in sales, with fractions over $250 rounded up
// && No stars have been printed for sales <= $250
{
while (deptSales > 250.0)
{
cout << '*' ; // Print '*' for each $500
deptSales = deptSales - 500.0; // Update loop control
} // variable
}
ANSWERS TO QUESTIONS
True/False
Multiple Choice
Fill-In
38. actual parameter
39. formal parameter
40. reference
41. local variable
42. value
43. function definition
44. reference
47. precondition
48. reference
49. reference
50. reference
51. value
52. postcondition
53.