SOLUTION_HW4

SOLUTION_HW4 - CS 31: INTRODUCTION TO COMPUTER SCIENCE I...

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

View Full Document Right Arrow Icon
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 , 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: void findUnordered(double arr[], int n, double* p) 3. The declaration double* pa;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

SOLUTION_HW4 - CS 31: INTRODUCTION TO COMPUTER SCIENCE I...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online