diff --git a/docs/compilation.html b/docs/compilation.html index dcdd4fc2f..261922cd1 100644 --- a/docs/compilation.html +++ b/docs/compilation.html @@ -22,7 +22,7 @@

Help! My code doesn't compile!

  • Check the section, below, on common compiler errors and how to solve them, as the error that you are looking for may be listed there
  • Did you submit all the required files? In particular, if some of the files that you #include are not present, then it won't compile.
  • Filename case: Linux (what the compilation is done on) does care about the capitalization of file names. So if your code has an #include "FileName.h" and your file is named filename.h, this will NOT compile under Linux. Change your file name cases, or your #include line.
  • -
  • Did you include the appropriate system #include lines? If it is complaining that it can't find exit(), for example, be sure to #include <stdlib.h>. Many compilers automatically include <stdlib.h> and <stdio.h>, which means you can directly call functions such as printf() and exit() without the appropriate #include line. The compiler used by the submission system does not include, by default, any #include files.
  • +
  • Did you include the appropriate system #include lines? If it is complaining that it can't find find(), for example, be sure to #include <algorithm>.
  • Did you enter using namespace std; at the top of each file?
  • The submission system provides the compilation command - try running it on your machine. In particular, try creating a NEW directory, copying into that directory the files that you are submitting (and ONLY those files), and then executing the compile command.
  • If the compile command is make (you learn about make and Makefiles about 1/3 of the way through the course), then your Makefile needs to be configured to properly compile the code
  • diff --git a/docs/compilation.md b/docs/compilation.md index f1ca142d4..ee3b6dedb 100644 --- a/docs/compilation.md +++ b/docs/compilation.md @@ -15,7 +15,7 @@ There are a number of things you can try: - Check the section, below, on common compiler errors and how to solve them, as the error that you are looking for may be listed there - Did you submit all the required files? In particular, if some of the files that you `#include` are not present, then it won't compile. - Filename case: Linux (what the compilation is done on) does care about the capitalization of file names. So if your code has an `#include "FileName.h"` and your file is named filename.h, this will NOT compile under Linux. Change your file name cases, or your `#include` line. -- Did you include the appropriate system `#include` lines? If it is complaining that it can't find `exit()`, for example, be sure to `#include `. Many compilers automatically include `` and ``, which means you can directly call functions such as `printf()` and `exit()` without the appropriate `#include` line. The compiler used by the submission system does not include, by default, any `#include` files. +- Did you include the appropriate system `#include` lines? If it is complaining that it can't find `find()`, for example, be sure to `#include `. - Did you enter `using namespace std;` at the top of each file? - The submission system provides the compilation command - try running it on your machine. In particular, try creating a NEW directory, copying into that directory the files that you are submitting (and ONLY those files), and then executing the compile command. - If the compile command is `make` (you learn about make and Makefiles about 1/3 of the way through the course), then your Makefile needs to be configured to properly compile the code diff --git a/labs/lab01/list.cpp b/labs/lab01/list.cpp index bb83c3ef7..f4b00ba41 100644 --- a/labs/lab01/list.cpp +++ b/labs/lab01/list.cpp @@ -9,7 +9,6 @@ // 01-13-14: Modified to fit modern C++ compilers; reformatted #include -#include #include "list.h" using namespace std; @@ -24,14 +23,14 @@ int main() { cout << "size(): returned " << l->size() << endl; n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -44,14 +43,14 @@ int main() { cout << "empty() called, returned " << l->empty() << endl; n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -59,14 +58,14 @@ int main() { l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -81,14 +80,14 @@ int main() { l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -96,14 +95,14 @@ int main() { l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -112,54 +111,54 @@ int main() { cout << endl; n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; cout << "size(): returned " << l->size() << endl; n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; @@ -182,16 +181,16 @@ int main() { l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop_head(); - if ( n == NULL ) - cout << "pop_head(): list is empty, pop_head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop_head(): list is empty, pop_head(): returned nullptr." << endl; else cout << "pop_head(): returned " << *n << endl; diff --git a/labs/lab01/list.cpp.html b/labs/lab01/list.cpp.html index fa520a1f0..7bf7371de 100644 --- a/labs/lab01/list.cpp.html +++ b/labs/lab01/list.cpp.html @@ -20,7 +20,6 @@ // 01-13-14: Modified to fit modern C++ compilers; reformatted #include <iostream> -#include <stdio.h> #include "list.h" using namespace std; @@ -35,14 +34,14 @@ cout << "size(): returned " << l->size() << endl; n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -55,14 +54,14 @@ cout << "empty() called, returned " << l->empty() << endl; n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -70,14 +69,14 @@ l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -92,14 +91,14 @@ l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -107,14 +106,14 @@ l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; @@ -123,54 +122,54 @@ cout << endl; n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->head(); - if ( n == NULL ) - cout << "head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "head(): returned nullptr." << endl; else cout << "head(): returned " << *n << endl; n = l->tail(); - if ( n == NULL ) - cout << "tail(): returned NULL." << endl; + if ( n == nullptr ) + cout << "tail(): returned nullptr." << endl; else cout << "tail(): returned " << *n << endl; cout << "size(): returned " << l->size() << endl; n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; @@ -193,16 +192,16 @@ l->display(); n = l->pop(); - if ( n == NULL ) - cout << "pop(): list is empty, pop(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop(): list is empty, pop(): returned nullptr." << endl; else cout << "pop(): returned " << *n << endl; l->display(); n = l->pop_head(); - if ( n == NULL ) - cout << "pop_head(): list is empty, pop_head(): returned NULL." << endl; + if ( n == nullptr ) + cout << "pop_head(): list is empty, pop_head(): returned nullptr." << endl; else cout << "pop_head(): returned " << *n << endl; diff --git a/labs/lab01/list.h b/labs/lab01/list.h index 647bba4db..2f66551c2 100644 --- a/labs/lab01/list.h +++ b/labs/lab01/list.h @@ -32,7 +32,7 @@ * int element (T item); Returns 1 if T is in the list, 0 otherwise * pop_head (T item); Removes an item from the head of the list * int remove (T item) Removes item, returns 1 if sucessful, else 0 - * T* getptr (T item) Returns the pointer to the parameter or NULL + * T* getptr (T item) Returns the pointer to the parameter or nullptr * void save(FILE *fp) Saves the list- calls the objects methods * * @@ -90,7 +90,7 @@ class List { public: List() { - _head = _tail = NULL; + _head = _tail = nullptr; } // cout << "List Constructor called.\n"; } ~List() { @@ -99,9 +99,9 @@ class List { void push(T item) { // cout << "push(): called with element: " << item << endl; - if ( _head == NULL ) { + if ( _head == nullptr ) { _head = _tail = new List_Link; - _head->next = NULL; + _head->next = nullptr; } else { List_Link *temp; temp = _tail; @@ -113,14 +113,14 @@ class List { } T* pop() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else { List_Link *temp; T *ret; temp = _tail; if ( _head == _tail ) - _tail = _head = NULL; + _tail = _head = nullptr; else _tail = _tail->next; ret = temp->data; @@ -130,61 +130,61 @@ class List { } int size() { - if ( _tail == NULL ) + if ( _tail == nullptr ) return 0; else { int s = 0; List_Link *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) ++s; return s; } } void save(FILE *fp) { - if ( _head == NULL ) {} + if ( _head == nullptr ) {} else { List_Link *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) temp->data->save(fp); } } void display() { - if ( _head == NULL ) + if ( _head == nullptr ) cout << "List is empty.\n"; else { // cout << "display(): List is printed reverse: List is not empty.\n\t"; List_Link *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) cout << *temp->data << " "; cout << endl; } } int empty() { - return ( _head == NULL ); + return ( _head == nullptr ); } T* tail() { - if ( _tail == NULL ) - return NULL; + if ( _tail == nullptr ) + return nullptr; else return _tail->data; } T* head() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else return _head->data; } int element (T item) { List_Link *temp; - if ( _tail == NULL ) + if ( _tail == nullptr ) return 0; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) if ( *temp->data == item ) return 1; return 0; @@ -192,36 +192,36 @@ class List { T* getptr (T item) { List_Link *temp; - if ( _tail == NULL ) - return NULL; - for ( temp = _tail; temp != NULL; temp = temp->next ) + if ( _tail == nullptr ) + return nullptr; + for ( temp = _tail; temp != nullptr; temp = temp->next ) if ( *temp->data == item ) return temp->data; - return NULL; + return nullptr; } void clear() { - while ( this->pop() != NULL ) { } + while ( this->pop() != nullptr ) { } } void push_head (T item) { - if ( _head == NULL ) { + if ( _head == nullptr ) { _head = _tail = new List_Link; - _head->next = NULL; + _head->next = nullptr; } else { List_Link *temp; temp = _head; _head = new List_Link; temp->next = _head; - _head->next = NULL; + _head->next = nullptr; } _head->data = new T; *_head->data = item; } T* pop_head() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else { List_Link *temp; T *ret; @@ -230,7 +230,7 @@ class List { _head = temp; temp = temp->next; delete temp; - _head->next = NULL; + _head->next = nullptr; return ret; } } @@ -240,7 +240,7 @@ class List { cout << "remove(): element '" << item << "' exists.\n"; if ( _head == _tail ) { delete _head; - _head = _tail = NULL; + _head = _tail = nullptr; } else if ( *_tail->data == item ) { List_Link *temp; temp = _tail; diff --git a/labs/lab01/list.h.html b/labs/lab01/list.h.html index 69b8ddc40..246549fde 100644 --- a/labs/lab01/list.h.html +++ b/labs/lab01/list.h.html @@ -43,7 +43,7 @@ * int element (T item); Returns 1 if T is in the list, 0 otherwise * pop_head (T item); Removes an item from the head of the list * int remove (T item) Removes item, returns 1 if sucessful, else 0 - * T* getptr (T item) Returns the pointer to the parameter or NULL + * T* getptr (T item) Returns the pointer to the parameter or nullptr * void save(FILE *fp) Saves the list- calls the objects methods * * @@ -101,7 +101,7 @@ public: List() { - _head = _tail = NULL; + _head = _tail = nullptr; } // cout << "List Constructor called.\n"; } ~List() { @@ -110,9 +110,9 @@ void push(T item) { // cout << "push(): called with element: " << item << endl; - if ( _head == NULL ) { + if ( _head == nullptr ) { _head = _tail = new List_Link<T>; - _head->next = NULL; + _head->next = nullptr; } else { List_Link<T> *temp; temp = _tail; @@ -124,14 +124,14 @@ } T* pop() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else { List_Link<T> *temp; T *ret; temp = _tail; if ( _head == _tail ) - _tail = _head = NULL; + _tail = _head = nullptr; else _tail = _tail->next; ret = temp->data; @@ -141,61 +141,61 @@ } int size() { - if ( _tail == NULL ) + if ( _tail == nullptr ) return 0; else { int s = 0; List_Link<T> *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) ++s; return s; } } void save(FILE *fp) { - if ( _head == NULL ) {} + if ( _head == nullptr ) {} else { List_Link<T> *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) temp->data->save(fp); } } void display() { - if ( _head == NULL ) + if ( _head == nullptr ) cout << "List is empty.\n"; else { // cout << "display(): List is printed reverse: List is not empty.\n\t"; List_Link<T> *temp; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) cout << *temp->data << " "; cout << endl; } } int empty() { - return ( _head == NULL ); + return ( _head == nullptr ); } T* tail() { - if ( _tail == NULL ) - return NULL; + if ( _tail == nullptr ) + return nullptr; else return _tail->data; } T* head() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else return _head->data; } int element (T item) { List_Link<T> *temp; - if ( _tail == NULL ) + if ( _tail == nullptr ) return 0; - for ( temp = _tail; temp != NULL; temp = temp->next ) + for ( temp = _tail; temp != nullptr; temp = temp->next ) if ( *temp->data == item ) return 1; return 0; @@ -203,36 +203,36 @@ T* getptr (T item) { List_Link<T> *temp; - if ( _tail == NULL ) - return NULL; - for ( temp = _tail; temp != NULL; temp = temp->next ) + if ( _tail == nullptr ) + return nullptr; + for ( temp = _tail; temp != nullptr; temp = temp->next ) if ( *temp->data == item ) return temp->data; - return NULL; + return nullptr; } void clear() { - while ( this->pop() != NULL ) { } + while ( this->pop() != nullptr ) { } } void push_head (T item) { - if ( _head == NULL ) { + if ( _head == nullptr ) { _head = _tail = new List_Link<T>; - _head->next = NULL; + _head->next = nullptr; } else { List_Link<T> *temp; temp = _head; _head = new List_Link<T>; temp->next = _head; - _head->next = NULL; + _head->next = nullptr; } _head->data = new T; *_head->data = item; } T* pop_head() { - if ( _head == NULL ) - return NULL; + if ( _head == nullptr ) + return nullptr; else { List_Link<T> *temp; T *ret; @@ -241,7 +241,7 @@ _head = temp; temp = temp->next; delete temp; - _head->next = NULL; + _head->next = nullptr; return ret; } } @@ -251,7 +251,7 @@ cout << "remove(): element '" << item << "' exists.\n"; if ( _head == _tail ) { delete _head; - _head = _tail = NULL; + _head = _tail = nullptr; } else if ( *_tail->data == item ) { List_Link<T> *temp; temp = _tail; diff --git a/labs/lab02/ListNode.h b/labs/lab02/ListNode.h index 52ab43702..dc39d5008 100644 --- a/labs/lab02/ListNode.h +++ b/labs/lab02/ListNode.h @@ -8,9 +8,6 @@ #include -// next line needed because NULL is part of std namespace -using namespace std; - class ListNode { public: ListNode(); // Constructor diff --git a/labs/lab02/ListNode.h.html b/labs/lab02/ListNode.h.html index c6687942d..b414f132d 100644 --- a/labs/lab02/ListNode.h.html +++ b/labs/lab02/ListNode.h.html @@ -19,9 +19,6 @@ #include <iostream> -// next line needed because NULL is part of std namespace -using namespace std; - class ListNode { public: ListNode(); // Constructor diff --git a/labs/lab02/ListTest.cpp b/labs/lab02/ListTest.cpp index fcf6b3cbc..150e60fd2 100644 --- a/labs/lab02/ListTest.cpp +++ b/labs/lab02/ListTest.cpp @@ -14,6 +14,7 @@ // if the 'forward' parameter is false, // the list is printed in reverse order +#include #include #include #include @@ -22,10 +23,11 @@ using namespace std; #include "List.h" #include "ListItr.h" -int menu (string option[], int n_opt); +template +int menu(array options); //set up menu options -string option[] = { +array options{ "Quit", "New List", "Show List elements", @@ -45,8 +47,6 @@ string option[] = { "Make list empty", }; -int const n_choice = 17; - /* * This main driver program interactively exercises a * list package. @@ -59,8 +59,8 @@ int const n_choice = 17; int main () { int command; string response; - List *list = NULL; - ListItr *itr = NULL; + List *list = nullptr; + ListItr *itr = nullptr; // Initialize this run cout << "--------------------------------------------------" << endl; @@ -71,7 +71,7 @@ int main () { cout << "\tobjects." << endl; while (true) { - command = menu(option, n_choice); + command = menu(options); switch (command) { // Quit @@ -81,11 +81,11 @@ int main () { // Exit normally if (response[0] == 'y' || response[0] == 'Y') { - if (list != NULL) { + if (list != nullptr) { delete list; } - if (itr != NULL) { + if (itr != nullptr) { delete itr; } return 0; @@ -95,7 +95,7 @@ int main () { // New list case 2: - if (list != NULL) { + if (list != nullptr) { delete list; } list = new List(); @@ -116,7 +116,7 @@ int main () { cin >> response; while (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAtTail(element); cout << "\tEnter next element: "; @@ -130,7 +130,7 @@ int main () { // Show elements case 3: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -152,14 +152,14 @@ int main () { // first() case 4: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; } cout << "\tSetting the ListItr to the first element..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->first()); @@ -167,7 +167,7 @@ int main () { // find() case 5: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -177,10 +177,10 @@ int main () { cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); cout << "\tSetting the ListItr to find(" << element << ")..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->find(element)); @@ -191,14 +191,14 @@ int main () { // last() case 6: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; } cout << "\tSetting the ListItr to the last element..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->last()); @@ -206,7 +206,7 @@ int main () { // moveForwards() case 7: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -218,7 +218,7 @@ int main () { // moveBackwards() case 8: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -230,7 +230,7 @@ int main () { // retrieve() case 9: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -247,7 +247,7 @@ int main () { // insertBefore() case 10: - if (list == NULL || itr == NULL) { + if (list == nullptr || itr == nullptr) { cout << endl; cout << "\tCreate a List and ListItr first." << endl; break; @@ -263,7 +263,7 @@ int main () { cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertBefore(element, *itr); cout << "\tInserting " << element << " before the current ListItr" << endl; } else { @@ -278,7 +278,7 @@ int main () { // insertAfter() case 11: - if (list == NULL || itr == NULL) { + if (list == nullptr || itr == nullptr) { cout << endl; cout << "\tCreate a List and ListItr first." << endl; break; @@ -294,7 +294,7 @@ int main () { cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAfter(element, *itr); cout << "\tInserting " << element << " after the current ListItr" <> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAtTail(element); cout << "\tInserting " << element << " at the tail of the list" << endl; } else { @@ -334,7 +334,7 @@ int main () { // remove() case 13: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -344,7 +344,7 @@ int main () { cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->remove(element); cout << "\tRemoving " << element << " from list" <makeEmpty(); - if (itr != NULL) { + if (itr != nullptr) { delete itr; - itr = NULL; + itr = nullptr; } cout << "The list was made empty (forward): "; @@ -478,15 +478,16 @@ int main () { * * NOTE, all input and output uses 'stdin' and 'stdout' */ -int menu (string option[], int n_opt) { - int choice, i; +template +int menu(array options) { + int choice = -1; string input; cout << " - - - - - - MENU - - - - - -" << endl; cout << endl; - for (i = 0; i < n_opt; i++) { - cout << "\t" << i + 1 << " - " << option[i] << endl; + for (size_t i = 0; i < SIZE; i++) { + cout << "\t" << i + 1 << " - " << options[i] << endl; } cout << endl; @@ -497,13 +498,13 @@ int menu (string option[], int n_opt) { cin >> input; if (isdigit(input[0])) { - choice = atoi(input.c_str()); + choice = stoi(input); - if (choice <= n_opt && choice > 0) { + if (choice > 0 && choice <= SIZE) { return choice; } else { /* choice out of range */ - cout << "\tYour response MUST be between 1 and " << n_opt << endl; + cout << "\tYour response MUST be between 1 and " << SIZE << endl; input.clear(); } } else { diff --git a/labs/lab02/ListTest.cpp.html b/labs/lab02/ListTest.cpp.html index 9c87d70a1..980eb88bb 100644 --- a/labs/lab02/ListTest.cpp.html +++ b/labs/lab02/ListTest.cpp.html @@ -25,6 +25,7 @@ // if the 'forward' parameter is false, // the list is printed in reverse order +#include <array> #include <iostream> #include <string> #include <cctype> @@ -33,10 +34,11 @@ #include "List.h" #include "ListItr.h" -int menu (string option[], int n_opt); +template<size_t SIZE> +int menu(array<string, SIZE> options); //set up menu options -string option[] = { +array<string, 17> options{ "Quit", "New List", "Show List elements", @@ -56,8 +58,6 @@ "Make list empty", }; -int const n_choice = 17; - /* * This main driver program interactively exercises a * list package. @@ -70,8 +70,8 @@ int main () { int command; string response; - List *list = NULL; - ListItr *itr = NULL; + List *list = nullptr; + ListItr *itr = nullptr; // Initialize this run cout << "--------------------------------------------------" << endl; @@ -82,7 +82,7 @@ cout << "\tobjects." << endl; while (true) { - command = menu(option, n_choice); + command = menu(options); switch (command) { // Quit @@ -92,11 +92,11 @@ // Exit normally if (response[0] == 'y' || response[0] == 'Y') { - if (list != NULL) { + if (list != nullptr) { delete list; } - if (itr != NULL) { + if (itr != nullptr) { delete itr; } return 0; @@ -106,7 +106,7 @@ // New list case 2: - if (list != NULL) { + if (list != nullptr) { delete list; } list = new List(); @@ -127,7 +127,7 @@ cin >> response; while (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAtTail(element); cout << "\tEnter next element: "; @@ -141,7 +141,7 @@ // Show elements case 3: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -163,14 +163,14 @@ // first() case 4: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; } cout << "\tSetting the ListItr to the first element..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->first()); @@ -178,7 +178,7 @@ // find() case 5: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -188,10 +188,10 @@ cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); cout << "\tSetting the ListItr to find(" << element << ")..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->find(element)); @@ -202,14 +202,14 @@ // last() case 6: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; } cout << "\tSetting the ListItr to the last element..." << endl; - if (itr != NULL) { + if (itr != nullptr) { delete itr; } itr = new ListItr(list->last()); @@ -217,7 +217,7 @@ // moveForwards() case 7: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -229,7 +229,7 @@ // moveBackwards() case 8: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -241,7 +241,7 @@ // retrieve() case 9: - if (itr == NULL) { + if (itr == nullptr) { cout << endl; cout << "\tCreate a ListItr first." << endl; break; @@ -258,7 +258,7 @@ // insertBefore() case 10: - if (list == NULL || itr == NULL) { + if (list == nullptr || itr == nullptr) { cout << endl; cout << "\tCreate a List and ListItr first." << endl; break; @@ -274,7 +274,7 @@ cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertBefore(element, *itr); cout << "\tInserting " << element << " before the current ListItr" << endl; } else { @@ -289,7 +289,7 @@ // insertAfter() case 11: - if (list == NULL || itr == NULL) { + if (list == nullptr || itr == nullptr) { cout << endl; cout << "\tCreate a List and ListItr first." << endl; break; @@ -305,7 +305,7 @@ cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAfter(element, *itr); cout << "\tInserting " << element << " after the current ListItr" <<endl; } else { @@ -320,7 +320,7 @@ // insertAtTail() case 12: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -330,7 +330,7 @@ cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->insertAtTail(element); cout << "\tInserting " << element << " at the tail of the list" << endl; } else { @@ -345,7 +345,7 @@ // remove() case 13: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -355,7 +355,7 @@ cin >> response; if (isdigit(response[0])) { - int element = atoi(response.c_str()); + int element = stoi(response); list->remove(element); cout << "\tRemoving " << element << " from list" <<endl; } else { @@ -370,7 +370,7 @@ // size() case 14: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -381,7 +381,7 @@ // Copy constructor case 15: { - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -407,16 +407,16 @@ cout << "The old list should be destroyed now." << endl; delete old_list; - if (itr != NULL) { + if (itr != nullptr) { delete itr; - itr = NULL; + itr = nullptr; } break; } // operator= case 16: { - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -443,16 +443,16 @@ cout << "The old list should be destroyed now." << endl; delete old_list; - if (itr != NULL) { + if (itr != nullptr) { delete itr; - itr = NULL; + itr = nullptr; } break; } // makeEmpty() case 17: - if (list == NULL) { + if (list == nullptr) { cout << endl; cout << "\tCreate a List first." << endl; break; @@ -465,9 +465,9 @@ printList(*list, false); list->makeEmpty(); - if (itr != NULL) { + if (itr != nullptr) { delete itr; - itr = NULL; + itr = nullptr; } cout << "The list was made empty (forward): "; @@ -489,15 +489,16 @@ * * NOTE, all input and output uses 'stdin' and 'stdout' */ -int menu (string option[], int n_opt) { - int choice, i; +template<size_t SIZE> +int menu(array<string, SIZE> options) { + int choice = -1; string input; cout << " - - - - - - MENU - - - - - -" << endl; cout << endl; - for (i = 0; i < n_opt; i++) { - cout << "\t" << i + 1 << " - " << option[i] << endl; + for (size_t i = 0; i < SIZE; i++) { + cout << "\t" << i + 1 << " - " << options[i] << endl; } cout << endl; @@ -508,13 +509,13 @@ cin >> input; if (isdigit(input[0])) { - choice = atoi(input.c_str()); + choice = stoi(input); - if (choice <= n_opt && choice > 0) { + if (choice > 0 && choice <= SIZE) { return choice; } else { /* choice out of range */ - cout << "\tYour response MUST be between 1 and " << n_opt << endl; + cout << "\tYour response MUST be between 1 and " << SIZE << endl; input.clear(); } } else { diff --git a/labs/lab02/index.html b/labs/lab02/index.html index d92dfc534..6ce904adf 100644 --- a/labs/lab02/index.html +++ b/labs/lab02/index.html @@ -66,7 +66,7 @@

    UML Diagram

    UML diagram
    UML diagram
    -

    This diagram shows a list containing two elements, the integers 3 and 7. Note that there are more methods in the List and ListItr classes than what is shown above. The head and tail pointers in the List class point to dummy nodes -- they are put there to make inserting elements into the list easier. It doesn't matter what the value of the dummy nodes is set to, as it won't be used. Each ListNode points to the nodes before and after it (although the dummy nodes each have one pointer pointing to NULL).

    +

    This diagram shows a list containing two elements, the integers 3 and 7. Note that there are more methods in the List and ListItr classes than what is shown above. The head and tail pointers in the List class point to dummy nodes -- they are put there to make inserting elements into the list easier. It doesn't matter what the value of the dummy nodes is set to, as it won't be used. Each ListNode points to the nodes before and after it (although the dummy nodes each have one pointer pointing to nullptr).

    Thus, our doubly linked list will have only one List object and many ListNode objects (2 more than the number of elements in the list). A ListItr is a separate object, which points to one element in the list (possibly a dummy node). As you call the various methods in ListItr to move the iterator forward and backward, the node that it points to will change.

    ListNode

    A ListNode contains an integer value, as well as next and previous pointers to other ListNodes. View the ListNode.h (src) code for details.

    @@ -90,7 +90,7 @@

    Getting started

  • printList
  • Segmentation faults

    -

    When beginning to test your code, chances are your program will crash unexpectedly with a message similar to Segmentation fault (core dumped), commonly referred to as a segfault. Get prepared to see these often throughout the course, as unlike other programming languages like Java, C++ does not give any extra debugging information on a crash. In order to determine where and why your program is crashing, run your code through the debugger and look at the backtrace. Segfaults generally indicate that you are trying to dereference a NULL or invalid pointer, so those are good things to look out for.

    +

    When beginning to test your code, chances are your program will crash unexpectedly with a message similar to Segmentation fault (core dumped), commonly referred to as a segfault. Get prepared to see these often throughout the course, as unlike other programming languages like Java, C++ does not give any extra debugging information on a crash. In order to determine where and why your program is crashing, run your code through the debugger and look at the backtrace. Segfaults generally indicate that you are trying to dereference a nullptr or invalid pointer, so those are good things to look out for.

    The constructor

    By the time the constructor finishes, we should have a functioning (but empty) list. This means that head and tail need to be set appropriately! What should they point to if the list is empty?

    Make sure you are initializing the variables that are specified in the .h file and not declaring new variables that have the same name as those in the header file. Take a look back at lifecycle.cpp's constructors from Lab 1 if you need a refresher.

    @@ -145,7 +145,7 @@

    Find and remove

    makeEmpty and the destructor

    makeEmpty should clear the list of all elements (isEmpty should return true after calling makeEmpty). It should also make sure that head and tail no longer point to those deleted elements (what should they point to instead in an empty list?).
    Since we have been dynamically allocating ListNodes, we must also be responsible for deleting them to ensure we do not leak memory. There are multiple ways to iterate through the list and delete each ListNode -- experiment and see what makes the most sense to you.
    -Important: once you delete a ListNode, you can no longer reliably access any of its data, such as its next or previous pointers! To make sure you don't do this accidentally, we recommend setting each ListNode to NULL as soon as you delete it.

    +Important: once you delete a ListNode, you can no longer reliably access any of its data, such as its next or previous pointers! To make sure you don't do this accidentally, we recommend setting each ListNode to nullptr as soon as you delete it.

    The destructor should delete all dynamically-allocated memory, as we no longer need this List instance. Thus, it makes sense that we should delete all the elements we inserted (hint: do we already have a method for that?). However, what else do we dynamically allocate that we need to delete?

    printList

    This one's interesting becaus it's a non-member function, which means it doesn't have access to any private variables.

    diff --git a/labs/lab02/index.md b/labs/lab02/index.md index ed46748a9..6bfa0f343 100644 --- a/labs/lab02/index.md +++ b/labs/lab02/index.md @@ -74,7 +74,7 @@ Below is a UML diagram showing how these classes interact with each other. ![UML diagram](list-diagram.png) -This diagram shows a list containing two elements, the integers 3 and 7. Note that there are more methods in the List and ListItr classes than what is shown above. The head and tail pointers in the List class point to dummy nodes -- they are put there to make inserting elements into the list easier. It doesn't matter what the value of the dummy nodes is set to, as it won't be used. Each ListNode points to the nodes before and after it (although the dummy nodes each have one pointer pointing to NULL). +This diagram shows a list containing two elements, the integers 3 and 7. Note that there are more methods in the List and ListItr classes than what is shown above. The head and tail pointers in the List class point to dummy nodes -- they are put there to make inserting elements into the list easier. It doesn't matter what the value of the dummy nodes is set to, as it won't be used. Each ListNode points to the nodes before and after it (although the dummy nodes each have one pointer pointing to nullptr). Thus, our doubly linked list will have only one List object and many ListNode objects (2 more than the number of elements in the list). A ListItr is a separate object, which points to one element in the list (possibly a dummy node). As you call the various methods in ListItr to move the iterator forward and backward, the node that it points to will change. @@ -117,7 +117,7 @@ as well as suggested implementation order: When beginning to test your code, chances are your program will crash unexpectedly with a message similar to `Segmentation fault (core dumped)`, commonly referred to as a _segfault_. Get prepared to see these often throughout the course, as unlike other programming languages like Java, C++ does not give any extra debugging information on a crash. In order to determine where and why your program is crashing, run your code through the debugger and look at the backtrace. -Segfaults generally indicate that you are trying to dereference a NULL or invalid pointer, so those are good things to look out for. +Segfaults generally indicate that you are trying to dereference a nullptr or invalid pointer, so those are good things to look out for. #### The constructor #### By the time the constructor finishes, we should have a functioning (but empty) list. @@ -196,7 +196,7 @@ It should also make sure that `head` and `tail` no longer point to those deleted Since we have been dynamically allocating ListNodes, we must also be responsible for deleting them to ensure we do not leak memory. There are multiple ways to iterate through the list and `delete` each ListNode -- experiment and see what makes the most sense to you.\ **Important:** once you delete a ListNode, you can no longer reliably access any of its data, such as its `next` or `previous` pointers! -To make sure you don't do this accidentally, we recommend setting each ListNode to NULL as soon as you delete it. +To make sure you don't do this accidentally, we recommend setting each ListNode to nullptr as soon as you delete it. The destructor should delete _all_ dynamically-allocated memory, as we no longer need this List instance. Thus, it makes sense that we should delete all the elements we inserted (hint: do we already have a method for that?). diff --git a/labs/lab04/index.html b/labs/lab04/index.html index b3ba908b9..564cae66e 100644 --- a/labs/lab04/index.html +++ b/labs/lab04/index.html @@ -180,7 +180,7 @@

    Worksheet

    C++ Type Size in bytes? Max value? (base 16/hex) -NULL is stored as (in hex)? +nullptr is stored as (in hex)? @@ -244,7 +244,7 @@

    sizeof

    Max values

    climits will come in handy here again. For types not in climits, you should reason about how the data is stored and the size of that type.

    Viewing values

    -

    For some parts of this lab, it is helpful to assign a value to a variable, then inspect that variable's contents using a debugger. You can write a simple C++ program that creates the variables, and stores the appropriate value (zero, one, or NULL) into them. Compile (remember the -g flag!), load the debugger, set a breakpoint, and start the program execution.

    +

    For some parts of this lab, it is helpful to assign a value to a variable, then inspect that variable's contents using a debugger. You can write a simple C++ program that creates the variables, and stores the appropriate value (zero, one, or nullptr) into them. Compile (remember the -g flag!), load the debugger, set a breakpoint, and start the program execution.

    When using LLDB, you can use the 'x' (for 'eXamine') command to print out the pointee of an address. Consider the C++ program that has two variables defined, int i and int *p. To print out the int variable i, you would enter x &i (as you have to enter the address of where the data is located). If p is a pointer to a value, you would enter x p to print out the pointee. This may print it using many more hexadecimal digits than you wanted, so you can add a parameter to the 'x' command to have it print only a certain amount:

    • x/xb p: this will print the one byte at the address that is pointed to by p
    • diff --git a/labs/lab04/index.md b/labs/lab04/index.md index 77afd8f1e..9295f1bae 100644 --- a/labs/lab04/index.md +++ b/labs/lab04/index.md @@ -151,7 +151,7 @@ The [inlab4.doc](inlab4.doc) worksheet asks you to fill in two tables describing   -| C++ Type | Size in bytes? | Max value? (base 16/hex) | NULL is stored as (in hex)? | +| C++ Type | Size in bytes? | Max value? (base 16/hex) | nullptr is stored as (in hex)? | |----------|----------------|----------------------|-----------------------------| | int\* | | | | | char\* | | | | @@ -222,7 +222,7 @@ For types not in `climits`, you should reason about how the data is stored and t #### Viewing values #### For some parts of this lab, it is helpful to assign a value to a variable, then inspect that variable's contents using a debugger. -You can write a simple C++ program that creates the variables, and stores the appropriate value (zero, one, or NULL) into them. +You can write a simple C++ program that creates the variables, and stores the appropriate value (zero, one, or nullptr) into them. Compile (remember the `-g` flag!), load the debugger, set a breakpoint, and start the program execution. When using LLDB, you can use the 'x' (for 'eXamine') command to print out the pointee of an address. diff --git a/labs/lab05/code/inlab/BinaryNode.cpp b/labs/lab05/code/inlab/BinaryNode.cpp index fa7d55487..5b4601055 100644 --- a/labs/lab05/code/inlab/BinaryNode.cpp +++ b/labs/lab05/code/inlab/BinaryNode.cpp @@ -4,13 +4,13 @@ using namespace std; BinaryNode::BinaryNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } BinaryNode::~BinaryNode() { delete left; delete right; - left = NULL; - right = NULL; -} \ No newline at end of file + left = nullptr; + right = nullptr; +} diff --git a/labs/lab05/code/inlab/BinaryNode.cpp.html b/labs/lab05/code/inlab/BinaryNode.cpp.html index 69c942700..c226e914d 100644 --- a/labs/lab05/code/inlab/BinaryNode.cpp.html +++ b/labs/lab05/code/inlab/BinaryNode.cpp.html @@ -15,15 +15,16 @@ BinaryNode::BinaryNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } BinaryNode::~BinaryNode() { delete left; delete right; - left = NULL; - right = NULL; -} + left = nullptr; + right = nullptr; +} + diff --git a/labs/lab05/code/inlab/BinarySearchTree.cpp b/labs/lab05/code/inlab/BinarySearchTree.cpp index e64b72f2f..6679be240 100644 --- a/labs/lab05/code/inlab/BinarySearchTree.cpp +++ b/labs/lab05/code/inlab/BinarySearchTree.cpp @@ -5,12 +5,12 @@ using namespace std; BinarySearchTree::BinarySearchTree() { - root = NULL; + root = nullptr; } BinarySearchTree::~BinarySearchTree() { delete root; - root = NULL; + root = nullptr; } // insert finds a position for x in the tree and places it there. @@ -26,34 +26,34 @@ void BinarySearchTree::remove(const string& x) { // private helper for remove to allow recursion over different nodes. returns // a BinaryNode* that is assigned to the original node. BinaryNode* BinarySearchTree::remove(BinaryNode*& n, const string& x) { - if (n == NULL) { - return NULL; + if (n == nullptr) { + return nullptr; } // first look for x if (x == n->value) { // found - if (n->left == NULL && n->right == NULL) { + if (n->left == nullptr && n->right == nullptr) { // no children // just delete it :) delete n; - n = NULL; - return NULL; - } else if (n->left == NULL) { + n = nullptr; + return nullptr; + } else if (n->left == nullptr) { // single child (right) // remove current node and return right child node for parent BinaryNode* temp = n->right; - n->right = NULL; + n->right = nullptr; delete n; - n = NULL; + n = nullptr; return temp; - } else if (n->right == NULL) { + } else if (n->right == nullptr) { // single child (left) // remove current node and return left child node for parent BinaryNode* temp = n->left; - n->left = NULL; + n->left = nullptr; delete n; - n = NULL; + n = nullptr; return temp; } else { // two children @@ -89,7 +89,7 @@ int BinarySearchTree::numNodes() const { // min finds the string with the smallest value in a subtree. string BinarySearchTree::min(BinaryNode* node) const { // go to bottom-left node - if (node->left == NULL) { + if (node->left == nullptr) { return node->value; } return min(node->left); @@ -98,19 +98,19 @@ string BinarySearchTree::min(BinaryNode* node) const { // Helper function to print branches of the binary tree // You do not need to know how this function works. void showTrunks(Trunk* p) { - if (p == NULL) return; + if (p == nullptr) return; showTrunks(p->prev); cout << p->str; } void BinarySearchTree::printTree() { - printTree(root, NULL, false); + printTree(root, nullptr, false); } // Recursive function to print binary tree // It uses inorder traversal void BinarySearchTree::printTree(BinaryNode* root, Trunk* prev, bool isRight) { - if (root == NULL) return; + if (root == nullptr) return; string prev_str = " "; Trunk* trunk = new Trunk(prev, prev_str); diff --git a/labs/lab05/code/inlab/BinarySearchTree.cpp.html b/labs/lab05/code/inlab/BinarySearchTree.cpp.html index c5ec5fce1..cc141eb83 100644 --- a/labs/lab05/code/inlab/BinarySearchTree.cpp.html +++ b/labs/lab05/code/inlab/BinarySearchTree.cpp.html @@ -16,12 +16,12 @@ using namespace std; BinarySearchTree::BinarySearchTree() { - root = NULL; + root = nullptr; } BinarySearchTree::~BinarySearchTree() { delete root; - root = NULL; + root = nullptr; } // insert finds a position for x in the tree and places it there. @@ -37,34 +37,34 @@ // private helper for remove to allow recursion over different nodes. returns // a BinaryNode* that is assigned to the original node. BinaryNode* BinarySearchTree::remove(BinaryNode*& n, const string& x) { - if (n == NULL) { - return NULL; + if (n == nullptr) { + return nullptr; } // first look for x if (x == n->value) { // found - if (n->left == NULL && n->right == NULL) { + if (n->left == nullptr && n->right == nullptr) { // no children // just delete it :) delete n; - n = NULL; - return NULL; - } else if (n->left == NULL) { + n = nullptr; + return nullptr; + } else if (n->left == nullptr) { // single child (right) // remove current node and return right child node for parent BinaryNode* temp = n->right; - n->right = NULL; + n->right = nullptr; delete n; - n = NULL; + n = nullptr; return temp; - } else if (n->right == NULL) { + } else if (n->right == nullptr) { // single child (left) // remove current node and return left child node for parent BinaryNode* temp = n->left; - n->left = NULL; + n->left = nullptr; delete n; - n = NULL; + n = nullptr; return temp; } else { // two children @@ -100,7 +100,7 @@ // min finds the string with the smallest value in a subtree. string BinarySearchTree::min(BinaryNode* node) const { // go to bottom-left node - if (node->left == NULL) { + if (node->left == nullptr) { return node->value; } return min(node->left); @@ -109,19 +109,19 @@ // Helper function to print branches of the binary tree // You do not need to know how this function works. void showTrunks(Trunk* p) { - if (p == NULL) return; + if (p == nullptr) return; showTrunks(p->prev); cout << p->str; } void BinarySearchTree::printTree() { - printTree(root, NULL, false); + printTree(root, nullptr, false); } // Recursive function to print binary tree // It uses inorder traversal void BinarySearchTree::printTree(BinaryNode* root, Trunk* prev, bool isRight) { - if (root == NULL) return; + if (root == nullptr) return; string prev_str = " "; Trunk* trunk = new Trunk(prev, prev_str); diff --git a/labs/lab05/code/postlab/AVLNode.cpp b/labs/lab05/code/postlab/AVLNode.cpp index 40bf07a8a..a1fbfa30d 100644 --- a/labs/lab05/code/postlab/AVLNode.cpp +++ b/labs/lab05/code/postlab/AVLNode.cpp @@ -4,14 +4,14 @@ using namespace std; AVLNode::AVLNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; height = 0; } AVLNode::~AVLNode() { delete left; delete right; - left = NULL; - right = NULL; -} \ No newline at end of file + left = nullptr; + right = nullptr; +} diff --git a/labs/lab05/code/postlab/AVLNode.cpp.html b/labs/lab05/code/postlab/AVLNode.cpp.html index c66c624b8..75c146ecc 100644 --- a/labs/lab05/code/postlab/AVLNode.cpp.html +++ b/labs/lab05/code/postlab/AVLNode.cpp.html @@ -15,16 +15,17 @@ AVLNode::AVLNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; height = 0; } AVLNode::~AVLNode() { delete left; delete right; - left = NULL; - right = NULL; -} + left = nullptr; + right = nullptr; +} + diff --git a/labs/lab05/code/postlab/AVLTree.cpp b/labs/lab05/code/postlab/AVLTree.cpp index 7a23e922d..28e6580a1 100644 --- a/labs/lab05/code/postlab/AVLTree.cpp +++ b/labs/lab05/code/postlab/AVLTree.cpp @@ -5,12 +5,12 @@ using namespace std; AVLTree::AVLTree() { - root = NULL; + root = nullptr; } AVLTree::~AVLTree() { delete root; - root = NULL; + root = nullptr; } // insert finds a position for x in the tree and places it there, rebalancing @@ -60,31 +60,31 @@ AVLNode* AVLTree::rotateRight(AVLNode*& n) { // private helper for remove to allow recursion over different nodes. // Returns an AVLNode* that is assigned to the original node. AVLNode* AVLTree::remove(AVLNode*& n, const string& x) { - if (n == NULL) { - return NULL; + if (n == nullptr) { + return nullptr; } // first look for x if (x == n->value) { // found - if (n->left == NULL && n->right == NULL) { + if (n->left == nullptr && n->right == nullptr) { // no children delete n; - n = NULL; - return NULL; - } else if (n->left == NULL) { + n = nullptr; + return nullptr; + } else if (n->left == nullptr) { // Single child (left) AVLNode* temp = n->right; - n->right = NULL; + n->right = nullptr; delete n; - n = NULL; + n = nullptr; return temp; - } else if (n->right == NULL) { + } else if (n->right == nullptr) { // Single child (right) AVLNode* temp = n->left; - n->left = NULL; + n->left = nullptr; delete n; - n = NULL; + n = nullptr; return temp; } else { // two children -- tree may become unbalanced after deleting n @@ -108,7 +108,7 @@ AVLNode* AVLTree::remove(AVLNode*& n, const string& x) { // min finds the string with the smallest value in a subtree. string AVLTree::min(AVLNode* node) const { // go to bottom-left node - if (node->left == NULL) { + if (node->left == nullptr) { return node->value; } return min(node->left); @@ -117,7 +117,7 @@ string AVLTree::min(AVLNode* node) const { // height returns the value of the height field in a node. // If the node is null, it returns -1. int AVLTree::height(AVLNode* node) const { - if (node == NULL) { + if (node == nullptr) { return -1; } return node->height; @@ -134,7 +134,7 @@ int max(int a, int b) { // Helper function to print branches of the binary tree // You do not need to know how this function works. void showTrunks(Trunk* p) { - if (p == NULL) return; + if (p == nullptr) return; showTrunks(p->prev); cout << p->str; } @@ -142,7 +142,7 @@ void showTrunks(Trunk* p) { // Recursive function to print binary tree // It uses inorder traversal void AVLTree::printTree(AVLNode* root, Trunk* prev, bool isRight) { - if (root == NULL) return; + if (root == nullptr) return; string prev_str = " "; Trunk* trunk = new Trunk(prev, prev_str); @@ -171,5 +171,5 @@ void AVLTree::printTree(AVLNode* root, Trunk* prev, bool isRight) { } void AVLTree::printTree() { - printTree(root, NULL, false); + printTree(root, nullptr, false); } diff --git a/labs/lab05/code/postlab/AVLTree.cpp.html b/labs/lab05/code/postlab/AVLTree.cpp.html index 0ecb9902a..1946e2162 100644 --- a/labs/lab05/code/postlab/AVLTree.cpp.html +++ b/labs/lab05/code/postlab/AVLTree.cpp.html @@ -16,12 +16,12 @@ using namespace std; AVLTree::AVLTree() { - root = NULL; + root = nullptr; } AVLTree::~AVLTree() { delete root; - root = NULL; + root = nullptr; } // insert finds a position for x in the tree and places it there, rebalancing @@ -71,31 +71,31 @@ // private helper for remove to allow recursion over different nodes. // Returns an AVLNode* that is assigned to the original node. AVLNode* AVLTree::remove(AVLNode*& n, const string& x) { - if (n == NULL) { - return NULL; + if (n == nullptr) { + return nullptr; } // first look for x if (x == n->value) { // found - if (n->left == NULL && n->right == NULL) { + if (n->left == nullptr && n->right == nullptr) { // no children delete n; - n = NULL; - return NULL; - } else if (n->left == NULL) { + n = nullptr; + return nullptr; + } else if (n->left == nullptr) { // Single child (left) AVLNode* temp = n->right; - n->right = NULL; + n->right = nullptr; delete n; - n = NULL; + n = nullptr; return temp; - } else if (n->right == NULL) { + } else if (n->right == nullptr) { // Single child (right) AVLNode* temp = n->left; - n->left = NULL; + n->left = nullptr; delete n; - n = NULL; + n = nullptr; return temp; } else { // two children -- tree may become unbalanced after deleting n @@ -119,7 +119,7 @@ // min finds the string with the smallest value in a subtree. string AVLTree::min(AVLNode* node) const { // go to bottom-left node - if (node->left == NULL) { + if (node->left == nullptr) { return node->value; } return min(node->left); @@ -128,7 +128,7 @@ // height returns the value of the height field in a node. // If the node is null, it returns -1. int AVLTree::height(AVLNode* node) const { - if (node == NULL) { + if (node == nullptr) { return -1; } return node->height; @@ -145,7 +145,7 @@ // Helper function to print branches of the binary tree // You do not need to know how this function works. void showTrunks(Trunk* p) { - if (p == NULL) return; + if (p == nullptr) return; showTrunks(p->prev); cout << p->str; } @@ -153,7 +153,7 @@ // Recursive function to print binary tree // It uses inorder traversal void AVLTree::printTree(AVLNode* root, Trunk* prev, bool isRight) { - if (root == NULL) return; + if (root == nullptr) return; string prev_str = " "; Trunk* trunk = new Trunk(prev, prev_str); @@ -182,7 +182,7 @@ } void AVLTree::printTree() { - printTree(root, NULL, false); + printTree(root, nullptr, false); } diff --git a/labs/lab05/code/prelab/TreeCalc.cpp b/labs/lab05/code/prelab/TreeCalc.cpp index 840c75d63..2f2fb8587 100644 --- a/labs/lab05/code/prelab/TreeCalc.cpp +++ b/labs/lab05/code/prelab/TreeCalc.cpp @@ -59,7 +59,7 @@ void TreeCalc::printPostfix(TreeNode* tree) const { // Prints tree in all 3 (post, in, pre) forms // DO NOT MODIFY void TreeCalc::printOutput() const { - if (expressionStack.size() != 0 && expressionStack.top() != NULL) { + if (expressionStack.size() != 0 && expressionStack.top() != nullptr) { TreeNode* tree = expressionStack.top(); cout << "Expression tree in postfix expression: "; printPostfix(tree); diff --git a/labs/lab05/code/prelab/TreeCalc.cpp.html b/labs/lab05/code/prelab/TreeCalc.cpp.html index cacfa529f..f7b169d2a 100644 --- a/labs/lab05/code/prelab/TreeCalc.cpp.html +++ b/labs/lab05/code/prelab/TreeCalc.cpp.html @@ -70,7 +70,7 @@ // Prints tree in all 3 (post, in, pre) forms // DO NOT MODIFY void TreeCalc::printOutput() const { - if (expressionStack.size() != 0 && expressionStack.top() != NULL) { + if (expressionStack.size() != 0 && expressionStack.top() != nullptr) { TreeNode* tree = expressionStack.top(); cout << "Expression tree in postfix expression: "; printPostfix(tree); diff --git a/labs/lab05/code/prelab/TreeNode.cpp b/labs/lab05/code/prelab/TreeNode.cpp index e309f4930..69c289723 100644 --- a/labs/lab05/code/prelab/TreeNode.cpp +++ b/labs/lab05/code/prelab/TreeNode.cpp @@ -4,16 +4,16 @@ #include "TreeNode.h" -// Default Constructor - left and right are NULL, value '?' +// Default Constructor - left and right are nullptr, value '?' TreeNode::TreeNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } // Constructor - sets value to val TreeNode::TreeNode(const string& val) { value = val; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } diff --git a/labs/lab05/code/prelab/TreeNode.cpp.html b/labs/lab05/code/prelab/TreeNode.cpp.html index 99c34d913..bff4f1e85 100644 --- a/labs/lab05/code/prelab/TreeNode.cpp.html +++ b/labs/lab05/code/prelab/TreeNode.cpp.html @@ -15,18 +15,18 @@ #include "TreeNode.h" -// Default Constructor - left and right are NULL, value '?' +// Default Constructor - left and right are nullptr, value '?' TreeNode::TreeNode() { value = "?"; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } // Constructor - sets value to val TreeNode::TreeNode(const string& val) { value = val; - left = NULL; - right = NULL; + left = nullptr; + right = nullptr; } diff --git a/labs/lab06/code/getWordInGrid.cpp b/labs/lab06/code/getWordInGrid.cpp index 3c693567f..95cb86f27 100644 --- a/labs/lab06/code/getWordInGrid.cpp +++ b/labs/lab06/code/getWordInGrid.cpp @@ -1,4 +1,5 @@ -/** This file defines and demonstrates two necessary components for +/** + * This file defines and demonstrates two necessary components for * the hash table lab for CS 2150. The first is the use of the * getWordInGrid() function, which is used for retrieving a word in a * grid of letters in one of the cardinal 8 directions (north, @@ -9,23 +10,25 @@ * Written by Aaron Bloomfield, 2009 */ -#include +#include #include +#include #include using namespace std; // We create a 2-D array of some big size, and assume that the grid // read in will be less than this size (a valid assumption for lab 6) -#define MAXROWS 500 -#define MAXCOLS 500 -char grid[MAXROWS][MAXCOLS]; +const int MAXROWS = 500; +const int MAXCOLS = 500; +array, MAXROWS> grid; // Forward declarations bool readInGrid(string filename, int& rows, int& cols); string getWordInGrid(int startRow, int startCol, int dir, int len, int numRows, int numCols); -/** The main() function shows how to call both the readInGrid() +/** + * The main() function shows how to call both the readInGrid() * function as well as the getWordInGrid() function. */ int main() { @@ -42,7 +45,6 @@ int main() { // Get a word (of length 10), starting at position (2,2) in the // array, in each of the 8 directions - cout << endl; for (int i = 0; i < 8; i++) { cout << i << ": " << getWordInGrid(2, 2, i, 10, rows, cols) << endl; } @@ -50,8 +52,9 @@ int main() { return 0; } -/** This function will read in a grid file, as per the format in the - * CS 2150 lab 6 document, into a global grid[][] array. It uses C++ +/** + * This function will read in a grid file, as per the format in the + * CS 2150 lab 6 document, into a global 2D grid array. It uses C++ * file streams, and thus requires the the #include header. * * @return true or false, depending on whether the file was @@ -86,8 +89,7 @@ bool readInGrid(string filename, int& rows, int& cols) { // close the file file.close(); - // convert the string read in to the 2-D grid format into the - // grid[][] array. + // convert the string read in to the 2-D grid format into the grid. // In the process, we'll print the grid to the screen as well. int pos = 0; // the current position in the input data for (int r = 0; r < rows; r++) { @@ -100,11 +102,12 @@ bool readInGrid(string filename, int& rows, int& cols) { return true; } -/** This function will retrieve a word in a grid of letters in a given +/** + * This function will retrieve a word in a grid of letters in a given * direction. If the end of the grid is encountered before the length * of the desired string is reached, then a shorter string will be - * returned. The data is retrieved from a global char grid[][] - * array, which is assumed to be defined (and in scope). NOTE: The + * returned. The data is retrieved from a global grid, + * which is assumed to be defined (and in scope). NOTE: The * return value is a static string variable (for efficiency * reasons), so a successive return value will overwrite a previous * return value. @@ -121,10 +124,8 @@ bool readInGrid(string filename, int& rows, int& cols) { * possible up to the edge of the grid, so the returned * string may not have the same length as this parameter * indicates). - * @param numRows The number of rows in the global char grid[][] - * array. - * @param numCols The number of columns in the global char grid[][] - * array. + * @param numRows The number of rows in the grid. + * @param numCols The number of columns in the grid. */ string getWordInGrid (int startRow, int startCol, int dir, int len, int numRows, int numCols) { diff --git a/labs/lab06/code/getWordInGrid.cpp.html b/labs/lab06/code/getWordInGrid.cpp.html index bb765d44a..5343cde90 100644 --- a/labs/lab06/code/getWordInGrid.cpp.html +++ b/labs/lab06/code/getWordInGrid.cpp.html @@ -9,7 +9,8 @@ getWordInGrid.cpp -
      /** This file defines and demonstrates two necessary components for
      +
      /**
      + * This file defines and demonstrates two necessary components for
        * the hash table lab for CS 2150.  The first is the use of the
        * getWordInGrid() function, which is used for retrieving a word in a
        * grid of letters in one of the cardinal 8 directions (north,
      @@ -20,23 +21,25 @@
        * Written by Aaron Bloomfield, 2009
        */
       
      -#include <iostream>
      +#include <array>
       #include <fstream>
      +#include <iostream>
       #include <string>
       using namespace std;
       
       // We create a 2-D array of some big size, and assume that the grid
       // read in will be less than this size (a valid assumption for lab 6)
      -#define MAXROWS 500
      -#define MAXCOLS 500
      -char grid[MAXROWS][MAXCOLS];
      +const int MAXROWS = 500;
      +const int MAXCOLS = 500;
      +array<array<char, MAXCOLS>, MAXROWS> grid;
       
       // Forward declarations
       bool readInGrid(string filename, int& rows, int& cols);
       string getWordInGrid(int startRow, int startCol, int dir, int len,
                            int numRows, int numCols);
       
      -/** The main() function shows how to call both the readInGrid()
      +/**
      + * The main() function shows how to call both the readInGrid()
        * function as well as the getWordInGrid() function.
        */
       int main() {
      @@ -53,7 +56,6 @@
       
           // Get a word (of length 10), starting at position (2,2) in the
           // array, in each of the 8 directions
      -    cout << endl;
           for (int i = 0; i < 8; i++) {
               cout << i << ": " << getWordInGrid(2, 2, i, 10, rows, cols) << endl;
           }
      @@ -61,8 +63,9 @@
           return 0;
       }
       
      -/** This function will read in a grid file, as per the format in the
      - * CS 2150 lab 6 document, into a global grid[][] array.  It uses C++
      +/**
      + * This function will read in a grid file, as per the format in the
      + * CS 2150 lab 6 document, into a global 2D grid array.  It uses C++
        * file streams, and thus requires the the <fstream> #include header.
        *
        * @return true or false, depending on whether the file was
      @@ -97,8 +100,7 @@
           // close the file
           file.close();
       
      -    // convert the string read in to the 2-D grid format into the
      -    // grid[][] array.
      +    // convert the string read in to the 2-D grid format into the grid.
           // In the process, we'll print the grid to the screen as well.
           int pos = 0; // the current position in the input data
           for (int r = 0; r < rows; r++) {
      @@ -111,11 +113,12 @@
           return true;
       }
       
      -/** This function will retrieve a word in a grid of letters in a given
      +/**
      + * This function will retrieve a word in a grid of letters in a given
        * direction.  If the end of the grid is encountered before the length
        * of the desired string is reached, then a shorter string will be
      - * returned.  The data is retrieved from a global char grid[][]
      - * array, which is assumed to be defined (and in scope).  NOTE: The
      + * returned.  The data is retrieved from a global grid,
      + * which is assumed to be defined (and in scope).  NOTE: The
        * return value is a static string variable (for efficiency
        * reasons), so a successive return value will overwrite a previous
        * return value.
      @@ -132,10 +135,8 @@
        *            possible up to the edge of the grid, so the returned
        *            string may not have the same length as this parameter
        *            indicates).
      - * @param numRows The number of rows in the global char grid[][]
      - *                array.
      - * @param numCols The number of columns in the global char grid[][]
      - *                array.
      + * @param numRows The number of rows in the grid.
      + * @param numCols The number of columns in the grid.
        */
       string getWordInGrid (int startRow, int startCol, int dir, int len,
                             int numRows, int numCols) {
      diff --git a/labs/lab06/code/timer.cpp b/labs/lab06/code/timer.cpp
      index 60b3dcfe4..37334dc3c 100644
      --- a/labs/lab06/code/timer.cpp
      +++ b/labs/lab06/code/timer.cpp
      @@ -1,15 +1,5 @@
       #include "timer.h"
       
      -timer::timer() {
      -    running = false;
      -}
      -
      -timer::timer(timer& t) {
      -    running = t.running;
      -    start_time = t.start_time;
      -    stop_time = t.stop_time;
      -}
      -
       void timer::start() {
           if (!running) {
               running = true;
      @@ -31,6 +21,6 @@ double timer::getTime() {
           return duration_cast>(stop_time - start_time).count();
       }
       
      -ostream& operator<<(ostream& out, timer& t) {
      -    return out << to_string(t.getTime());
      +ostream& operator<<(ostream& out, timer& timer) {
      +    return out << to_string(timer.getTime());
       }
      diff --git a/labs/lab06/code/timer.cpp.html b/labs/lab06/code/timer.cpp.html
      index 259b840fd..71fb55b8d 100644
      --- a/labs/lab06/code/timer.cpp.html
      +++ b/labs/lab06/code/timer.cpp.html
      @@ -11,16 +11,6 @@
       
       
      #include "timer.h"
       
      -timer::timer() {
      -    running = false;
      -}
      -
      -timer::timer(timer& t) {
      -    running = t.running;
      -    start_time = t.start_time;
      -    stop_time = t.stop_time;
      -}
      -
       void timer::start() {
           if (!running) {
               running = true;
      @@ -42,8 +32,8 @@
           return duration_cast<duration<double>>(stop_time - start_time).count();
       }
       
      -ostream& operator<<(ostream& out, timer& t) {
      -    return out << to_string(t.getTime());
      +ostream& operator<<(ostream& out, timer& timer) {
      +    return out << to_string(timer.getTime());
       }
       
      diff --git a/labs/lab06/code/timer.h b/labs/lab06/code/timer.h index 5612d8818..5e25430d7 100644 --- a/labs/lab06/code/timer.h +++ b/labs/lab06/code/timer.h @@ -13,8 +13,7 @@ using namespace std::chrono; class timer { public: - timer(); - timer(timer& myTimer); + timer() = default; void start(); void stop(); @@ -22,10 +21,10 @@ class timer { double getTime(); private: steady_clock::time_point start_time, stop_time; - bool running; + bool running = false; }; -ostream& operator<<(ostream& theStream, timer& theTimer); +ostream& operator<<(ostream& out, timer& timer); #endif diff --git a/labs/lab06/code/timer.h.html b/labs/lab06/code/timer.h.html index 3fb1f6ca8..6f2e99164 100644 --- a/labs/lab06/code/timer.h.html +++ b/labs/lab06/code/timer.h.html @@ -24,8 +24,7 @@ class timer { public: - timer(); - timer(timer& myTimer); + timer() = default; void start(); void stop(); @@ -33,11 +32,11 @@ double getTime(); private: steady_clock::time_point start_time, stop_time; - bool running; + bool running = false; }; -ostream& operator<<(ostream& theStream, timer& theTimer); +ostream& operator<<(ostream& out, timer& timer); #endif
      diff --git a/labs/lab07/timer.cpp b/labs/lab07/timer.cpp index 06d21437b..0fc2d91b7 100644 --- a/labs/lab07/timer.cpp +++ b/labs/lab07/timer.cpp @@ -1,15 +1,5 @@ #include "timer.h" -timer::timer() { - running = false; -} - -timer::timer(timer& t) { - running = t.running; - start_time = t.start_time; - stop_time = t.stop_time; -} - void timer::start() { if (!running) { running = true; @@ -32,6 +22,6 @@ double timer::getTime() { } // Prints the time elapsed, in milliseconds -ostream& operator<<(ostream& out, timer& t) { - return out << to_string(static_cast(t.getTime() * 1000)); +ostream& operator<<(ostream& out, timer& timer) { + return out << to_string(static_cast(timer.getTime() * 1000)); } diff --git a/labs/lab07/timer.cpp.html b/labs/lab07/timer.cpp.html index a5b695d74..f81e09d63 100644 --- a/labs/lab07/timer.cpp.html +++ b/labs/lab07/timer.cpp.html @@ -11,16 +11,6 @@
      #include "timer.h"
       
      -timer::timer() {
      -    running = false;
      -}
      -
      -timer::timer(timer& t) {
      -    running = t.running;
      -    start_time = t.start_time;
      -    stop_time = t.stop_time;
      -}
      -
       void timer::start() {
           if (!running) {
               running = true;
      @@ -43,8 +33,8 @@
       }
       
       // Prints the time elapsed, in milliseconds
      -ostream& operator<<(ostream& out, timer& t) {
      -    return out << to_string(static_cast<int>(t.getTime() * 1000));
      +ostream& operator<<(ostream& out, timer& timer) {
      +    return out << to_string(static_cast<int>(timer.getTime() * 1000));
       }
       
      diff --git a/labs/lab07/timer.h b/labs/lab07/timer.h index 5612d8818..5e25430d7 100644 --- a/labs/lab07/timer.h +++ b/labs/lab07/timer.h @@ -13,8 +13,7 @@ using namespace std::chrono; class timer { public: - timer(); - timer(timer& myTimer); + timer() = default; void start(); void stop(); @@ -22,10 +21,10 @@ class timer { double getTime(); private: steady_clock::time_point start_time, stop_time; - bool running; + bool running = false; }; -ostream& operator<<(ostream& theStream, timer& theTimer); +ostream& operator<<(ostream& out, timer& timer); #endif diff --git a/labs/lab07/timer.h.html b/labs/lab07/timer.h.html index 3fb1f6ca8..6f2e99164 100644 --- a/labs/lab07/timer.h.html +++ b/labs/lab07/timer.h.html @@ -24,8 +24,7 @@ class timer { public: - timer(); - timer(timer& myTimer); + timer() = default; void start(); void stop(); @@ -33,11 +32,11 @@ double getTime(); private: steady_clock::time_point start_time, stop_time; - bool running; + bool running = false; }; -ostream& operator<<(ostream& theStream, timer& theTimer); +ostream& operator<<(ostream& out, timer& timer); #endif
      diff --git a/labs/lab08-64bit/main.cpp b/labs/lab08-64bit/main.cpp index 202992396..d79209799 100644 --- a/labs/lab08-64bit/main.cpp +++ b/labs/lab08-64bit/main.cpp @@ -1,8 +1,7 @@ // main.cpp #include -#include -#include +#include using namespace std; @@ -14,30 +13,31 @@ extern "C" long vecsum (long*, long); // Originally written by Adam Ferrari, and updated by Aaron Bloomfield -int main () { - - // delcare the local variables - long n, *vec, sum; +int main() { + // declare the local variables + long n; + long* vec; + long sum; // how big is the array we want to use? - cout << "Please enter a array size: "; + cout << "Please enter a array size: "; cin >> n; // sanity check the array size if (n <= 0) { - cerr << "Array size must be greater than zero.\n"; + cerr << "Array size must be greater than zero." << endl; return 1; } // allocate the array vec = new long[n]; - // use current time as random seed - srand((unsigned) time(NULL)); + // initialize Mersenne-Twister randm number generator + mt19937 gen(random_device{}()); // fill the array with random values for (long i = 0; i < n; ++i) { - vec[i] = rand() % 100; + vec[i] = gen() % 100; cout << "\tvec[" << i << "] = " << vec[i] << endl; } @@ -46,7 +46,7 @@ int main () { cout << "The sum of all array elements is " << sum << endl; // properly deallocate the array - delete [] vec; + delete[] vec; // all done! return 0; diff --git a/labs/lab08-64bit/main.cpp.html b/labs/lab08-64bit/main.cpp.html index ba1a2ced1..89167beee 100644 --- a/labs/lab08-64bit/main.cpp.html +++ b/labs/lab08-64bit/main.cpp.html @@ -12,8 +12,7 @@
      // main.cpp
       
       #include <iostream>
      -#include <time.h>
      -#include <cstdlib>
      +#include <random>
       
       using namespace std;
       
      @@ -25,30 +24,31 @@
       
       // Originally written by Adam Ferrari, and updated by Aaron Bloomfield
       
      -int  main () {
      -
      -    // delcare the local variables
      -    long  n, *vec, sum;
      +int main() {
      +    // declare the local variables
      +    long n;
      +    long* vec;
      +    long sum;
       
           // how big is the array we want to use?
      -    cout << "Please enter a array size:  ";
      +    cout << "Please enter a array size: ";
           cin >> n;
       
           // sanity check the array size
           if (n <= 0) {
      -        cerr << "Array size must be greater than zero.\n";
      +        cerr << "Array size must be greater than zero." << endl;
               return 1;
           }
       
           // allocate the array
           vec = new long[n];
       
      -    // use current time as random seed
      -    srand((unsigned) time(NULL));
      +    // initialize Mersenne-Twister randm number generator
      +    mt19937 gen(random_device{}());
       
           // fill the array with random values
           for (long i = 0; i < n; ++i) {
      -        vec[i] = rand() % 100;
      +        vec[i] = gen() % 100;
               cout << "\tvec[" << i << "] = " << vec[i] << endl;
           }
       
      @@ -57,7 +57,7 @@
           cout << "The sum of all array elements is " << sum << endl;
       
           // properly deallocate the array
      -    delete [] vec;
      +    delete[] vec;
       
           // all done!
           return 0;
      diff --git a/slides/01-cpp.html b/slides/01-cpp.html
      index f320a8bdc..41b1500f3 100644
      --- a/slides/01-cpp.html
      +++ b/slides/01-cpp.html
      @@ -954,9 +954,9 @@ 

      Pointer Pitfalls: Uninitialized Pointers

      *p = n; //ERROR!!!
      • p does not have a valid memory address!
      • -
      • A common initializer value used by programmers is NULL -
        int *p=NULL; // better code, then add code to check 
        -             // for NULL value
        +
      • A common initializer value used by programmers is nullptr +
        int *p=nullptr; // better code, then add code to check 
        +             // for nullptr value
         
    @@ -1271,7 +1271,7 @@

    swap()

    - A reference is like a pointer -- it holds an address -- with three primary differences: 1. Its address cannot change (its address is constant) 2. It MUST be initialized upon declaration - - Cannot (easily) be initialized to `NULL` + - Cannot (easily) be initialized to `nullptr` 3. Has implicit dereferencing diff --git a/slides/03-numbers.html b/slides/03-numbers.html index 52e32f559..5a137084d 100644 --- a/slides/03-numbers.html +++ b/slides/03-numbers.html @@ -872,8 +872,9 @@

    Mantissa

    boolean foo = Math.abs (a-b) < EPSILON; ``` ``` -// C++: #include & compile with -lm -#define EPSILON 0.000001 +// C++ +#include +static const double EPSILON = 0.000001; bool foo = fabs (a-b) < EPSILON; ``` - A float has 7 decimal places of (printed) accuracy diff --git a/slides/05-trees.html b/slides/05-trees.html index 432fb02ad..c3cda5d1e 100644 --- a/slides/05-trees.html +++ b/slides/05-trees.html @@ -214,7 +214,7 @@

    First child/next sibling

    - In-order: left node first, then self, then right node (C++): ``` void BST::print(BinaryNode *curNode) { - if (curNode != NULL) { + if (curNode != nullptr) { print(curNode->left); cout << curNode->element << endl; print(curNode->right); @@ -228,7 +228,7 @@

    First child/next sibling

    - Post-order: children first, then node (partly C++, partly pseudo-code): ``` int TreeNode::numNodes(TreeNode *tnode) { - if ( tnode == NULL ) + if ( tnode == nullptr ) return 0; else { sum=0; @@ -281,7 +281,7 @@

    Binary Trees

    Binary Trees: diagram details

    -

    In reality, any child not shown is really a NULL pointer, as shown here; but these are generally omitted from the diagrams

    +

    In reality, any child not shown is really a nullptr pointer, as shown here; but these are generally omitted from the diagrams

    bst-4
    bst-2
    @@ -321,7 +321,7 @@

    Binary Trees: diagram details

    - Compare value to be found to key of the root of the tree - If they are equal, then done - If not equal, recurse depending on which half of tree the value should be in if it is in tree - - If you hit a `NULL` pointer, then you have "run off" the bottom of the tree, and the value is not in the tree + - If you hit a `nullptr` pointer, then you have "run off" the bottom of the tree, and the value is not in the tree
    @@ -899,19 +899,10 @@

    Uniform initialization

    - This resolves problems when you are adding lots of C++ qualifiers -
    -
    -
    - diff --git a/slides/12-memory.html b/slides/12-memory.html index aef330ecb..16fd21e9b 100644 --- a/slides/12-memory.html +++ b/slides/12-memory.html @@ -157,7 +157,7 @@

    x86 callee epilogue

    - Returns an untyped pointer (can point to anything) - Returns an address that is the location of at least size bytes of previously unused memory, and reserves that space. - - Returns `NULL` if there isn't enough space. + - Returns `nullptr` if there isn't enough space. - Parameter is the size in bytes