CS 215-401 Fall 2014 Homework 2 Answers R6.6 (3 pts): What is wrong with the following loop? int values[10]; for (int i = 1; i <= 10; i++){ values[i] = i * i; } Explain two ways of fixing the error. Solution R6.6: The problem with this loop is that it tries to access a location outside of the bounds of the array. The array has 10 locations, numbered 0 through 9, but this code tries to access values[10], which doesn’t exist. It also starts with position 1 of the array, which is probably not what was intended (because arrays start at position 0, not 1). The first way to fix it is to change the “1” to a “0” and the “<=” to “<” in the loop condition: int values[10]; for (int i = 0; i < 10; i++) { values[i] = i * i; } The second way to fix it is to change the “1” to “0” and the “10” to “9” in the loop condition: int values[10]; for (int i = 0; i <= 9; i++) { values[i] = i * i; } R6.14 (1 pt each): For the operations on partially filled arrays of integers below, provide the header of a function (everything but the body). Do not implement the functions. a) b) c) d) e) Sort the elements in decreasing order. Print all elements, separated by a given string. Count how many elements are less than a given value. Remove all elements that are less than a given value. Place all elements that are less than a given value in another array. The headers of the described functions. a. b. c. d. e. void sort_decreasing(int arr[], int size) void print_with_string(int arr[], int size, string sep) int num_items_below(int arr[], int size, int value) void remove_items_less_than(int arr[], int size, int value) void copy_less_than(int source[], int first_size, int target[], int second_size, int value) R6.24 (1 pt each): True or false? a) All elements of an array are of the same type. b) Arrays cannot contain strings as elements. c) Two-dimensional arrays always have the same number of rows and columns. d) Elements of different columns in a two-dimensional array can have different types. e) A function cannot return a two - dimensional array. f) All array parameters are reference parameters. g) A function cannot change the dimensions of a two-dimensional array that is passed as a parameter. The answers for this question are: a. True b. False. Strings are just fine, dude. c. False. They might be the same, but don’t have to be. d. False. The entire array is the same data type. e. True. f. True. g. True. R6.26 (1 pt each): True or false? a) All elements of a vector are of the same type. b) Vector subscripts (indices) must be integers. c) Vectors cannot contain strings as elements. d) Vectors cannot use strings as subscripts (indices). e) All vector parameters are reference parameters. f) A function cannot return a vector. g) A function cannot change the length of a vector that is a reference parameter. The answers for this question are: a. b. c. d. e. f. g. True True False True False False False, it can add or remove elements. R7.3 (2 pts): What does the following code print? double a = 1000; double b = 2000; double* p = &a; doubl e* q = p; b = *q; p = &b; a = *p + *q; cout << a << " " << b << endl; The output will be: 2000 1000 R7.7 (1 pt each): Suppose the array primes, defined as double primes[] = { 2, 3, 5, 7, 11, 13 }; starts at the memory location 20300, What are the values of a) Primes b) *primes c) primes + 4 d) *(primes + 4) e) primes[4] f) &primes[4] The answers for this question are: a. b. c. d. e. f. 20300 2 20332 11 11 20332 R7.10 (5 pts): Pointers are addresses and have a numerical value. You can print out the value of a pointer with cout << unsigned(p). Write a program to compare p, p + 1, q and q + 1, where p is an int* and q is a double*. . Explain the results. Note: You do not need to turn in the code for R7.10. Explaining the results is the important part. The program itself might look something like that shown below. When you look at the output, you will notice that the addresses increment by 4 bytes for the integer pointer and by 8 bytes for the double pointer. This is because the double pointer uses twice as much memory as the integer. The answer for this question is: #include <iostream> using namespace std; int main() { int a = 3; double b = 4.0; int* p = &a; double* q = &b; for (int i = 0; i < 5; i++) { cout << p << " " << q << endl; p++; q++; } return 0; } R7.19 (4 pts): What happens if you forget to delete an object that you obtained from heap? What happens if you delete it twice? The answer for this question is: If you forget to delete an object obtained from the heap, that memory will not be released (deallocated) so that other programs can use it. If this is done many times (as might be the case if a piece of software runs constantly in a loop, allocating new memory each time and never deleting it), then the program may use up all of the free memory. Once you delete an object, it is gone. You cannot use it again, and if you try to delete it a second time, you will get an error message. X1 (5 pts): Write (and show) a function that takes an integer n and returns a pointer to an array of the first n cubes (0, 1, 8, 27, and so on). The array should be dynamically allocated from the heap. R8.3 (2 pts): What happens if you try to open a file for reading that doesn't exist? What happens if you try to open a file for writing that doesn't exist? The answer for this question is: If you open a file for reading and the file doesn’t exist, then the file object will be created, but all input operations will fail. If you open a file for writing and the file doesn’t exist, then the file is created with 0 length. R8.9 (4 pts): How can you convert the string "3.14" into the floating-point number 3.14? How can you convert the floating-point number 3.14 into the string "3.14"? The answer for this question is: You can do the conversion with the istringstream and ostringstream classes. The following code will convert "3.14" to a floating-point number, and then the reverse: string pi_string1 = "3.14"; istringstream instr; instr.str(pi_string1); // string -> double double pi_double1; instr >> pi_double1; double pi_double2 = 3.14; ostringstream outstr; // double -> string outstr << pi_double2; string pi_string2 = outstr.str();