Key for Homework 8 on Array Basics Q A Reason ----------------------------------------------------------------------------------- 1 2 In C++, array indices start at 0. 2 1 The initialization places the three given values (5, 10 and 15) into the cells of the array Gamma[] in the order listed. 3 4 Loop 1 accesses the non-existent cell status[10]. Loop 2 misses the cell status[0]. Loop 3 combines the errors in loop 1 and loop 2. Loop 5 misses status[0] and accesses two non-existent cells. 4 3 The array stores counters, so it must be of type int. There are counts for 256 different characters, so the dimension must be 256 (or greater). For questions 5 and 6 you just need to trace the execution of the code; here's the final state of the array Arr[]: index 0 1 2 3 4 value 2 3 8 9 6 5 2 6 5 The value in Arr[3] would be 9. 7 3 The loop prints cells 4, 3, 2 and 1 but not cell 0. 8 5 Loop 3 prints 2*Beta[0], 2*Beta[1], etc. For questions 9 through 11, you must consider what each function does. FillEm() initializes the two arrays, so both must be passed by reference. Copy() copies the contents of the second array into the first, so the first must be passed by reference but the second should not. 9 3 Answers 1, 5 and 6 make no sense at all. As for 2, NEVER use the ampersand for a parameter that's an array; an array is passed by reference by default. 10 3 Same logic as for 9. 11 4 As noted above, this one should NOT be passed by reference since the function Copy() does not change it. The only alternative is to pass the array by constant reference. 12 7 The code in 2 is not allowed. 13 2 You need two parallel arrays to hold names and majors; both arrays must have space for data for up to MAXSTUDENTS students. 14 6 In general it's better to pass string variables either by reference or constant reference in order to avoid copying the whole string. Since, in this case, the string is guaranteed to be relatively short, the time required to copy it would be acceptable. Passing by pure reference is not acceptable since there is no reason that the function should be allowed to modify the actual parameter. 15 1 You want to stop at the last cell of the array. 16 2 You need to compare the current element of the array Majors[] to the string you're looking for. 17 6 By specification, the function is supposed to return the INDEX if a match is found (so it really should be Idx). 18 4 There are 100 rows, each holding 50 integers. 19 3 Remember, indexing starts at 0. 20 3 Assignment isn't supported for arrays, but you could manage the copying by a for loop, which could be contained in a user-defined function.