HW_4 - CS 31 INTRODUCTION TO COMPUTER SCIENCE I Computer...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: CS 31: INTRODUCTION TO COMPUTER SCIENCE I Computer Science Department University of California, Los Angeles Dr. John A. Rohr February 24, 2010 HOMEWORK 4 Practice with Pointers Objective : Learn to use pointers in C++ programs. Assignment : There are ten problems below. In problems that ask you to change code, make the few changes necessary to fix the code without changing its overall approach. For example, don't fix the program in the first problem by changing it to int main() { cout << "[*] " << endl; } Due Date : Tuesday March 2, 2010: 9:00 PM : Projects must be submitted electronically. PROBLEMS 1. This program is supposed to write [*], but it doesn't work correctly. Find all of the bugs and show a fixed version of the program: int main() { char arr[3] = { 'P', 'Q', 'R' }; char* ptr = arr; *ptr = '['; // Set arr[0] to [ ptr + 1 = '*'; // Set arr[1] to * ptr += 2; ptr[0] = ']'; // Set arr[2] to ] while (ptr >= arr) { ptr--; cout << " " << *ptr; // Print values } cout << endl; } CS 31: Introduction to Computer Science I: Winter 2010: Homework 4 Page 2 2. The findUnordered function is supposed to find the first item in an array that is greater than the element preceding it, and set the parameter pBad to point to that item, so the caller can know the location of the item. The function will not work correctly. Explain the problem and how to fix it. Changes can be made to the function only. Also, the way the function is called by the main routine cannot be changed. Therefore changes such as making the function return a value as the function result are not allowed. function return a value as the function result are not allowed....
View Full Document

This note was uploaded on 03/08/2010 for the course COM SCI 31 taught by Professor Kinsey during the Spring '98 term at UCLA.

Page1 / 6

HW_4 - CS 31 INTRODUCTION TO COMPUTER SCIENCE I Computer...

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

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