Unformatted text preview:

CS 215 - Fundamentals of Programming IISpring 2011 - Homework 920 pointsOut: February 23Due: February 28 (Monday) at the beginning of class; no late work acceptedThis homework consists of some sample problems similar to what will be on the written midtermexam. (Note: this is not an exhaustive sample of all possible question types. All previous homeworkproblems should be considered samples as well.) We will go over these problems during the examreview on Monday.1. (3 points) A complex number has the form a+bi, where a (the real part) and b (the imaginarypart) are real numbers, and i represents the√−1. The following (partial) class Complex modelsa complex number.class Complex{public:Complex (double a = 0, double b = 0);// Explicit value constructor: a+bi; default value is 0+0idouble RealPart() const; // return real partdouble ImaginaryPart() const; // return imaginary partprivate:double real, imag; // (real)+(imag)i};Write the prototype and function definition for a member function Magnitude that returns themagnitude of a complex number. The magnitude of a complex number of the form a+bi is√a2+ b2. The C++ functions for square root and exponentiation are sqrt and pow, respectively.2. (3 points) Write a recursive function Reverse that has three parameters: an array of integersarr and indexes first and last. This function is to reverse the contents of the array in range[first, last), i.e., from arr[first] to arr[last-1]. For example, if the array arr contains values1 2 3 4 5, then after the function call Reverse(arr, 1, 4), arr’s values will be 1 4 3 2 5.3. (4 points) Write a function CountItem that receives two parameters, an STL vector of strings vand an string search value t, and returns the number of elements in v that are equal to t. AssumestrVector is a vector of strings. Write a code fragment using CountItem that will display thenumber of times "C++" appears in strVector.14. (10 points) Consider the following (partial) class that implements a dynamic array of integers.A dynamic array behaves just like a static array except that its size is set at run-time duringconstruction. After it is constructed, it cannot change size.class DynamicArray{public:DynamicArray (int initSize = 1, int initValue = 0);// Explicit value constructor:// creates array of initSize integers to store data// elements initialized to initValueint & operator[] (int index); // indexing operator returns arr[i]private:int *arr; // pointer to data storagesize_t capacity; // number of elements in the array};Answer the following questionsa. Write the function definition for the explicit-value constructor that behaves as indicated inthe comments above.b. Write the prototypes and function definitions for the destructor, copy constructor, and as-signment operator (operator=) for this


View Full Document

UE CS 215 - Homework

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Homework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?