1
h08
CS16 W18
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"

h08: Chapter 7: Arrays and Chapter 9 Pointers

ready? assigned due points
true Tue 02/13 09:00AM Tue 02/20 08:00PM

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 (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).

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

1.(5 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.(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 arr[7] = {5};
for (int i = 0; i < 7; i++)
	cout << arr[i] + i <<" ";

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

4.(5 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;

5.(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…”

6.(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