Homework #1 Solution

$35.00

Description

Here is a C++ class definition for an abstract data type Map from stringto double,representing the concept of a function mapping strings to doubles. (For example, we could represent a collection of students and their GPAs: “Fred” maps to 2.956, “Ethel” maps to 3.538, “Lucy” maps to 2.956, etc.) We’ll call the strings the keys and the doubles the values. No two keys in the map are allowed to be the same (e.g., so “Fred” appears no more than once), although two keys might map to the same value (as in the example, where both of the keys “Fred” and “Lucy” map to the value

2.956). To make things simpler for you, the case of letters in a string matters, so that the strings Fredand fReDare not considered duplicates.

class Map

{

public:

Map(); // Create an empty map (i.e., one with no key/value pairs)

bool empty(); // Return true if the map is empty, otherwise false.

int size(); // Return the number of key/value pairs in the map.

bool insert(const std::string& key, const double& value);

// If key is not equal to any key currently in the map, and if the

// key/value pair can be added to the map, then do so and return true. // Otherwise, make no change to the map and return false (indicating // that either the key is already in the map, or the map has a fixed // capacity and is full).

bool update(const std::string& key, const double& value);

// If key is equal to a key currently in the map, then make that key no // longer map to the value it currently maps to, but instead map to // the value of the second parameter; return true in this case.

// Otherwise, make no change to the map and return false.

bool insertOrUpdate(const std::string& key, const double& value);

// If key is equal to a key currently in the map, then make that key no // longer map to the value it currently maps to, but instead map to

// the value of the second parameter; return true in this case. // If key is not equal to any key currently in the map and if the

// key/value pair can be added to the map, then do so and return true. // Otherwise, make no change to the map and return false (indicating // that the key is not already in the map and the map has a fixed

// capacity and is full).

bool erase(const std::string& key);

// If key is equal to a key currently in the map, remove the key/value

// pair with that key from the map and return true. Otherwise, make // no change to the map and return false.

bool contains(const std::string& key);

// Return true if key is equal to a key currently in the map, otherwise // false.

bool get(const std::string& key, double& value);

// If key is equal to a key currently in the map, set value to the

// value in the map that that key maps to, and return true. Otherwise, // make no change to the value parameter of this function and return // false.

bool get(int i, std::string& key, double& value);

// If 0 <= i < size(), copy into the key and value parameters the // key and value of one of the key/value pairs in the map and return

// true. Otherwise, leave the key and value parameters unchanged and

// return false. (See below for details about this function.)

void swap(Map& other);

// Exchange the contents of this map with the other one.

};

(When we don’t want a function to change a key or value parameter, we pass that parameter by constant reference. Passing it by value would have been perfectly fine for this problem, but we chose the const reference alternative because that will be more suitable after we make some generalizations in a later problem.)

The three-argument getfunction enables a client to iterate over all elements of a Mapbecause of this property it must have: If nothing is inserted into or erased from the map in the interim, then calling that version of getrepeatedly with the first parameter ranging over all the integers from 0 to size()-1 inclusive will copy into the other parameters every key/value pair from the map exactly once. The order in which key/value pairs are copied is up to you. In other words, this code fragment

Map m;

m.insert(“A”, 10);

m.insert(“B”, 44);

m.insert(“C”, 10);

string all;

double total = 0;

for (int n = 0; n < m.size(); n++)

{

string k;

double v;

m.get(n, k, v);

all += k;

total += v;

}

cout << all << total;

3/12/ CS32 Homework 1, Winter

must result in the output being exactly one of the following: ABC64,ACB64,BAC64,BCA64,CAB64,or CBA64,and the client can’t depend on it being any particular one of those six. If the map is modified between successive calls to the three-argument form of get,all bets are off as to whether a particular key/value pair is returned exactly once.

If nothing is inserted into or erased from the map in the interim, then calling the three-argument form of getrepeatedly with the same value for the first parameter each time must copy the same key into the second parameter each time, so that this code is fine:

Map gpas;

gpas.insert(“Fred”, 2.956);

gpas.insert(“Ethel”, 3.538);

double v;

string k1;

assert(gpas.get(1,k1,v) && (k1 == “Fred” || k1 == “Ethel”)); string k2;

assert(gpas.get(1,k2,v) && k2 == k1);

Notice that the empty string is just as good a string as any other; you should not treat it in any special way:

Map gpas;

gpas.insert(“Fred”, 2.956);

assert(!gpas.contains(“”));

gpas.insert(“Ethel”, 3.538);

gpas.insert(“”, 4.000);

gpas.insert(“Lucy”, 2.956);

assert(gpas.contains(“”));

gpas.erase(“Fred”);

assert(gpas.size() == 3 && gpas.contains(“Lucy”) && gpas.contains(“Ethel”) && gpas.contains(“”));

Here’s an example of the swapfunction:

Map m1;

m1.insert(“Fred”, 2.956);

Map m2;

m2.insert(“Ethel”, 3.538);

m2.insert(“Lucy”, 2.956);

m1.swap(m2);

assert(m1.size() == 2 && m1.contains(“Ethel”) && m1.contains(“Lucy”) &&

m2.size() == 1 && m2.contains(“Fred”));

When comparing keys for insert,update,insertOrUpdate,erase,contains,and the two-argument form of get,just use the ==or !=operators provided for the string type by the library. These do case-sensitive comparisons, and that’s fine.

Here is what you are to do:

  1. Determine which member functions of the Map class should be const member functions (because they do not modify the Map), and change the class declaration accordingly.

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 3/12

3/12/ CS32 Homework 1, Winter

  1. As defined above, the Map class allows the client to use a map that contains only std::strings as keys and doubles as values. Someone who wanted to modify the class to contain keys or values of another type, such as keys being ints and values being ints, would have to make changes in many places. Modify the class definition you produced in the previous problem to use a typedef-defined type for all keys wherever the original definition used a std::stringfor that purpose, and a typedef-defined type for all values wherever the original definition used a doublefor that purpose. Here’s an example of a use of typedef:

typedef int Number; // define Number as a synonym for int

int main()

{

Number total = 0;

Number x;

while (cin >> x)

total += x;

cout << total << endl;

}

To modify this code to sum a sequence of longs or of doubles, we need make a change in only one place: the typedef.

You may find the example using typedefin Appendix A.1.8 of the textbook useful.

To make the grader’s life easier, we’ll require that everyone use the same synonym as their typedef-defined names: You must use the name KeyTypefor the name of the type used for keys, and ValueTypefor the name of the type used for values, with exactly those spellings and case.

  1. Now that you have defined an interface for a map class where the key and the value types can be easily changed, implement the class and all its member functions in such a way that the key/value pairs in a map are contained in a data member that is an array. (Notice we said an array, not a pointer. It’s not until problem 5 of this homework that you’ll deal with a dynamically allocated array.) A map must be able to hold a maximum of DEFAULT_MAX_ITEMS distinct keys, where

const int DEFAULT_MAX_ITEMS = 200;

(Hint: Define a structure type containing a member of type KeyType and a member of type ValueType. Have Map’s array data member be an array of these structures.)

Test your class for a Map that maps std::strings to doubles. Place your class definition, non-inline function prototypes, and inline function implementations (if any) in a file named Map.h,and your non-inline function implementations (if any) in a file named Map.cpp.(If we haven’t yet discussed inline, then if you haven’t encountered the topic yourself, all your functions will be non-inline, which is fine.)

You may add any private data members or private member functions that you like, but you must not add anything to or delete anything from the public interface you defined in the previous problem, nor may you change the function signatures. There is one exception to this: If you wish, you may add a public member function with the signature void dump() const.

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 4/12

3/12/ CS32 Homework 1, Winter

The intent of this function is that for your own testing purposes, you can call it to print information about the map; we will never call it. You do not have to add this function if you don’t want to, but if you do add it, it must not make any changes to the map; if we were to replace your implementation of this function with one that simply returned immediately, your code must still work correctly. The dumpfunction must not write to cout,but it’s allowed to write to cerr.

Your implementation of the Map class must be such that the compiler-generated destructor, copy constructor, and assignment operator do the right things. Write a test program named testMap.cppto make sure your Map class implementation works properly. Here is one possible (incomplete) test program:

#include “Map.h”

#include <iostream>

#include <cassert>

using namespace std;

int main()

{

Map m; // maps strings to doubles

assert(m.empty());

ValueType v = -1234.5;

assert( !m.get(“abc”, v) && v == -1234.5); // v unchanged by get failure

m.insert(“xyz”, 9876.5);

assert(m.size() == 1);

KeyType k = “hello”;

assert(m.get(0, k, v) && k == “xyz” && v == 9876.5); cout << “Passed all tests” << endl;

}

Now change (only) the two typedefs in Map.hso that the Map type will now map ints to std::strings. Make no other changes to Map.h,and make no changes to Map.cpp.Verify that your implementation builds correctly and works properly with this alternative main routine (which again, is not a complete test of correctness):

#include “Map.h”

#include <iostream>

#include <cassert>

using namespace std;

int main()

{

Map m; // maps ints to strings

assert(m.empty());

ValueType v = “Ouch”;

assert( !m.get(42, v) && v == “Ouch”); // v unchanged by get failure

m.insert(123456789, “Wow!”);

assert(m.size() == 1);

KeyType k = 9876543;

assert(m.get(0, k, v) && k == 123456789 && v == “Wow!”);

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 5/12

3/12/ CS32 Homework 1, Winter

cout << “Passed all tests” << endl;

}

You may need to flip back and forth a few times to fix your Map.hand Map.cppcode so that the only change to those files you’d need to make to change a map’s key and value types is to the two typedefs in Map.h.(When you turn in the project, have the typedefs in Map.hspecify the key type to be std::stringand the value type to be double.)

Except in a typedef statement in Map.h,the word doublemust not appear in Map.hor Map.cpp.Except in a typedef statement and in the context of #include <string>in Map.h,the word stringmust not appear in Map.hor Map.cpp.

(Implementation note 1: If you declare another structure to help you implement a Map, put its declaration in Map.h(and newMap.hfor Problem 5), since it is not intended to be used by clients for its own sake, but merely to help you implement the Map class. In fact, to enforce clients not using that structure type, don’t declare it outside of the Map class; instead, declare that helper structure in the private section of Map. Although it would probably be overkill for this structure to have anything more than two public data members, if for some reason you decide to declare any member functions for it that need to be implemented, those implementations should be in Map.cpp(and newMap.cppfor Problem 5).)

(Implementation note 2: The swapfunction is easily implementable without creating any additional array or additional Map.)

  1. Now that you’ve implemented the class, write some client code that uses it. We might want a class that keeps track of people enrolled in a weight loss program and their weights in pounds. Implement the following class:

#include “Map.h”

class WeightMap

{

public:

WeightMap(); // Create an empty weight map.

bool enroll(std::string name, double startWeight);

// If a person with the given name is not currently in the map, // there is room in the map, and the startWeight is not negative, // add an entry for that person and weight and return true. // Otherwise make no change to the map and return false.

double weight(std::string name) const;

// If a person with the given name is in the map, return that // person’s weight; otherwise, return -1.

bool adjustWeight(std::string name, double amt);

// If no person with the given name is in the map or if

// weight() + amt is negative, make no change to the map and return

// false. Otherwise, change the weight of the indicated person by

// the given amount and return true. For example, if amt is -8.2, // the person loses 8.2 pounds; if it’s 3.7, the person gains 3.7

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 6/12

3/12/ CS32 Homework 1, Winter

// pounds.

int size() const; // Return the number of people in the WeightMap.

void print() const;

// Write to cout one line for every person in the map. Each line // has the person’s name, followed by one space, followed by that // person’s weight.

private:

// Some of your code goes here.

};

Your WeightMap implementation must employ a data member of type Map that uses the typedefs KeyTypeand ValueTypeas synonyms for std::stringand double,respectively. (Notice we said a member of type Map, not of type pointer to Map.) Except for the typedefs, you must not make any changes to the Map.hand Map.cppfiles you produced for Problem 3, so you must not add any member functions to the Map class. Each of the member functions enroll,weight,adjustWeight,size,and printmust delegate as much of the work that they need to do as they can to Map member functions. (In other words, they must not do work themselves that they can ask Map member functions to do instead.) If the compiler-generated destructor, copy constructor, and assignment operator for WeightMap don’t do the right thing, declare and implement them. Write a program to test your WeightMap class. Name your files WeightMap.h,WeightMap.cpp,and testWeightMap.cpp.

The words forand whilemust not appear in WeightMap.hor WeightMap.cpp,except in the implementation of WeightMap::printif you wish. The characters [(open square bracket) and *must not appear in WeightMap.hor WeightMap.cpp,except in comments if you wish. You do not have to change std::stringto KeyTypeand doubleto ValueTypein WeightMap.h and WeightMap.cppif you don’t want to (since unlike Map, which is designed for a wide variety of key and value types, WeightMap is specifically designed to work with strings and doubles).

  1. Now that you’ve created a map type based on arrays whose size is fixed at compile time, let’s change the implementation to use a dynamically allocated array of objects. Copy the three files you produced for problem 3, naming the new files newMap.h,newMap.cpp,and testnewMap.cpp.Update those files by either adding another constructor or modifying your existing constructor so that a client can do the following:

Map a(1000); // a can hold at most 1000 key/value pairs

Map b(5); // b can hold at most 5 key/value pairs

Map c; // c can hold at most DEFAULT_MAX_ITEMS key/value pairs KeyType k[6] = { a list of six distinct values of the appropriate type };

ValueType v = a value of the appropriate type;

// No failures inserting pairs with 5 distinct keys into b for (int n = 0; n < 5; n++)

assert(b.insert(k[n], v));

// Failure if we try to insert a pair with a sixth distinct key into b

assert(!b.insert(k[5], v));

// When two Maps’ contents are swapped, their capacities are swapped // as well:

a.swap(b);

assert(!a.insert(k[5], v) && b.insert(k[5], v));

Since the compiler-generated destructor, copy constructor, and assignment operator no longer do the right thing, declare them (as public members) and implement them. Make no other changes to the public interface of your class. (You are free to make changes or additions to the private members and to the implementations of the member functions.) Change the implementation of the swapfunction so that the number of statement executions when swapping two maps is the same no matter how many key/value pairs are in the maps. (You would not satisfy this requirement if, for example, your swap function caused a loop to visit each pair in the map, since the number of statements executed by all the iterations of the loop would depend on the number of pairs in the map.)

The character [(open square bracket) must not appear in newMap.h(but is fine in newMap.cpp).

Test your new implementation of the Map class. (Notice that newMap.h,the name of the class defined therein must still be

even though the file is named Map.)

Verify that your WeightMap class still works properly with this new version of Map. You should not need to change your WeightMap class or its implementation in any way, other than to include “newMap.h”instead of “Map.h”.(For this test, be sure to link with newMap.cpp, not Map.cpp.) (Before you turn in WeightMap.h,be sure to restore the include to “Map.h” instead of “newMap.h”.)

Turn it in

By Monday, January 18, there will be a link on the class webpage that will enable you to turn in this homework. Turn in one zip file that contains your solutions to the homework problems. (Since problem 3 builds on problems 1 and 2, you will not turn in separate code for problems 1 and 2.) If you solve every problem, the zip file you turn in will have nine files (three for each of problems 3, 4, and 5). The files must meet these requirements, or your score on this homework will be severely reduced:

Each of the header files Map.h,WeightMap.h,and newMap.hmust have an appropriate include guard. In the files you turn in, the typedefs in Map.hand newMap.hmust define KeyTypeto be a synonym for std::stringand ValueTypeto be a synonym for double.

If we create a project consisting of Map.h,Map.cpp,and testMap.cpp,it must build successfully under both Visual C++ and either clang++ or g++.

If we create a project consisting of Map.h,Map.cpp,WeightMap.h,WeightMap.cpp,and testWeightMap.cpp,it must build successfully under both Visual C++ and either clang++ or g++.

If we create a project consisting of newMap.h,newMap.cpp,and testnewMap.cpp,it must build

2

3/12/ CS32 Homework 1, Winter

successfully under both Visual C++ and either clang++ or g++.

If we create a project consisting of newMap.h,newMap.cpp,and testMap.cpp,where in testMap.cppwe change only the #include “Map.h”to #include “newMap.h”,the project must build successfully under both Visual C++ and either clang++ or g++. (If you try this, be sure to change the #includeback to “Map.h”before you turn in testMap.cpp.)

The source files you submit for this homework must not contain the word friendor vector, and must not contain any global variables whose values may be changed during execution. (Global constants are fine.)

No files other than those whose names begin with testmay contain code that reads anything from cinor writes anything to cout,except that for problem 4, WeightMap::printmust write to cout,and for problem 5, the implementation of the constructor that takes an integer parameter may write a message and exit the program if the integer is negative. Any file may write to cerr(perhaps for debugging purposes); we will ignore any output written to cerr.

You must have an implementation for every member function of Map and WeightMap. If you can’t get a function implemented correctly, its implementation must at least build successfully. For example, if you don’t have time to correctly implement Map::eraseor Map::swap,say, here are implementations that meet this requirement in that they at least allow programs to build successfully even though they might execute incorrectly:

bool Map::erase(const KeyType& value)

{

return true; // not correct, but at least this compiles

}

void Map::swap(Map& other)

{

// does nothing; not correct, but at least this compiles

}

Given Map.hwith the typedef for the Map’s key type specifying std::stringand the typedef for its value type specifying double,if we make no change to your Map.cpp,then if we compile your Map.cppand link it to a file containing

#include “Map.h”

#include <string>

#include <iostream>

#include <cassert>

using namespace std;

void test()

{

Map m;

assert(m.insert(“Fred”, 2.956));

assert(m.insert(“Ethel”, 3.538));

assert(m.size() == 2);

ValueType v = 42;

assert(!m.get(“Lucy”, v) && v == 42);

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 9/12

3/12/ CS32 Homework 1, Winter

assert(m.get(“Fred”, v) && v == 2.956);

v = 42;

KeyType x = “Lucy”;

assert(m.get(0, x, v) &&

((x == “Fred” && v == 2.956) || (x == “Ethel” && v == 3.538))); KeyType x2 = “Ricky”;

assert(m.get(1, x2, v) &&

((x2 == “Fred” && v == 2.956) || (x2 == “Ethel” && v == 3.538))

x != x2);

}

int main()

{

test();

cout << “Passed all tests” << endl;

}

the linking must succeed. When the resulting executable is run, it must write Passed all testsand nothing more to coutand terminate normally.

If we successfully do the above, then in Map.hchange the Map’s typedefs to specify intas the key type and std::stringas the value type without making any other changes, recompile Map.cpp,and link it to a file containing

#include “Map.h”

#include <string>

#include <iostream>

#include <cassert>

using namespace std;

void test()

{

Map m;

assert(m.insert(10, “diez”));

assert(m.insert(20, “veinte”));

assert(m.size() == 2);

ValueType v = “cuarenta y dos”;

assert(!m.get(30, v) && v == “cuarenta y dos”);

assert(m.get(10, v) && v == “diez”);

v = “cuarenta y dos”;

KeyType x = 30;

assert(m.get(0, x, v) &&

((x == 10 && v == “diez”) || (x == 20 && v == “veinte”))); KeyType x2 = 40;

assert(m.get(1, x2, v) &&

((x2 == 10 && v == “diez”) || (x2 == 20 && v == “veinte”)) && x != x2);

}

int main()

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 10/12

3/12/ CS32 Homework 1, Winter

{

test();

cout << “Passed all tests” << endl;

}

the linking must succeed. When the resulting executable is run, it must write Passed all testsand nothing more to coutand terminate normally.

Given newMap.hwith the typedef for the Map’s key type specifying std::stringand the typedef for its value type specifying double,if we make no change to your newMap.cpp,then if we compile your newMap.cppand link it to a file containing

#include “newMap.h”

#include <string>

#include <iostream>

#include <cassert>

using namespace std;

void test()

{

Map m;

assert(m.insert(“Fred”, 2.956));

assert(m.insert(“Ethel”, 3.538));

assert(m.size() == 2);

ValueType v = 42;

assert(!m.get(“Lucy”, v) && v == 42);

assert(m.get(“Fred”, v) && v == 2.956);

v = 42;

KeyType x = “Lucy”;

assert(m.get(0, x, v) &&

((x == “Fred” && v == 2.956) || (x == “Ethel” && v == 3.538))); KeyType x2 = “Ricky”;

assert(m.get(1, x2, v) &&

((x2 == “Fred” && v == 2.956) || (x2 == “Ethel” && v == 3.538))

x != x2);

}

int main()

{

test();

cout << “Passed all tests” << endl;

}

the linking must succeed. When the resulting executable is run, it must write Passed all testsand nothing more to coutand terminate normally.

If we successfully do the above, then in newMap.hchange the Map’s typedefs to specify intas the key type and std::stringas the value type without making any other changes, recompile newMap.cpp,and link it to a file containing

#include “newMap.h”

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 11/12

3/12/ CS32 Homework 1, Winter

#include <string>

#include <iostream>

#include <cassert>

using namespace std;

void test()

{

Map m;

assert(m.insert(10, “diez”));

assert(m.insert(20, “veinte”));

assert(m.size() == 2);

ValueType v = “cuarenta y dos”;

assert(!m.get(30, v) && v == “cuarenta y dos”);

assert(m.get(10, v) && v == “diez”);

v = “cuarenta y dos”;

KeyType x = 30;

assert(m.get(0, x, v) &&

((x == 10 && v == “diez”) || (x == 20 && v == “veinte”))); KeyType x2 = 40;

assert(m.get(1, x2, v) &&

((x2 == 10 && v == “diez”) || (x2 == 20 && v == “veinte”)) && x != x2);

}

int main()

{

test();

cout << “Passed all tests” << endl;

}

the linking must succeed. When the resulting executable is run, it must write Passed all testsand nothing more to coutand terminate normally.

During execution, your program must not perform any undefined actions, such as accessing an array element out of bounds, or dereferencing a null or uninitialized pointer.

Notice that we are not requiring any particular content in testMap.cpp,testWeightMap.cpp,and testnewMap.cpp,as long as they meet the requirements above. Of course, the intention is that you’d use those files for the test code that you’d write to convince yourself that your implementations are correct. Although we will throughly evaluate your implementations for correctness, for homeworks, unlike for projects, we will not grade the thoroughness of your test cases. Incidentally, for homeworks, unlike for projects, we will also not grade your program commenting.

http://web.cs.ucla.edu/classes/winter16/cs32/Homeworks/1/spec.html 12/12