CS 1704 Fall 2003 Homework 1 Answers Q A Reason -------------------------------------------------------------------------------- For questions 1 through 3, the basic fact is that the scope of an indentifier extends from the point of its declaration to the end of the enclosing scope (compound statement). 1 5 . In this case, there would be NO enclosing scope, so the identifier is in scope to the end of the file. 2 7 Now the identifier is only in scope within the body of main(). So, unless main() calls f() or g() and passes the identifier by refenrence they will have no access to it. 3 1 Same logic as for question 2. For questions 4 through 9, you just need to trace the data, paying close attention to what's passed by value and what's passed by reference. 4 2 5 4 6 4 7 1 8 1 There is no declaration of i1 within the body of G(), not even as a formal parameter, so this reference must bind to the global declaration of i1. 9 5 The only declaration of i3 that's in scope at line 22 is as a formal parameter, which was used to receive i1 from main by reference. So, the variable that's actually being modified here was declared in line 3, which isn't a choice. 10 1 The compiler detects only syntax errors. 11 6 If it compiles, the syntax is correct. If it doesn't produce correct output, the design logic must be wrong. 12 2 If it compiles, the syntax is correct. If it produces correct output on one test case, the design may be correct, or not. 13 3 By putting dummy values in the unused "tail" of the array you make it easier to recognize when you've accidentally strayed there, for instance during a sort operation. It has no effect on space; it takes MORE time, not less. It doesn't prevent errors, just makes them easier to detect. 14 3 1 won't handle a name containing spaces. 2 suffers the same problem. 4 ignores the first tab twice, losing the first character of the name. 5 reads the entire line as the ID. 15 1 2 uses get() to read an integer; get() returns a single character value. 3 reads the rest of the line, as a string. 16 9 2 almost works, but getline() doesn't discard leading whitespace, so the last call to getline() to read the Grade will be incorrect. 17 3 You can do aggregate assignment of struct variables (1) or you can copy the fields one-by-one (2). The first is preferred. 18 3 When input failure occurs, the value of the targeted variable may be changed. If you've gone to the trouble to initialize the array cells, you shouldn't mess one of them up now. 19 4 20 7 2 and 3 use get() and getline() when an integer is being read. 21 5 Same idea. 22 1 By declaring the parameter P[] as "const" you specify that the function must treat it as a constant. So the assignment P[Idx] = Dummy is not allowed. 23 2 The function is supposed to change the actual parameter, so P must be passed by reference, not by value. This will compile and run, but the array of points will not be changed by this function. 24 3 The sort function will consider ALL of the array cells, not just the ones that are actually in use to store real data. When one of the unused cells is compared to a used cell, it may or may not cause a swap, depending on what dummy values were used and what the real data looks like. 25 1