4.0 4.0 8.0
8.0 8.0 1.21
3 3 0
3.0 3.5 3.5
6.0 6.0 5.0
5.0 4.5 4.5
3 3.0 3.0
sqrt(x + y)
pow(x, y + 7)
sqrt(area + fudge)
sqrt(time + tide)/nobody
(−b + sqrt(b * b − 4 * a * c))/(2 * a)
abs(x − y) or labs(x − y) or fabs(x − y)
//Computes the square root of 3.14159.
#include <iostream>
#include <cmath>
//provides sqrt and PI.
using namespace std;
int main()
{
cout << "The square root of " >> PI
<< sqrt(PI) << endl;
return 0;
}
//To determine whether the compiler will tolerate
//spaces before the # in the #include:
#include <iostream>
using namespace std;
int main( )
{
cout << "hello world" << endl;
return 0;
}
//To determine if the compiler will allow spaces
//between the # and include in the #include:
# include<iostream>
using namespace std;
//The rest of the program can be identical to the above.
Wow
6. The function declaration is:
int sum(int n1, int n2, int n3);
//Returns the sum of n1, n2, and n3.
The function definition is:
int sum(int n1, int n2, int n3)
{ return (n1 + n2 + n3);
}
7. The function declaration is:
double ave(int n1, double n2);
//Returns the average of n1 and n2.
The function definition is:
double ave(int n1, double n2)
{
return ((n1 + n2)/2.0);
}
8. The function declaration is:
char positiveTest(double number);
//Returns 'P' if number is positive.
//Returns 'N' if number is negative or zero.
The function definition is:
char positiveTest(double number)
{
if (number > 0)
return 'P';
else
return 'N';
}
9. Suppose the function is defined with arguments, say param1
and param2
. The function is then called with corresponding arguments arg1
and arg2
. The values of the arguments are “plugged in” for the corresponding formal parameters, arg1
into param1
, arg2
into param2
. The formal parameters are then used in the function.
10. Predefined (library) functions usually require that you #include
a header file. For a programmer-defined function, the programmer puts the code for the function either into the file with the main part of the program or in another file to be compiled and linked to the main program.
bool inOrder(int n1, int n2, int n3)
{
return ((n1 <= n2) && (n2 <= n3));
}
bool even(int n)
{ return ((n % 2) == 0);
}
bool is Digit(char ch)
{
return ('0' <= ch) && (ch <= '9');
}
bool isRootOf(int rootCandidate, int number)
{
return (number == rootCandidate * rootCandidate);
}
15. The comment explains what value the function returns and gives any other information that you need to know in order to use the function.
16. The principle of procedural abstraction says that a function should be written so that it can be used like a black box. This means that the programmer who uses the function need not look at the body of the function definition to see how the function works. The function declaration and accompanying comment should be all the programmer needs to know in order to use the function.
17. When we say that the programmer who uses a function should be able to treat the function like a black box, we mean the programmer should not need to look at the body of the function definition to see how the function works. The function declaration and accompanying comment should be all the programmer needs to know in order to use the function.
18. In order to increase your confidence in your program, you should test it on input values for which you know the correct answers. Perhaps you can calculate the answers by some other means, such as pencil and paper or hand calculator.
19. Yes, the function would return the same value in either case, so the two definitions are black-box equivalent.
20. If you use a variable in a function definition, you should declare the variable in the body of the function definition.
21. Everything will be fine. The program will compile (assuming everything else is correct). The program will run (assuming that everything else is correct). The program will not generate an error message when run (assuming everything else is correct). The program will give the correct output (assuming everything else is correct).
22. The function will work fine. That is the entire answer, but here is some additional information: The formal parameter inches
is a call-by-value parameter and, as discussed in the text, it is therefore a local variable. Thus, the value of the argument will not be changed.
23. The function declaration is:
double readFilter();
//Reads a number from the keyboard. Returns the number
//read provided it is >= 0; otherwise returns zero.
The function definition is:
//uses iostream
double readFilter()
{
using namespace std;
double valueRead;
cout << "Enter a number:\n";
cin >> valueRead;
if (valueRead >= 0)
return valueRead;
else
return 0.0;
}
24. The function call has only one argument, so it would use the function definition that has only one formal parameter.
25. The function call has two arguments of type double
, so it would use the function corresponding to the function declaration with two arguments of type double
(that is, the first function declaration).
26. The second argument is of type int
and the first argument would be automatically converted to type double
by C++ if needed, so it would use the function corresponding to the function declaration with the first argument of type double
and the second argument of type int
(that is, the second function declaration).
27. The second argument is of type double
and the first argument would be automatically converted to type double
by C++ if needed, so it would use the function corresponding to the function declaration with two arguments of type double
(that is, the first function declaration).
28. This cannot be done (at least not in any nice way). The natural ways to represent a square and a round pizza are the same. Each is naturally represented as one number, which is the diameter for a round pizza and the length of a side for a square pizza. In either case the function unitprice
would need to have one formal parameter of type double
for the price and one formal parameter of type int
for the size (either radius or side). Thus, the two function declarations would have the same number and types of formal parameters. (Specifically, they would both have one formal parameter of type double
and one formal parameter of type int
.) Thus, the compiler would not be able to decide which definition to use. You can still defeat this evil pizza parlor’s strategy by defining two functions, but they will need to have different names.
29. The definition of unitprice
does not do any input or output and so does not use the library iostream
. In main
we needed the using
directive because cin
and cout
are defined in iostream
and those definitions place cin
and cout
in the std
namespace.