Study Guide 7 Answers

  1. e
  2. same
  3. double doubleArray[100]
  4. 0 to 9
  5. cout << doubleArray[j] or cout << doubleArray[j] << cout
  6. int coins[] = { 1, 5, 10, 25, 50, 100 }; or int coins[] { 1, 5, 10, 25, 50, 100 }
  7. d
  8. a
  9. c
  10. l
  11. They all work
  12. address
  13. b
  14. a
  15. a
  16. b
  17. a
  18. b
  19. c
  20. int scores[3][5]; (see Test Yourself)
  21. scores[1][3] = 100; (see Test Yourself). The box highlighted in red explains why int scores[1][3] = 100; doesn't work.
  22. b or d: When a stack is implemented as an array, the bottom of the stack is at index 0.
  23. b
  24. e
  25. b
  26. b
  27. a, b, and c
  28. b or c: Multi-dimensional arrays must include the size of each dimension except the first dimension.
  29. b
  30. The memory needed to store all automatic or local variables is automatically allocated when the variable comes into scope and is automatically deallocated when the variable goes out of scope. So, in the case of automatic variables, scope and memory allocation are tightly coupled, but the two are nevertheless distinct concepts. The get_data function suffers from a memory allocation problem not a scope problem. (See Scope vs. Memory Allocation.) A variable's name has scope, but its address does not.

    The memory needed to store the array named data is allocated and deallocated based on scope. But the problem is that the memory is deallocated, not that the variable goes out of scope. We'll look at three ways of fixing the error below. All three corrections use a variable named data and it does go out of scope when the get_data function ends, but the memory in the solutions is not deallocated when the function ends.

    int* get_data()
    {
    	int data[100];
    	// fill the data array
    	return data;
    }
    1. The function returns the address of data (the syntax is correct - the name of an array is an address), but datais a local automatic variable whose memory is deallocated when the function ends. The function caller is left trying to access deallocated memory that is available for reuse.
    2. There are three ways to fix the problem (see Returning One-Dimensional Arrays From Functions for example code fragments)
      1. Make data static (i.e., add the "static" keyword to the variable definition). The array name data still goes out of scope (and becomes inaccessible) when the function ends. But the memory is not deallocated because the array is static rather than automatic.
      2. Define the array in the calling scope and pass it in as a pointer argument. The argument named data is a local variable and goes out of scope when the function returns, but the array that data points at is defined in a different scope and is not deallocated.
      3. Allocate the array dynamically with the new operator. The local variable data goes out of scope when the function ends but the memory allocated by new is not deallocated.
    int* get_data()
    {
    	static int data[100];
    	// fill the data array
    	return data;
    }
    int* get_data(int* data)
    {
    	// fill the data array
    	return data;
    }
     
    int* get_data()
    {
    	int* data = new int[100];
    	// fill the data array
    	return data;
    }
  31. answer: b. False: Unlike Java, C++ arrays are simple, fundamental data types with no members (variables or functions). Specifically, C++ arrays do NOT support .length.
  32. See sg02_answers, question 28.

    The Problem The Solution
    void f(foo* data, int x, int y)
    {
    	. . . 
    }
    foo temp = data[x];
    data[x] = data[y];
    data[y] = temp;
     

    The type of the array is foo, so the type of the temporary variable, temp in this example, must also be foo - int will not work. The name of the temporary variable is not significant.

  33. Answer: a
  34. Answer: b. sizeof(data) is still the total number of bytes in data, but data is now a pointer. If we changed new char[100] to new char[200], the value saved in count wouldn't change. So, the size of a pointer divided by the size of a character isn't meaningful. We wouldn't normally write the code fragment appearing in this problem, but it helps us understand the difference between the code appearing in the previous problem and the next.
  35. Answer: b. Unlike the previous question, we might naturally try to use the code fragment illustrated here. Unfortunately, it's incorrect as described above.
  36. void copy(char* destination, char* source, int size)
    {
    	for (int i = 0; i < size; i++)
    		destination[i] = source[i];
    }
  37. This problem asks you to write a function that is similar to the average.cpp demonstration program appearing in the textbook.
    //double average(double[] scores, int size)
    double average(double* scores, int size)
    {
    	double sum = 0;
    	for (int i = 0; i < size; i++)
    		sum += scores[i];
    	return sum / size;
    }

    Some issues to consider: