Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Battleroid / Stack.cpp
Last active December 14, 2015 11:48
Thing to create a 'stack' class that accepts any kind of type for input using vectors instead of arrays per class exercise. For whatever reason doesn't work in VS2012 but compiles online just fine (using the same compiler, go figure). Whatever.
#include <iostream>
#include "Stack.h"
using namespace std;
int main () {
Stack<int> s1;
Stack<double> s2;
for (int i = 0; i < 5; i++)
@Battleroid
Battleroid / BinarySearch.h
Last active December 14, 2015 11:58
Binary search for any data type using template for C++. Does not return true/false, but instead returns index of element searching for. Not entirely sure if it works, don't care enough to find out.
#ifndef BINARYSEARCH_H
#define BINARYSEARCH_H
template<typename T>
int binarySearch(T list[], T key, int size) {
int low = 0;
int high = size - 1;
while (high >= low) {
int mid = (low + high) / 2;
@Battleroid
Battleroid / Rectangle2D.cpp
Last active December 14, 2015 12:39
Exercise surrounding object 'Rectangle2D'. Created several constructors, functions for finding overlapping parts, containing other items, as well as the area and perimeter. Of course, mutation and retrieval functions are included.
#include "11_09.h"
#include <iostream>
#include <cmath>
using namespace std;
// constructors
Rectangle2D::Rectangle2D() {
setX(0);
setY(0);
setWidth(1);
@Battleroid
Battleroid / Password.cpp
Created March 5, 2013 04:29
Continually asks for password until the correct answer is entered.
#include <iostream>
#include <string>
using namespace std;
int main () {
string passcode = "123";
string guess;
bool correct = false;
#ifndef ANAGRAM_H
#define ANAGRAM_H
#include <string>
#define NO_OF_CHARS 256
using namespace std;
bool isAnagram (const string& a, const string& b) {
int count[NO_OF_CHARS] = {0};
int i;
@Battleroid
Battleroid / CountOccurrences.cpp
Created March 5, 2013 04:35
Count and list occurrences of alphabet letters using a cstring.
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
// can't remember, do I need to include cstring?
void count (string input) {
// Setup arrays
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
@Battleroid
Battleroid / CommonPrefix.cpp
Last active December 14, 2015 12:59
Find common prefix among two strings.
#include <iostream>
#include <cmath>
using namespace std;
template<typename T>
T getDiff(const T a, const T b) {
if (a >= b)
return a;
else if (b > a)
return b;
@Battleroid
Battleroid / pulseSquare.pde
Last active December 14, 2015 14:58
Super dooper pulsating circle object that DOES work. http://youtu.be/bB02W9DUqJE For Processing.
// init
pulseSquare follower = new pulseSquare(160, height/2, 50, 50, 50);
boolean going = true; // false is left, true is right
void setup() {
size(640, 360);
noStroke();
ellipseMode(CENTER);
}
@Battleroid
Battleroid / 13_19.cpp
Created March 18, 2013 02:31
Set of functions to read & write information (in this case a simple sentence) to a file in binary and 'encrypt' it (just adding 5 to each character) and writing it to a new user specified file.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
void write(const string &input, string oname) {
fstream output(oname, ios::out | ios::binary);
if (output.is_open()) {
@Battleroid
Battleroid / 13_13.cpp
Created March 18, 2013 12:36
Creates binary file filled with random integers, appends information if file already exists.
#include <fstream>
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main () {
// create random array
int array[100];