View
9
Download
0
Category
Preview:
Citation preview
Chapter 6: User-Defined Functions
Determine the value of each of the following expressions:
a. static_cast<char>(toupper('$'))
b. static_cast<char>(toupper('3'))
c. static_cast<char>(toupper('#'))
d. static_cast<char>(toupper('d'))
e. static_cast<char>(tolower('+'))
f. static_cast<char>(tolower('?'))
g. static_cast<char>(tolower('H'))
h. static_cast<char>(tolower('%'))
Determine the value of each of the following expressions:
a. abs(12) b. fabs(23.45) c. fabs(-7.8) d. pow(4.8, 2)
e. pow(4.0, 2.5) f. sqrt(49.0) g. sqrt(7.29)
h. pow(6.0, 3.0) / abs(-36) i. floor(36.27) j. ceil(18.3)
Using the predefined functions in cmath library, write each of the following as a
C++ expression. (The expression in (e) denotes the absolute value of x + 2y 3.)
a. 3.75 6.8 b. √𝒙 − 𝟐𝒚
c. wt/3 e. |x+2y-3|
Consider the following function definition: int func (int x, double y, char u, string name)
{
//function body
}
Which of the following are correct function prototypes of the function func?
A. int func(x, y, u, name);
B. int func(int s, double k, char ch, string name);
C. int func(int, double, char, string); D. func(int, double, char, string)
Consider the following program:
#include <iostream>
#include <cmath>
using namespace std;
int main() { int num1;
int num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << endl;
if (num1 != 0 && num2 != 0) cout << sqrt(fabs(num1 + num2 + 0.0)) << endl;
else if (num1 != 0) cout << floor(num1 + 0.0) << endl;
else if (num2 != 0) cout << ceil(num2 + 0.0) << endl;
else
cout << 0 << endl;
return 0; }
2. What is the output if the input is 12 4?
3. What is the output if the input is 3 27?
4. What is the output if the input is 25 0?
5. What is the output if the input is 0 49?
Consider the following statements:
int num1, num2, num3; double length, width, height; double volume; num1 = 6;
num2 = 7;
num3 = 4; length = 6.2;
width = 2.3;
height = 3.4
and the function prototype:
double box(double, double, double);
Which of the following statements are valid? If they are invalid, explain why.
1. volume = box(length, width, height);
2. volume = box(length, 3.8, height);
3. cout << box(num1, num3, num2) << endl;
4. cout << box(length, width, 7.0) << endl;
5. volume = box(length, num1, height);
6. cout << box(6.2, , height) << endl;
7. volume = box(length + width, height);
8. volume = box(num1, num2 + num3);
Consider the following functions:
int find(int num) {
int first, second;
first = num * num;
second = first + num;
if (second > 100) num = first / 10;
else
return num + 2; }
int discover(int one, int two)
{
num = first / 20;
int secret = 0;
for (int i = one; i < two; i++)
secret = secret + i * i;
return secret; }
What is the output of each of the following program segments?
cout << find(15) << endl;
cout << discover(3, 9) << endl;
cout << find(10) << " " << discover(10, find(10)) << endl;
x=12;y=8;
cout << discover(y, x) << endl;
Consider the following function prototypes:
int func1(int, double);
double func2(string, int, double);
char func3(int, int, double, char);
string join(string, string);
Answer the following questions:
A. How many parameters does the function func1 have? What is the type of the
function func1?
B. How many parameters does function func2 have? What is the type of function
func2?
C. How many parameters does function func3 have? What is the type of function
func3?
D. How many parameters does function join have? What is the type of function join?
How many actual parameters are needed to call the function func1? What is the
type of each actual parameter, and in what order should you use these parameters
in a call to the function func1?
E. Write a C++ statement that prints the value returned by the function func1 with
the actual parameters 3 and 8.5.
F. Write a C++ statement that prints the value returned by function join with the
actual parameters "John" and "Project Manager", respectively.
G. Write a C++ statement that prints the next character returned by function func3.
(Use your own actual parameters.)
Write the definition of a function that takes as input a char value and returns true if
the character is uppercase; otherwise, it returns false.
Consider the following function:
int mystery(int x, double y, char ch)
{
if (x == 0 && ch > 'A')
return(static_cast<int>(pow(y, 2)) + static_cast<int>(ch));
else if (x > 0)
return(x + static_cast<int>(sqrt(y)) - static_cast<int>(ch));
else return(2 * x + static_cast<int>(y) - static_cast<int>(ch));
}
What is the output of the following C++ statements?
a. cout << mystery(0, 6.5, 'K') << endl;
b. cout << mystery(4, 16.0, '#') << endl;
c. cout << 2 * mystery(-11, 13.8, '8') << endl;
Consider the following function:
int secret(int m, int n)
{
int temp = 0;
for (int i = 1; i < abs(n); i++)
temp = temp + i * m;
return temp;
}
What is the output of the following C++ statements? a. cout << secret(3, 6) << endl;
b. cout << secret(5, -4) << endl;
c. What does the function secret do?
Write the definition of a function that takes as input the three numbers. The
function returns true if the first number to the power of the second number equals the
third number; otherwise, it returns false. (Assume that the three numbers are of type
double.)
Consider the following C++ program:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int temp = 0;
for (int counter = 1; counter <= 100; counter++) if (pow(floor(sqrt(counter / 1.0)), 2.0) == counter)
temp = temp + counter;
cout << temp << endl;
return 0; }
a. What is the output of this program?
b. What does this program do?
What is the output of the following program?
#include <iostream>
using namespace std;
int mystery(int x, int y, int z);
int main() { cout << mystery(7, 8, 3) << endl;
cout << mystery(10, 5, 30) << endl;
cout << mystery(9, 12, 11) << endl;
cout << mystery(5, 5, 8) << endl;
cout << mystery(10, 10, 10) << endl;
return 0; } int mystery(int x, int y, int z)
{ if (x <= y && x <= z)
return (y + z - x);
else if (y <= z && y <= x)
return (z + x - y);
else return (x + y - z);
}
Write the definition of a function that takes as input three decimal numbers and
returns the first number multiplied by the second number to the power of the
third number.
Consider the following C++ function:
int mystery(int num)
{
int y = 1;
if (num == 0)
return 1;
else if (num < 0)
return -1;
else for (int count = 1; count < num; count++)
y = y * (num - count);
return y; }
What is the output of the following statements? a. cout << mystery(6) << endl;
b. cout << mystery(0) << endl;
c. cout << mystery(-5) << endl;
d. cout << mystery(10) << endl;
e. How would you use a return statement in a void function?
f. Why would you want to use a return statement in a void function?
Identify the following items in the programming code shown below:
a. Function prototype, function heading, function body, and function definitions.
b. Function call statements, formal parameters, and actual parameters.
c. Value parameters and reference parameters.
d. Local variables and global variables.
e. Named constants.
#include <iostream> //Line 1
using namespace std; //Line 2
const double NUM = 3.5; //Line 3
int temp; //Line 4
void func(int, double&, char); //Line 5
int main() //Line 6
{ //Line 7
int num; //Line 8
double one; //Line 9
char ch; //Line 10
func(num, one, ch); //Line 11
cout << num << " " << one << " " << ch << endl; //Line 12
func(16, one, '%'); //Line 13
cout << num << " " << one << " " << ch << endl; //Line 14
return 0; //Line 15
} //Line 16
void func(int first, double& second, char ch) //Line 17
{ //Line 18
int num; //Line 19
double y; //Line 20
int u; //Line 21
num = 2 * first; //Line 22 y = second * first; //Line 23
u = static_cast<int> (ch); //Line 24
second = num + y * u; //Line 25
} //Line 26
What is the output of the following program?
#include <iostream>
using namespace std;
void func1();
void func2();
int main() { int num;
cout << "Enter 1 or 2: ";
cin >> num;
cout << endl;
cout << "Take ";
if (num == 1)
func1();
else if
(num == 2)
func2();
else
cout << "Invalid input. You must enter a 1 or 2" << endl;
return 0; } void func1( )
{ cout << "Programming I." <<endl;
} void func2( )
{ cout << "Programming II." << endl;
}
a. What is the output if the input is 1?
b. What is the output if the input is 2?
c. What is the output if the input is 3?
d. What is the output if the input is -1?
Write the definition of a void function that takes as input a decimal number and
outputs 3 times the value of the decimal number. Format your output to two
decimal places.
6
Write the definition of a void function that takes as input two decimal numbers. If
the first number is nonzero, it outputs the second number divided by the first
number; otherwise, it outputs a message indicating that the second number
cannot be divided by the first number because the first number is 0.
Write the definition of a void function with three reference parameters of type int,
double, and string. The function sets the values of the int and double variables to
0 and the value of the string variable to the empty string.
Write the definition of a void function that takes as input two parameters of type
int, say sum and testScore. The function updates the value of sum by adding the
value of testScore. The new value of sum is reflected in the calling environment.
What is the output of the following program?
#include <iostream>
using namespace std;
void find(int a, int& b, int& c);
int main() {
int one, two, three;
one = 5;
two = 10;
three = 15;
find(one, two, three);
cout << one << ", " <<
find(two, one, three);
cout << one << ", " <<
find(three, two, one);
cout << one << ", " <<
find(two, three, one);
cout << one << ", " <<
return 0; }
two << ",
two << ",
two << ",
two << ",
" << three << endl;
" << three << endl;
" << three << endl;
" << three << endl;
void find(int a, int& b, int& c) {
int temp;
c = a + b;
temp = a;
a = b;
b = 2 * temp;
}
What is the output of the following program?
#include <iostream>
using namespace std;
int x;
oid summer(int&, int);
void fall(int, int&);
int main() {
int intNum1 = 2;
int intNum2 = 5;
x = 6;
summer(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
fall(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
return 0; }
void summer(int& a, int b) {
int intNum1;
intNum1 = b + 12;
a = 2 * b + 5;
b = intNum1 + 4; }
void fall(int u, int& v) {
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v; }
In the following program, number the marked statements to show the order in
which they will execute (the logical order of execution). Also, what is the output if
the input is 10?
#include <iostream>
using namespace std;
int secret(int, int);
void func(int x, int& y);
int main() {
int num1, num2; num1 = 6;
cout << "Enter a positive integer: ";
cin >> num2;
cout << endl;
cout << secret(num1, num2) << endl;
num2 = num2 - num1;
cout << num1 << " " << num2 << endl;
func(num2, num1);
cout << num1 << " " << num2 << endl;
return 0;
}
int secret(int a, int b) {
int d; d = a + b;
b = a * d;
return b;
}
void func (int x, int& y) {
int val1, val2;
val1 = x + y;
val2 = x * y;
y = val1 + val2;
cout << val1 << " " << val2 << endl;
}
Consider the following program:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void traceMe (double x, double y); int main() {
double one, two;
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
traceMe(one, two);
traceMe(two, one);
return 0; }
void traceMe(double x, double y) { double z;
if (x != 0) z = sqrt(y) / x;
else
{
cout << "Enter a nonzero number: ";
cin >> x;
cout << endl;
z = floor(pow(y, x));
} cout << fixed << showpoint << setprecision(2);
cout << x << ", " << y << ", " << z << endl; }
a. What is the output if the input is 3 625?
b. What is the output if the input is 24 1024?
c. What is the output if the input is 0 196?
The function traceMe in Exercise 30 outputs the values of x, y, and z. Modify the
definition of this function so that rather than print these values, it sends the values
back to the calling environment and the calling environ- ment prints these values.
What is the output of the following code fragment? (Note: alpha and beta are int
variables.)
alpha = 5;
beta = 10;
if (beta >= 10)
{
int alpha = 10;
beta = beta + alpha;
cout << alpha << ' ' << beta << endl;
}
cout << alpha << ' ' << beta << endl;
Consider the following program. What is its exact output? Show the values of the
variables after each line executes, as in Example 6-13.
#include <iostream>
using namespace std;
void funOne(int& a);
int main()
{ int num1, num2;
num1 = 10; //Line 1
num2 = 20; //Line 2
cout << "Line 3: In main: num1 = " << num1 <<",num2="<<num2<<endl; //Line 3
funOne(num1);
cout << "Line 5: In main after funOne: num1 = "<< num1 << ", num2 = " << num2 << endl; //Line 4
return 0; //Line 5
}
void funOne(int& a)
{
int x = 12;
int z;
z = a + x; //Line 7
cout << "Line 8: In funOne: a = " << a << ", x = " << x << ", and z="<<z<<endl; //Line 8
x=x+5; //Line 9
cout << "Line 10: In funOne: a = " << a << ", x = " << x<< ", and z="<<z<<endl; //Line 10
a=a+8; //Line 11
cout << "Line 12: In funOne: a = " << a<< ", x = " << x<< ", and z = " << z << endl; //Line 12
}
What is the output of the following program?
#include <iostream>
using namespace std;
void tryMe(int& v);
int main()
{
int x = 8;
for (int count = 1; count < 5; count++)
tryMe(x);
return 0;
}
void tryMe(int& v)
{
static int num = 2;
if (v % 2 == 0)
{
num++;
v = v + 3;
}
else
{ num--;
v = v + 5;
}
cout << v << ", " << num << endl;
}
Consider the following function prototype:
void funcDefaultParam(double x = 7.3, int y = 4, char z = '*');
Which of the following function calls is correct?
1. funcDefaultParam();
2. funcDefaultParam(2.8);
3. funcDefaultParam(3.2, 0, 'h');
4. funcDefaultParam(9.2, '*');
5. funcDefaultParam(7, 3);
Consider the following function definition:
void defaultParam(int num1, int num2 = 7, double z = 2.5)
{
int num3;
num1 = num1 + static_cast<int>(z);
z = num2 + num1 * z;
num3 = num2 - num1;
cout << "num3 = " << num3 << endl;
}
What is the output of the following function calls?
A. defaultParam(7);
B. defaultParam(8, 2);
C. defaultParam(0, 1, 7.5);
D. defaultParam(1, 2, 3.0);
Recommended