EECS 183 Fall 2015 Exam 2 Free Response (134 points) Closed Book Closed Notes No Electronic Devices Closed Neighbor Turn off Your Cell Phone, even in your backpack We will confiscate all electronic devices that we see or hear including cell phones, calculators, smart watches, etc. Instructions: Read Carefully! 1. You may have one personally handwritten 3 5 notecard. 2. Some questions are not simple. Therefore, read carefully. 3. Assume all code and code fragments are syntactically valid, unless otherwise specified. 4. Assume/use only the standard C++11 and Python 2.7. 5. If a read into a variable fails, assume that the variable is changed to 0. 6. In all the given code, if any character looks like a space, it is one. 7. Write clearly. If we cannot read your writing, it will be marked wrong. This includes indentation and curly braces. 8. This course operates under the rules of the College of Engineering Honor Code. Your signature endorses the pledge below. After you finish your exam, sign below: I have neither given nor received aid on this examination, nor have I concealed any violations of the Honor Code. SIGN your name above Print your full name above, zero on exam if not legible or if not your full name Print your uniqname above, zero on exam if not legible or if not your uniqname Page Number Question Number Points Possible 3 1 24 4 2 15 5 3 25 6 4 30 8 5 40
Page 2 of 10 You can use this area as scratch paper for working out problems.
1. (24 points total, 2 points each) Crossword: put a letter in each box according to the clues below. 0 Page 3 of 10 1 e n c a p s u l a t 2 i o n 4 3 5 6 7 8 9 10 11 12 Across (C++) 1. Packaging code and data together is referred to as encapsulation. 4. Member variables and functions in classes can be directly accessed from outside of their class if they are. 5. This operator is used to call class member functions given an instance of a class. 6. Given the following code snippet: Dog snoopy; then snoopy is an instance of the Dog. 9. The keyword used to make class data members available only to member functions. 10. This constructor has no parameters. 11. When calling a function, all arrays are passed by. Down (Python) 0. Python is rather than compiled. 2. The data type in Python that is the equivalent of an int in C++. 3. This data structure stores any non-negative number of elements, which can be of different types. 7. A(n) is a way to specify a portion of a list in Python. 8. Put this in the blank space below to convert 1989 to a string. s = (1989) 12. Put this at the end of a print statement to suppress a new line.
Page 4 of 10 2. (15 points) Implement the following function, which returns the index of an element in an array. You do not have to use all the space available. /** * Requires: size > 0, and size is the size of array * Modifies: Nothing. * Effects : Returns the index of the first occurrence of value in * array, or -1 if it is not in array. * Example : The following code will print: 1-1 * int array[] = { 1, 2, 3, 2, 4 ; * cout << indexof(array, 5, 2) << " " << indexof(array, 5, 5); */ int indexof(int array[], int size, int value) {
Page 5 of 10 3. (25 points) An upper triangular matrix is a square matrix where every element that is strictly below the diagonal is equal to zero. For example, the matrix on the left is not upper triangular, while the one on the right is. 1 2 3 1 2 3 4 5 6 0 5 6 7 8 9 0 0 9 The function below, copyuppertriangular(), copies the elements that are in or above the diagonal from frommatrix to tomatrix, setting the remaining elements in tomatrix to zero, resulting in an upper triangular matrix. For example, if frommatrix is the matrix on the left above, tomatrix will be set to the matrix on the right. Fill in the definition, assuming that matrices are stored with the top row first and the bottom row last. // Requires: size > 0 and size <= MAX_SIZE, and size is the number of rows // and columns in both frommatrix and tomatrix // Modifies: tomatrix // Effects : Copies the elements in and above the diagonal in frommatrix to // tomatrix. Sets the remaining elements in tomatrix to zero. Does // not change any elements that are outside the valid size x size // portion of tomatrix. void copyuppertriangular(const int frommatrix[max_size][max_size], int tomatrix[max_size][max_size], int size) {
Page 6 of 10 4. (30 points) Professor Dorf wants to determine the class s average scores for Projects 3 and 4. She has a file called scores.txt with the following format: Email Project3 Project4 hjpotter@hogwarts.edu 62 65 rweasley@hogwarts.edu 50 56 granger@hogwarts.edu 67.75 67.75 The first line is a header with column labels, and each remaining line consists of an email address, a score for Project 3, and a score for Project 4. Professor Dorf started writing a C++ program to compute the class averages. However, she's too busy to finish it and needs your help. Fill in the rest of the program on the next page. Your code should work for any number of students greater than zero, and it should read every line in the file. You may assume that there is at least one student entry in scores.txt and that all entries are properly formatted. You may write code in any blank space, but you are not required to use every blank space or all of the blank space provided. Assume the following appears at the top of the C++ program file: #include <iostream> #include <string> #include <fstream> using namespace std; // Continued on next page... Kittens!!!
Page 7 of 10 //...Continued from previous page. int main() { ifstream ins; if ( ) { cout << "Could not open file" << endl; return 0; double p3sum = 0; double p4sum = 0; int numstudents = 0; while ( ) { cout << "Project 3 average: " << p3sum / numstudents << endl; cout << "Project 4 average: " << p4sum / numstudents << endl; ins.close();
Page 8 of 10 5. (40 points total) On the eecs183.org website, there is a map that has a dot for every city where students or staff members are from. The map stores coordinates, composed of a latitude and longitude, for each city. Suppose the Coordinates class is declared as follows in Coordinates.h: class Coordinates { public: Coordinates(); void setlatitude(double newlat); double getlatitude(); void setlongitude(double newlong); double getlongitude(); private: double latitude; double longitude; ; Valid latitude values range from -90 to 90, inclusive, and valid longitude values range from -180 to 180, inclusive. The default constructor sets both the latitude and longitude to 0. a) The getlatitude() member function is a getter for the latitude data member. In the box below, write the full definition of getlatitude() as it would appear in Coordinates.cpp. Assume that all necessary header files have been included in Coordinates.cpp. b) The setlatitude() member function is a setter for the latitude data member. However, if its argument is not a valid latitude, it makes no changes to the latitude data member. Write the full definition of setlatitude() as it would appear in Coordinates.cpp.
Page 9 of 10 Suppose the map also contains a Location class, declared as follows in Location.h: class Location { public: Location(); Location(string inname, Coordinates incoordinates); void write(ostream &outs); private: string name; Coordinates geolocation; ; c) The default location in the map has the name "Ann Arbor", latitude 42.3, and longitude -83.7. In the box below, write the full definition of the default constructor for Location that sets its data members to the values above, as it would appear in Location.cpp. Assume that all necessary header files have been included in Location.cpp, and that the Coordinates class has been implemented correctly. d) Now write the definition for the write() member function as it would appear in Location.cpp. It should write the name, latitude, and longitude, separated by spaces. No newline should be written. For example, for the default location, "Ann Arbor 42.3-83.7" would be written to the output stream (without the quotes).
Page 10 of 10 e) Write a test suite for the Location class below. Finish implementing test.cpp (using the main() function) to test the Location class. You do not have to use all the blank space provided. Assume that the Coordinates and Location classes have been implemented correctly. Your test suite should do the following: Use the default Location constructor to create a default location. Print out the Location to standard output, using the appropriate member function, followed by a newline. Create a second Location instance with name "Diagon Alley", latitude 51.7, longitude -0.42. Print out the second Location to standard output, followed by a newline. #include <iostream> #include "Coordinates.h" #include "Location.h" using namespace std; int main() { // Write your test code below