Home

hangman.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string answer = "softwares";
    string attempt = "_________";
    int gc = 5;

    // loop to check inputs
    while(true)
    {
        cout << attempt << endl;
        char guess;
        cin >> guess;
        int pos;
        int startpos = 0;
        while(-1 != (pos = answer.find(guess, startpos)))
        {
            attempt[pos] = guess;
            startpos = pos + 1;
        }
        if(startpos == 0)
        {
            cout << "Did not find that character." << endl;
            if(--gc < 0)
            {
                cout << "You lost!" << endl;
                break;
            }
        }
        if(attempt == answer)
        {
            cout << "You won!" << endl;
            break;
        }
    }

    return 0;
}

euler4.cpp

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool is_palindrome(string);
string to_string(int);
int main(){
	int max = 0;
	for(int i = 999; i > 100; i--){
		for(int j = 999; j > 100; j--){
			string s = to_string(i * j);
			if(is_palindrome(s)){
				if(max < i * j)
					max = i * j;
			}
		}
	}
	cout << "Final: " << max << endl;
	return 0;
}
string to_string(int a){
	stringstream ss;
	ss << a;
	return ss.str();
}
bool is_palindrome(string s){
	bool pal = true;
	for(int i = 0; i < s.length(); i++){
		int j = s.length() - i - 1;
		if(s[i] != s[j]){
			pal = false;
		}
	}
	return pal;
}

arrays.cpp

#include <iostream>
#include <cstdlib>
using namespace std;

void printIntArray(int *arr, int num)
{
	for(int i = 0; i < num; i++)
	{
		cout << *(arr+i) << endl;
	}
}

void printDoubleArray(double *arr, int num)
{
	for(int i = 0; i < num; i++)
	{
		cout << *(arr+i) << endl;
	}
}

double *generateRandomArray(int howMany)
{
	double *vals = new double[howMany];
	for(int i = 0; i < howMany; i++)
	{
		vals[i] = rand();
	}
	return vals;
}

int main()
{
	int vals[3];
	vals[0] = 55;
	vals[1] = 72;
	vals[2] = 100;
	printIntArray(vals, 3);

	srand(10);
	double *randArray = generateRandomArray(10);
	printDoubleArray(randArray, 10);

	/*
	cout << vals << endl;
	cout << vals+1 << endl;

	cout << *vals << endl;
	cout << *(vals+1) << endl;
	*/


	return 0;
}

2darrays.cpp

#include <iostream>
using namespace std;

int main()
{
	int **vals = new int*[3];
	for(int i = 0; i < 3; i++)
	{
		*(vals + i) = new int[4];
		for(int j = 0; j < 4; j++)
		{
			*(*(vals + i) + j) = x;
			x++;
		}
	}

	cout << vals << endl; // should see a pointer
	cout << vals[0] << endl; // should see a different pointer
	cout << vals[1][2] << endl; // should see 6
	cout << *(*(vals + 1) + 2) << endl;

	return 0;
}

pointers.cpp

#include <iostream>
using namespace std;

int *foo()
{
    int *pa = new int;
    *pa = 12;

    return pa;
}

int main()
{
    int n;
    cout << "Enter value for n: ";
    cin >> n;

    int *pn = &n; /// & is pronounced "reference to" or "address of"

    /// ALL POINTERS ARE OF THE SAME BASIC TYPE

    cout << "pn = " << (long)pn << endl;

    (*pn)++;

    cout << "n = " << *pn << endl;

    int **ppn = &pn;
    void *vppn = &pn;
    cout << "ppn = " << ppn << endl;
    cout << "pn = " << pn << endl;
    cout << "*ppn = " << *ppn << endl;

    int *q = pn;
    cout << "q = " << q << endl;
    cout << "*q = " << *q << endl;

    void *v = &n;
    cout << "v = " << v << endl;
    cout << "*v = " << *((double*)v) << endl;

    int w0 = 55;
    int *w1 = &w0;
    int **w2 = &w1;
    int ***w3 = &w2;
    int ****w4 = &w3;
    int *****w5 = &w4;
    cout << "*****w5 = " << *****w5 << endl;

    int *pa = foo();

    cout << "pa = " << pa << endl;
    cout << "*pa = " << *pa << endl;

    delete pa;

    cout << "pa = " << pa << endl;
    cout << "*pa = " << *pa << endl;


    int *u = 0;
    if(u != 0)
    {

    }
}
CSCI 221 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.