Spring 2024 CS 32

Homework 1 FAQ

  1. For problem 3 (and 5), what's an easy way to flip my test routine between testing a Set of std::string and a Set of unsigned long?

    Here's one technique that lets you flip by commenting out or uncommenting just one line in testSet.cpp:

        #include "Set.h"
        #include <iostream>
        #include <cassert>
        using namespace std;
    
        // To test a Set of unsigned long, leave the #define line commented out;
        // to test a Set of string, remove the "//".
    
        // #define TEST_WITH_STRING
    
        #ifdef TEST_WITH_STRING
    
        const ItemType DUMMY_VALUE = "hello";
        const ItemType V1 = "abc";
    
        #else // assume unsigned long
    
        const ItemType DUMMY_VALUE = 9876543;
        const ItemType V1 = 123456789;
    
        #endif
    
        void test()
        {
            Set s;
            assert(s.empty());
            ItemType v = DUMMY_VALUE;
            assert( !s.get(0, v)  &&  v == DUMMY_VALUE); // v unchanged by get failure
            s.insert(V1);
            assert(s.size() == 1);
            assert(s.get(0, v)  &&  v == V1);
        }
    
        int main()
        {
            test();
            cout << "Passed all tests" << endl;
        }
    

    When your Set.h has ItemType being a type alias for std::string, then by making the symbol TEST_WITH_STRING defined, the #ifdef will select the code that initializes the constants DUMMY_VALUE and V1 in a way that is consistent with the Set. Similarly, if the type alias specifies unsigned long, you would comment out the #define of TEST_WITH_STRING; this would cause the #ifdef to select the #else part, which initializes the constants appropriately for that kind of Set.

  2. I have an egg carton with two rows of six eggs each. In the row nearer to the back of the carton, the eggs are white; in the other, they're brown. I'd like to exchange the rows, changing from

            ======back=======
    	W1 W2 W3 W4 W5 W6
    	B1 B2 B3 B4 B5 B6
    

    to

            ======back=======
    	B1 B2 B3 B4 B5 B6
    	W1 W2 W3 W4 W5 W6
    

    I have no extra egg cartons, and I can't set an egg on the table or it will roll off. Can I do it using just my two hands?

    Yes.