Each problem below requires a separate program to solve, except for (3), which can simply be a typed answer. E-mail all solutions, with screen shots of at least 2 sample runs of each program,
(1) 22pts.
Searching unsorted arrays and using templates.
Define and implement a function with three parameters: an unsorted array of integers, the number of elements in the array, and a target value. Write code that efficiently returns the position of the occurrence of the target
closest to the
middle
of the array. For example, in the array {3, 5, 6, 5, 4, 7, 5, 6} with target value 6, the function should return 2.
Next, alter the function to take in any type of array of variables or objects using templates. Submit this version of the function.
(2) 22pts.
Searching sorted arrays, using recursion, and using templates.
Define and implement a recursive binary search function that takes four parameters: a sorted array of integers, a lower bound on the index to search, an upper bound on the index to search, and a target value. Next, alter the function to take in any type of sorted array of variables or objects using templates. Submit this version of the function.
(3) 28pts.
Operator overloading and class design.
Consider the SunnyDays class header and implementation on the next page. days is a dynamic array that holds the number of hours that the sun was out in a given day. numDays indicates the number of elements in the days array. Expand the class by defining the following functions in the header and writing implementation code for the class.
- A function
display
that prints out the elements in the days array
- A function
pctCloudy
that returns the percentage of days in the array
without
sun
- A
destructor
that frees up any memory used by the class
- An
overloaded assignment operator
that creates a separate (deep) copy of the object’s member data
- An
overloaded addition operator
that creates a new array that contains all elements from each of the arrays
- An
overloaded greater-than operator
that indicates if the left-hand side has a higher percentage of sunny days
(4) 12pts.
Inheritance.
This is a question for which you will not generate any code—just answer the questions fully with explanations.
[1] You created a BaseCharacter class for a video game and are now writing code for the sequel. In the original, the character could walk, run, and jump. In the sequel, you are creating a DerivedCharacter class for a character that can still walk, run, and jump, but also can fly. Would you use public or private inheritance? Why?
[2] You created a BaseCharacter class for a video game and are now writing code for the sequel. In the original, the character could walk, run, and jump. In the sequel, you are creating a DerivedCharacter class for a character that can still walk and jump, can also fly, but cannot run. Would you use public or private inheritance? Why?
[3] You created a BaseCharacter class for a video game and are now writing code for the sequel. In the original, the character could walk, run, and jump. In the sequel, you are creating a DerivedCharacter class for a character that can still walk, jump, and run, but runs at multiple speeds. Would you use public or private inheritance? Why?
(5) 24pts.
Vectors and Stacks
A vector can be used as a stack, utilizing its
.push_back
and
.pop_back
functions. Write a program to read in a file and write it to a new file in reversed order (similar to your last lab). The required procedure is as follows: using a
while
loop, read in a file, character by character, into a character vector (use
.get
on your file object to read the file one character at a time, and use
.push_back
on your vector to push back that character onto the vector). In a separate loop, in each iteration write the last character (use
.back) of the vector to the output file and call
.pop_back
to remove it from the vector.
(6) 22pts.
Maps
In this program, you will read through a file to count the number of times each word appears. To do so, create a map where the key is a string and the value is an int. Read through the file one word (string separated by spaces) at a time, using the >> operator. Check if the word exists in the map. If it does not exist, insert it as the key, making the value 1. If it does exist, update the value for that key to increase by one. Finally, print contents of the map to the console. Finally, you may earn up to 2 points bonus for checking for and removing all punctuation on words before entering into the map and ignoring case in the map (for example,
dog,
and
Dog.
and
dog
would all simply be translated to
dog
or
DOG
before entering into the map.