1 |
h06 |
CS16 M18 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h06: Arrays and Pointers
ready? | assigned | due | points |
---|---|---|---|
true | Tue 07/31 08:00AM | Tue 08/07 11:00AM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).
Reading: Sections 7.1 - 7.2, 9.1
- Read Chapter 7.1 - 7.2 (up to and including the const parameter modifier) and Chapter 9.1 (up to and including uses of the assignment operator).
- It’s recommended to write and observe the output for the code segments when working on this homework.
- (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
- Filling in your name and umail address.
- Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
- Filling in your name and umail address.
- (5 pts) What is the output of the following code? If there's an error that will not allow an output, point it out and briefly justify your answer.
int arr[5]; for (int i = 0; i < 5; i++) { if (i < 3) arr[i] = 'a'; else arr[i] = 'z'; cout << arr[i] << endl; }
- (8 pts) What is the output of the following code? If there's an error that will not allow an output, point it out.
int arr[7] = {5}; for (int i = 0; i < 7; i++) cout << arr[i] + i << " ";
- (5 pts) What is the output of the following code? If there's an error that will not allow an output, point it out.
int codes[] = {44, 66, 83, 973, -977}; for (int i = 0; i < 5; i++) { if ( (codes[i]/2) < 50 ) cout << codes[i] << endl; else cout << "invalid" << endl; }
- (6 pts) Draw a diagram to show how the content of the array 'nums' changes in memory after every line of the following code is executed. Start by showing the elements of the array in memory when the array is initialized. Every time the element of an array changes, you may indicate the change by crossing out the old value and writing in the new value.
int nums[] = {44, 66, 83}; int tmp = nums[0]; nums[0] = nums[1]; nums[1] = nums[2]; nums[2]= tmp;
- (10 pts) Write the definition of a function named 'reverse' that takes two parameters: an integer array and the number of elements of the array. The function should reverse the order of elements of the array with the given size. The function should not return anything. For full credit, include the entire function definition including the correct parameters and return type. See below for an example of expected outcome when the reverse function is called:
int nums[] = {10,20,30,40,50}; reverse(nums, 5); // The order of elements should be reversed after this for(int i = 0; i < 5; i++) cout << nums[i] << " ";
- (6 pts) Draw a pointer diagram to demonstrate how the state of memory changes as the following code is executed. Cross out old values/arrows and draw new ones. Is this program likely to result in a segmentation fault? If so, why?
int num = 10, *ptr1 = &num, *ptr2=0; if (ptr2) ptr1 = ptr2; else if(ptr1) ptr2 = ptr1; (*ptr1)++;