{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SOLUTION_HW4

SOLUTION_HW4 - CS31: ,LosAngeles Dr.JohnA.Rohr March2,2010...

This preview shows pages 1–2. Sign up to view the full content.

CS 31: INTRODUCTION TO COMPUTER SCIENCE I Computer Science Department University of California, Los Angeles Dr. John A. Rohr March 2, 2010 HOMEWORK 4 SOLUTION Practice with Pointers 1. The program has two problems. First, because of the order of precedence of the operators, the expression  *ptr + 1 = '*'    means  (*ptr) + 1 = '*' .  The expression   (*ptr) + 1   evaluates to the character  '*'  which cannot appear  on the left side of an assignment statement.  When corrected to  *(ptr+1) = '*' , the expression  then means  *(&arr[1]) = '*' , which means   arr[1] = '*' Second, the while loop prints elements in the wrong order, doesn't access arr[2], and tries to  access arr[-1]. One way to fix this is the following:                       for (ptr = arr;  ptr < arr + 3;  ptr++)                              cout  <<  *ptr; 2. The findUnordered function puts the correct value in pBad, but pBad is a copy of the caller's  variable ptr, so findUnordered has no effect on the variable ptr. The parameter pBad must be  passed by reference, not by value:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern