This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.2 */ | |
// determine if someone has won a game of tic-tac-toe | |
// (expand to a full game!) | |
#include <iostream> | |
using namespace std; | |
// assume only one row satisfying the condition exists since the games ends once a player succeeded | |
// in placing three respective marks in a horizontal, vertical or diagonal row. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.1 */ | |
// swap a number in place | |
#include <iostream> | |
void swapInPlace(int &a, int &b) | |
{ | |
a = a ^ b; | |
b = a ^ b; | |
a = a ^ b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 13.10 */ | |
// my2DAlloc(int rows, int cols) | |
#include <iostream> | |
using namespace std; | |
int **my2DAlloc(int rows, int cols) | |
{ | |
int header = rows * sizeof(int*); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 13.9 */ | |
// aligned malloc and free function | |
// (book solution) | |
#include <iostream> | |
void *aligned_malloc(size_t required_bytes, size_t alignment) | |
{ | |
void *p1; // original block | |
void **p2; // aligned block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 13.8 */ | |
// smart pointer class | |
// Template implementation! (book solution) | |
template <class T> class SmartPointer { | |
public: | |
// constructors | |
SmartPointer(T *ptr) { | |
ref = ptr; | |
refCount = (unsigned*)malloc(sizeof(unsigned)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 13.7 */ | |
// copy Node | |
#include <map> | |
using namespace std; | |
struct Node{ | |
Node *left, *right; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 13.1 */ | |
// print the last K lines of an input file | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
void printLastLines(string filename, int K) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 11.8 */ | |
// track(int x), getRank(int x) | |
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <sstream> | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 11.7 */ | |
// compute the largest possible people tower, givin (ht, wt) | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Person { | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 11.6 */ | |
// find an element in a M-by-N matrix in which each row and column is sorted in ascending order | |
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <sstream> | |
using namespace std; |