1
h05
CS16 F18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h05: Chapter 7 and 10:Arrays and Pointers

ready? assigned due points
true Thu 11/08 12:30PM Wed 11/14 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB 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 three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Read Chapter 7.1 - 7.2 up to page 397. Also read this handout on Pointers and Memory: http://cslibrary.stanford.edu/102/PointersAndMemory.pdf Please turn in the physical copy of your homework in person. Write in PEN or DARK PENCIL.

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
FOR BEST RESULTS, PRINT THIS PAGE AS A PDF, THEN PRINT THE PDF

INCLUDE QUESTIONS ON ARRAYS AND CSTRINGS

1.(10 pts) What is the output of the following code? If there’s an error that will not allow an output, point it out. 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;  }

2.(10 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 count : codes) {
  if ( (count/2) < 50 )
    cout << count << endl;
  else cout << "invalid" << endl; }

3.(10 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;

4.(5 pts) Describe in your own words what the code in the previous question does. Your description of the code should be as abstract as possible. For example..”the above code sorts the elements of the array in ascending order…”

5.(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. The function should not return anything. 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]<<" "

The output of the above code should be: 50 40 30 20 10

6.(5 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)++;