Lab 1

DUE: Tuesday 5/19

  1. Download julianday.cpp and open it up in your favorite text editor or C++ IDE (e.g. Visual Studio).
  2. Compile this code and review the compilation errors. Each error shows the line number on which it appears. Go to the specific line numbers to fix each error. Depending on your IDE, you might be able to double-click on the error to bring you to the exact line number where the error was detected.
  3. Once all compilation errors are fixed, execute your code. Test it thoroughly. Show it to your instructor once you have worked all the bugs out.
  4. Add validation code to exit your program if an invalid month, day, or year is entered. Consider using a function to help with this. Test this new functionality thoroughly.
  5. Write a display_date() function that displays a date in the following format by using a switch statement for the month:
  6. January 1, 2002
    May 18, 2009
    December 9, 2008
    
  7. In main(), after the given code, define two arrays that each hold ten integer values. One of these arrays should store months, the other should store days. We'll assume that the year is already set by the user in the given code.
  8. Write a for loop that reads ten month/day combinations from the user into the two arrays, validating each input.
  9. Create a third array that holds Julian days. Write another for loop that computes the Julian day from the month/day combinations and stores it in this new array.
  10. Finally, write a for loop that outputs the Julian days.
  11. Test your program thoroughly. Show it to your instructor once you have worked all the bugs out.

Lab 2

DUE: Thursday 5/21

  1. In this lab, you'll write a program to work with arrays and vectors. Start by downloading the grades.txt file, which is just a file containing a series of integers.
  2. In your program, read in the values from grades.txt and store them in both an array and a vector.
  3. Write a function to count the number of passing grades (greater than or equal to 60) in the array. Write the same function for the vector. Call these functions (and the functions that follow) from main() to test your code.
  4. Write a function to determine whether a specific grade value is in the array. Obtain this value from the user. For example, if the user enters 99, the program should display a message that the value 99 is in the list of grades. If the user enters 23, the program should display a message stating that the value 23 is (luckily) not in the list.
  5. Write the same "find-a-grade" function for the vector.
  6. Write a recursive function to display all elements of the array. In other words, the function should print an element, then repeatedly call itself with a new array with one less element. As a hint, feel free to use an additional helper function.
  7. Next, write a recursive function to display all elements of the vector. Again as a hint, feel free to use an additional helper function.
  8. Modify your recursive display functions to take an additional argument named minimum. As you traverse the array or vector, display only those grades that are greater than or equal to the given minimum value.

Lab 3

DUE: Wednesday 5/27

  1. Implement a Time class that represents all possible times in a 24-hour period, including hours, minutes, and seconds. An immediate representation issue is how to handle morning (AM) and afternoon (PM) times. We could use a separate bool member variable that indicates whether the time is AM or PM. It's probably easier to represent the hours in military time, meaning that the hours of the day are numbered from 0 to 23, with 13 being 1PM, 14 being 2PM, etc. Minutes and seconds should be numbered from 0 to 59.
  2. Create both a Time.h header file and a Time.cpp implementation file. All member variables must be defined as private.
  3. Initially, two constructors are required: one that takes no arguments, another that takes three integer arguments.
    1. The no-argument default constructor must set the time to exactly 1:00.00 AM.
    2. The three-argument constructor accepts values for hours, minutes, and seconds.
  4. Declare and implement the appropriate get and set functions for your private member variables.
  5. Declare and implement a print() function that outputs the time using a format of HH24:MI.SS, as in 8:15.39 or 22:05.06.
  6. Declare and implement a printAmPm() function that outputs the time using a default format of HH:MI.SS AMPM, as in 8:15.39 AM or 10:05.06 PM.
  7. Create a third source file named time_main.cpp that has your main() function. In main(), create two Time objects, showing the use of each of your constructors. Next, use your get, set, print(), and printAmPm() functions to be sure they work correctly.
  8. Extra Credit: Create a copy constructor and use it in main().
  9. Extra Credit: Create a function called secondsBetween that calculates the number of seconds between two Time objects. Handle both of the following cases:
    1. The first Time argument occurs before the second Time argument.
    2. The first Time argument occurs after the second Time argument.

Lab 4

DUE: Wednesday 6/3

  1. Download the Vec.h class declaration for our own templated vector class. This is your starting point for this lab.
  2. Complete the implementation and testing of the push_back() and erase() member functions of Vec<T>. Use the main() function of vec_main.cpp to test your code.
  3. Write the resize() member function. Do not use the push_back() member function in your implementation of resize(). Add code to main() to test the resize() function (e.g. apply to z a few times).
  4. Write a templated non-member operator==() function to compare two Vec<T> objects. Add this to Vec.h. Add code to main() to test this overloaded operator. The function prototype should be:
  5. template <class T>
    bool operator== ( const Vec<T>& left, const Vec<T>& right );
    
  6. Write and test a function called reverse() that reverses the contents of the vector. You may not use a second vector; instead, implement the reverse function in place. The trick is to step through the vector one location at a time, swapping values between the first half of the vector and the second half. In other words, the value at location 0 and the value at location size()-1 must be swapped, then the value at location 1 and the value at location size()-2 must be swapped, etc.
  7. Add code to main() to test the reverse() function. Be sure to test the interesting cases, including an empty vector, and vectors of one or two values.
  8. Overload the ++ and -- operators to perform rotate-right and rotate-left operations, respectively. Be sure to implement this as part of the class (i.e. as overloaded member functions).

Lab 5

DUE: Tuesday 6/9

In this lab, we'll focus on the list class of the standard library, as well as iterators. Remember that iterators are essentially pointers.

In this lab, iterators must be used; the [] operator is not allowed.

  1. Overload the << operator for list<T>. The overloaded operator<< function should display all elements of the list using an iterator. To help get you started, here's some code (note the use of const_iterator and typename):
  2. template <class T>
    ostream& operator<< ( ostream& os, const list<T>& l )
    {
      typename list<T>::const_iterator i = l.begin();
    
      // ...
    
    
    }
    
  3. Write a function called vector_to_list() that creates a list<double>, then copies elements from the given const vector<double> into the list. All elements that are negative are changed to positive values (e.g. -90.5 becomes 90.5). The function must return the number of elements copied (i.e. an int).
  4. Test the above function by using a vector<double> that holds double values {-99.5,-98.5,-97.5,...,97.5,98.5,99.5}.
  5. Convert the following code below to use a list instead of a vector. This requires the use of iterators. Further, make small_sort() a templated function. Test this function by sorting your list from step 3 above.
  6. void small_sort( vector<double>& v )
    {
      int n = v.size();
      for( int i = 0 ; i < n - 1 ; i++ )
      {
        // Find index of next smallest value
        int small_index = i;
        for ( int j = i + 1 ; j < n ; j++ )
        {
          if ( v[j] < v[small_index] ) {
            small_index = j;
          }
        }
    
        // Swap next smallest into place
        double temp = v[i];
        v[i] = v[small_index];
        v[small_index] = temp;
      }
    }
    

Lab 6

DUE: Thursday 6/11

In this lab, we'll focus on the map class of the standard library, as well as iterators. As with the previous lab, remember that iterators are essentially pointers.

In the steps that follow, you'll extend the word-count program we did in class to keep track of where each word appears in the given input stream.

  1. Start by creating a lab6_main.cpp file. Use the following code to start your main() function such that you can capture the command-line arguments:
  2. int main( int argc, char* argv[] )
    {
    
  3. The first command-line argument is the filename to read. Similar to what we did in class using cin, you'll read word by word from this file.
  4. Read each word and convert to lowercase. Further, strip out any punctuation (i.e. any characters that are not lowercase letters a-z). Consider using the tolower() and isalpha() functions from the <cctype> library.
  5. Add (or update) each word to a map declared as follows (where the vector<int> value is a sequence of integers that represent the nth word of where each occurrence is in the input file):
  6. map< string, vector<int> > words;
    
  7. Overload the << operator for map<T1,T2>. The overloaded operator<< function should display all elements of the map using an iterator. As with the previous lab, use const_iterator and typename. Further, you'll also need to overload the << operator for vector<T>.
  8. To test your code, use the warandpeace-ch1.txt file. Display your map to be sure it is correct.
  9. Interact with the user by asking the user to enter a word. Look the word up in your map and display the number of word occurrences, as well as the index of each word occurrence. If the word is not in the map, display an error message. Here's some sample output (with user input shown in bold):
  10. Enter the search word (single word only): Achilles
    The word "Achilles" appears 6 times as follows.
     It's word #18
     It's word #218
     It's word #303
     It's word #384
     It's word #489
     It's word #555
    
    Enter the search word (single word only): computer
    The word "computer" does not appear in the input file.
    
    Enter the search word (single word only): 
    ...
    

Lab 7

DUE: Wednesday 6/17

  1. Download the cs2set.h class declaration for our own set class (implemented as a binary tree structure). Read through this code to understand it.
  2. Download the cs2set_main.cpp code to test the classes defined in cs2set.h. Also read through this code to understand it.
  3. The implementation of the copy constructor and the assignment operator is not yet complete because each depends on a private member function called copy_tree(), the body of which has not yet been written. Write the copy_tree() function, then test to see if it works by "uncommenting" the appropriate code from the main() function (look for and remove the #if 0 and corresponding #endif).
  4. The implementation of the find() function is recursive. Comment it out and implement (and test!) a non-recursive replacement for this function.

Lab 8

DUE: Monday 6/22

  1. Download the hash_set.h class declaration for our own set class (implemented using a hash table structure). Read through this code to understand it.
  2. Download the hash_set_main.cpp code to test the classes defined in hash_set.h. Also read through this code to understand it.
  3. Implement the insert() function, ignoring, for now, the call to the resize_table() function. This function should implement a linked-list insertion (at the front of the list is fine) once the correct list has been found (using the hash function) and it has been determined that the key value is not already in the list. The function should store the hash value (not the index!) computed by the hash function in the HashNode object. The function should increment the m_size counter and return the appropriate iterator/bool pair.
  4. Implement and test the begin() function of the hash_set class. When the hash_set is empty, begin() should return the end() iterator. Code in main() should be uncommented to test this function.
  5. Implement and test the next() function of the iterator class. When next() runs out of values in the hash table, it should return the equivalent of the end() iterator. Code in main() should be uncommented to test this function.
  6. Implement and test the erase() function of the hash_set class. Code in main() should be uncommented to test this function.
  7. Implement and test the resize_table() function of the hash_set class. The resize_table() function should reuse the existing HashNode and must include no delete or new operations. Code in main() should be uncommented to test this function.