Chapter 8 Exercise Set 0: Chapter Review and Exploration

  1. Make a file named swaps.cpp that includes all three versions of swap functions, wont_swap, will_swap, and will_swap_with_pointers, presented in the chapter with calling code in main. Compile and then run your program several times, observing what happens.

  2. Write a function named str_len that takes a C string as an argument and returns the number of characters in the string.

  3. Write a function named shuffle, that takes an array of char elements and the number of elements in the array as arguments, and randomly “shuffles” the elements using the Fisher–Yates shuffle algorithm. Note: this is an “in place” shuffle, which means the array passed in as an argument will be modified by the function.

  4. Write another version of shuffle that takes the same arguments as in the previous exercise, but which leaves the array passed in unchanges, and instead returns a new array (hint You’ll need to use malloc) with the same elements in a random order.

  5. Write a program named talkin_trash.cpp that illustrates the two concepts of arrays being memory unsafe and garbage in, garbage out discussed in the chapter. Be creative. The best solution to this exercise will be a program example that helps others reading your code understand these two concepts.