Skip to content

Latest commit

 

History

History

Chapter06

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Chapter06: Functions

What is the difference between a parameter and an argument?

Indicate which of the following functions are in error and why. Suggest how you might correct the problems.

(a) int f() {
        string s;
        // . . .
        return s;
    }
(b) f2(int i) { /* . . . */ }
(c) int calc(int v1, int v1) /* . . . */ }
(d) double square(double x) return x * x;

Write and test your own version of fact.

Write a function that interacts with the user, asking for a number and generating the factorial of that number. Call this function from main.

Write a function to return the absolute value of its argument.

Explain the differences between a parameter, a local variable, and a local static variable. Give an example of a function in which each might be useful.

Write a function that returns 0 when it is first called and then generates numbers in sequence each time it is called again.

Write a header file named Chapter6.h that contains declarations for the functions you wrote for the exercises in § 6.1 (p. 205).

Write your own versions of the fact.cc and factMain.cc files. These files should include your Chapter6.h from the exercises in the previous section. Use these files to understand how your compiler supports separate compilation.

Using pointers, write a function to swap the values of two ints. Test the function by calling it and printing the swapped values.

Write and test your own version of reset that takes a reference.

Rewrite the program from exercise 6.10 in § 6.2.1 (p. 210) to use refer- ences instead of pointers to swap the value of two ints. Which version do you think would be easier to use and why?

Assuming T is the name of a type, explain the difference between a function declared as void f(T) and void f(T&).

Give an example of when a parameter should be a reference type. Give an example of when a parameter should not be a reference.

Explain the rationale for the type of each of find_char’s parameters In particular, why is s a reference to const but occurs is a plain reference? Why are these parameters references, but the char parameter c is not? What would happen if we made s a plain reference? What if we made occurs a reference to const?

The following function, although legal, is less useful than it might be. Identify and correct the limitation on this function:

bool is_empty(string& s) { return s.empty(); }

Write a function to determine whether a string contains any capital letters. Write a function to change a string to all lowercase. Do the parameters you used in these functions have the same type? If so, why? If not, why not?

Write declarations for each of the following functions. When you write these declarations, use the name of the function to indicate what the function does. (a) A function named compare that returns a bool and has two parameters that are references to a class named matrix. (b) A function named change_val that returns a vector iterator and takes two parameters: One is an int and the other is an iterator for a vector.

Given the following declarations, determine which calls are legal and which are illegal. For those that are illegal, explain why.

double calc(double);
int count(const string &, char);
int sum(vector<int>::iterator, vector<int>::iterator, int);
vector<int> vec(10);
(a) calc(23.4, 55.1);
(b) count("abcda", ’a’);
(c) calc(66);
(d) sum(vec.begin(), vec.end(), 3.8);

When should reference parameters be references to const? What hap- pens if we make a parameter a plain reference when it could be a reference to const?

Write a function that takes an int and a pointer to an int and returns the larger of the int value or the value to which the pointer points. What type should you use for the pointer?

Write a function to swap two int pointers.

Write your own versions of each of the print functions presented in this section. Call each of these functions to print i and j defined as follows:

int i = 0, j[2] = {0, 1};

Explain the behavior of the following function. If there are problems in the code, explain what they are and how you might fix them.

void print(const int ia[10])
{
    for (size_t i = 0; i != 10; ++i)
        cout << ia[i] << endl;
}

Write a main function that takes two arguments. Concatenate the sup- plied arguments and print the resulting string.

Write a program that accepts the options presented in this section. Print the values of the arguments passed to main.

Write a function that takes an initializer_list and pro- duces the sum of the elements in the list.

In the second version of error_msg that has an ErrCode parameter, what is the type of elem in the for loop?

When you use an initializer_list in a range for would you ever use a reference as the loop control variable? If so, why? If not, why not?

Compile the version of str_subrange as presented on page 223 to see what your compiler does with the indicated errors.

When is it valid to return a reference? A reference to const?

Indicate whether the following function is legal. If so, explain what it does; if not, correct any errors and then explain it.

int &get(int *arry, int index) { return arry[index]; }
int main() {
    int ia[10];
    for (int i = 0; i != 10; ++i)
        get(ia, i) = i;
}

Write a recursive function to print the contents of a vector.

What would happen if the stopping condition in factorial were

if (val != 0)

In the call to factorial, why did we pass val - 1 rather than val--?

Write the declaration for a function that returns a reference to an array of ten strings, without using either a trailing return, decltype, or a type alias.

Write three additional declarations for the function in the previous ex- ercise. One should use a type alias, one should use a trailing return, and the third should use decltype. Which form do you prefer and why?

Revise the arrPtr function on to return a reference to the array.

Explain the effect of the second declaration in each one of the following sets of declarations. Indicate which, if any, are illegal.

(a) int calc(int, int);
    int calc(const int, const int);
(b) int get();
    double get();
(c) int *reset(int *);
    double *reset(double *);

Which, if either, of the following declarations are errors? Why?

(a) int ff(int a, int b = 0, int c = 0);
(b) char *init(int ht = 24, int wd, char bckgrnd);

Which, if any, of the following calls are illegal? Why? Which, if any, are legal but unlikely to match the programmer’s intent? Why?

char *init(int ht, int wd = 80, char bckgrnd = ’ ’);
(a) init();
(b) init(24,10);
(c) init(14, ’*’);

Give the second parameter of make_plural (§ 6.3.2, p. 224) a default argument of ’s’. Test your program by printing singular and plural versions of the words success and failure.

Which one of the following declarations and definitions would you put in a header? In a source file? Explain why.

(a) inline bool eq(const BigInt&, const BigInt&) {...}
(b) void putValues(int *arr, int size);

Rewrite the isShorter function from § 6.2.2 (p. 211) to be inline.

Review the programs you’ve written for the earlier exercises and decide whether they should be defined as inline. If so, do so. If not, explain why they should not be inline.

Would it be possible to define isShorter as a constexpr? If so, do so. If not, explain why not.

Revise the program you wrote in the exercises in § 6.3.2 (p. 228) that used recursion to print the contents of a vector to conditionally print information about its execution. For example, you might print the size of the vector on each call. Compile and run the program with debugging turned on and again with it turned off.

Explain what this loop does and whether it is a good use of assert:

string s;
while (cin >> s && s != sought) { }  // empty body
assert(cin);

What is a candidate function? What is a viable function?

Given the declarations for f from page 242, list the viable functions, if any for each of the following calls. Indicate which function is the best match, or if the call is illegal whether there is no match or why the call is ambiguous.

(a) f(2.56, 42)
(b) f(42)
(c) f(42, 0)
(d) f(2.56, 3.14)

Write all four versions of f. Each function should print a distinguish- ing message. Check your answers for the previous exercise. If your answers were incorrect, study this section until you understand why your answers were wrong.

Given the following declarations,

void manip(int, int);
double dobj;

what is the rank (§ 6.6.1, p. 245) of each conversion in the following calls?

(a) manip(’a’, ’z’);
(b) manip(55.4, dobj);

Explain the effect of the second declaration in each one of the following sets of declarations. Indicate which, if any, are illegal.

(a) int calc(int&, int&);
    int calc(const int&, const int&);
(b) int calc(char*, char*);
    int calc(const char*, const char*);
(c) int calc(char*, char*);
    int calc(char* const, char* const);

Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type.

Write four functions that add, subtract, multiply, and divide two int values. Store pointers to these functions in your vector from the previous exercise.

Call each element in the vector and print their result.