Key for Homework 7 on Arrays and Structs Q A Reason ----------------------------------------------------------------------------------- 1 5 3 has syntax errors. 2 uses the aggregate initialization syntax from the notes, so that's OK. 1 initializes each field separately, which is OK. 2 1 Straight from the notes... the only operations on struct variables that are supported by default are assignment, passing as a parameter, and returning as a function value. 3 6 The variables You and Me have int fields named Age, and those can be incremented and compared using the relational operators. 4 1 2 confuses assignment with equality. 3 uses the wrong syntax to access the field Name. 5 8 1 uses == to compare structs, which is unsupported. 2 confuses assignment with equality. 3 uses <= to compare structs, which is unsupported. 6 2 Personnel is an array of IDType variables, so Personnel[17] refers to the IDType variable at index 17. So Personnel[17].Name refers to the Name field of the IDType variable at index 17. 1 would require searching the array to match the IDNum field. 3 doesn't apply since what's being printed is a field, not the entire struct variable. 7 2 Personnel is an array of IDType variables, so Personnel[17] refers to the IDType variable at index 17. Since assignment of struct variables is supported... 1 would require reversing the sides of the assignment. 3 would require three assignments, using a temporary IDType variable. 8 7 1 is wrong because the function call would be identical whether the parameter were passed by value, reference, or constant reference. 2 is true because the parameter would not have to be copied. 3 is true for the same reason. 9 7 Passing by constant reference vs by reference has NO effect on how much memory is used, nor on how much time is required. 10 1 Straightforward... it's true that x and y each contain an array, but the assignment is still legal (and the array field of x will be copied correctly into the array field of y). 2 doesn't apply because the variables being assigned are structs, not arrays. 11 2 1 would be correct if the field Foo wasn't an array. 2 is correct because assignment is not supported for arrays (unless they are wrapped in structs). 12 5 Just trace the code... 13 4 Ditto... 14 1 This is just a matter of struct field access syntax. 15 3 So is this one, BUT we have a hierarchical organization. A Board variable contains a field named Dimensions of type Size, and the Dimensions field contains a field named Width. So, to access Width, we must say: |----------------------------- name of variable | | |-------------------- name of the Size field of the variable | | | | |---------- name of the Width field of the Size field oneBoard.Dimensions.Width 1 doesn't work because the only fields of oneBoard are named Dimensions, Kind and smoothSurfaces. 2 doesn't work because Size is the type name, not the (field) variable name. For questions 16 and 17, the output code contains a typo -- it should refer to the field "Dimensions", not "Size". However, that doesn't have anything to do with the questions asked. 16 2 Lumber[Idx] refers to the Board variable at index Idx, so Lumber[Idx].Kind refers to the Kind field of that variable. 17 3 The value of the field Kind will be one of the enumerated values for the type WoodKind, not a string. 18 4 The function is returning values that belong to the enum type Status. 19 1 In this case, the index being checked IS negative. 20 1 In this case, the index being checked IS too big. 21 2 Given the description of what it does, the function needs to modify the actual parameter, so T must be passed by reference. 1 wouldn't compile because the function changes T. 3 would compile, but the function would only be changing a copy of the actual parameter. 22 3 This should assign a value to the field "Time" of the variable T. 23 1 The function doesn't need to modify either the formal parameter Leg1, or the corresponding actual parameter. Pass by value would work, but it would involve making a copy of the actual parameter; since that's a struct variable it's better to pass it by constant reference and avoid the copying. 24 2 Same logic as for question 21. 25 3 The function has to make sure that the destination for Leg1 is the same as the origin for Leg2.