Difference between revisions of "ME333 W2011 Quiz 2 Solutions"

From Mech
Jump to navigationJump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The main function begins by statically allocating an array of doubles of length 6, indexed by arr[0] through arr[5]. arr is a pointer to (address of) the first element of the array, equivalent to &arr[0]. &arr[k] is a pointer to (address of) the kth element of the array, and k should be between 0 and 5. arr[k] contains the (type double) contents of the kth element of the array, and *arr contains the (type double) contents of the first element of the array. The contents of the kth element of the array can be (awkwardly) referenced as *(&arr[k]).
The main() function begins by statically allocating an array of doubles of length 6, given by arr[0] through arr[5]. arr is a pointer to (address of) the first element of the array, equivalent to &arr[0]. &arr[k] is a pointer to (address of) the kth element of the array, and k should be between 0 and 5. arr[k] contains the (type double) contents of the kth element of the array, and *arr contains the (type double) contents of the first element of the array. The contents of the kth element of the array can be (awkwardly) referenced as *(&arr[k]).


Q1: initialize(arr) calls the function "initialize" with a pointer to first element of arr. This function just goes through a while loop and puts the value of i into each arr[i] for i = 0..5. So arr[1] = 1.0, arr[2] = 2.0, arr[3] = 3.0, arr[4] = 4.0.
Q1: initialize(arr) calls the function "initialize" with a pointer to first element of arr. This function just goes through a while loop and puts the value of i into each arr[i] for i = 0..5. So arr[0] = 0.0, '''arr[1] = 1.0, arr[2] = 2.0, arr[3] = 3.0, arr[4] = 4.0''', arr[5] = 5.0.


Q2. The function "add" is called with a pointer to (address of) the first element of arr (let's call this local variable containing the address "foo", to emphasize that this function has its own local copy of the address); the value 2.0 (the value of arr[2]), so the local variable addval (of type double) is set to 2.0; and 6 (from ARRAY_SIZE), so the local variable num (of type int) is set to 6. In "add", we execute a for loop using the counter i, which takes values from 0 to 5 (starts at 0 and runs the contents of the loop while i<num). So the contents of the addresses foo[0..5] are set to the previous contents plus addval, which is 2.0. Since the addresses of foo[0..5] are the same as the addresses of arr[0..5], we now have arr[1] = 3.0, arr[2] = 4.0, arr[3] = 5.0, arr[4] = 6.0.
Q2. The function "add" is called with a pointer to (address of) the first element of arr (let's call this local variable containing the address "foo", to emphasize that this function has its own local copy of the address); the value 2.0 (the value of arr[2]), so the local variable addval (of type double) is set to 2.0; and 6 (from ARRAY_SIZE), so the local variable num (of type int) is set to 6. In "add", we execute a for loop using the counter i, which takes values from 0 to 5 (starts at 0 and runs the contents of the loop while i<num). So the contents of the addresses foo[0..5] are set to the previous contents plus addval, which is 2.0. Since the addresses of foo[0..5] are the same as the addresses of arr[0..5], we now have arr[0] = 2.0, '''arr[1] = 3.0, arr[2] = 4.0, arr[3] = 5.0, arr[4] = 6.0''', arr[5] = 7.0.


Q3. The function "add" is called with a pointer to (address of) arr[2] (let's call this local variable containing the address foo); the value 5.0, so the local variable addval is set to 5.0; and 2, so the local variable num is set to 2. Note that in our function call
Q3. The function "add" is called with a pointer to (address of) arr[2] (let's call this local variable containing the address foo); the value 5.0, so the local variable addval is set to 5.0; and 2, so the local variable num is set to 2. Note that in our function call
Line 10: Line 10:


the third argument takes the contents of the address arr (this is equivalent to arr[0]), which is 2.0 (a double), and "casts" it as a type int, to match what is required by the function "add". So "add" changes the contents of foo[0] to foo[0] + 5.0, and the contents of foo[1] to foo[1] + 5.0. Since the address of foo[0] is the address of arr[2], this means that arr[2] back in the main function has had its contents changed to arr[2] + 5.0. The address of foo[1] is the same as the address of arr[3], so arr[3] is now arr[3] + 5.0. arr[0], arr[1], arr[4], and arr[5] remain unchanged.
the third argument takes the contents of the address arr (this is equivalent to arr[0]), which is 2.0 (a double), and "casts" it as a type int, to match what is required by the function "add". So "add" changes the contents of foo[0] to foo[0] + 5.0, and the contents of foo[1] to foo[1] + 5.0. Since the address of foo[0] is the address of arr[2], this means that arr[2] back in the main function has had its contents changed to arr[2] + 5.0. The address of foo[1] is the same as the address of arr[3], so arr[3] is now arr[3] + 5.0. arr[0], arr[1], arr[4], and arr[5] remain unchanged.
So now arr[1] = 3.0, arr[2] = 9.0, arr[3] = 10.0, arr[4] = 6.0.
So now arr[0] = 2.0, '''arr[1] = 3.0, arr[2] = 9.0, arr[3] = 10.0, arr[4] = 6.0''', arr[5] = 7.0.

Latest revision as of 04:02, 18 January 2011

The main() function begins by statically allocating an array of doubles of length 6, given by arr[0] through arr[5]. arr is a pointer to (address of) the first element of the array, equivalent to &arr[0]. &arr[k] is a pointer to (address of) the kth element of the array, and k should be between 0 and 5. arr[k] contains the (type double) contents of the kth element of the array, and *arr contains the (type double) contents of the first element of the array. The contents of the kth element of the array can be (awkwardly) referenced as *(&arr[k]).

Q1: initialize(arr) calls the function "initialize" with a pointer to first element of arr. This function just goes through a while loop and puts the value of i into each arr[i] for i = 0..5. So arr[0] = 0.0, arr[1] = 1.0, arr[2] = 2.0, arr[3] = 3.0, arr[4] = 4.0, arr[5] = 5.0.

Q2. The function "add" is called with a pointer to (address of) the first element of arr (let's call this local variable containing the address "foo", to emphasize that this function has its own local copy of the address); the value 2.0 (the value of arr[2]), so the local variable addval (of type double) is set to 2.0; and 6 (from ARRAY_SIZE), so the local variable num (of type int) is set to 6. In "add", we execute a for loop using the counter i, which takes values from 0 to 5 (starts at 0 and runs the contents of the loop while i<num). So the contents of the addresses foo[0..5] are set to the previous contents plus addval, which is 2.0. Since the addresses of foo[0..5] are the same as the addresses of arr[0..5], we now have arr[0] = 2.0, arr[1] = 3.0, arr[2] = 4.0, arr[3] = 5.0, arr[4] = 6.0, arr[5] = 7.0.

Q3. The function "add" is called with a pointer to (address of) arr[2] (let's call this local variable containing the address foo); the value 5.0, so the local variable addval is set to 5.0; and 2, so the local variable num is set to 2. Note that in our function call

add(&arr[2], 5.0, ((int) *arr));

the third argument takes the contents of the address arr (this is equivalent to arr[0]), which is 2.0 (a double), and "casts" it as a type int, to match what is required by the function "add". So "add" changes the contents of foo[0] to foo[0] + 5.0, and the contents of foo[1] to foo[1] + 5.0. Since the address of foo[0] is the address of arr[2], this means that arr[2] back in the main function has had its contents changed to arr[2] + 5.0. The address of foo[1] is the same as the address of arr[3], so arr[3] is now arr[3] + 5.0. arr[0], arr[1], arr[4], and arr[5] remain unchanged. So now arr[0] = 2.0, arr[1] = 3.0, arr[2] = 9.0, arr[3] = 10.0, arr[4] = 6.0, arr[5] = 7.0.