Wednesday, August 4, 2010

topics for final

loops (nested)
arrays (1d, 2d)
functions (pass by ref, by val, arrays)
recursion (tracing recursion)
sorting

Tuesday, August 3, 2010

// dfgdfdfg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;

bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
    return ! aComposite[n];
}

void setupLookupTable()
{
    for (int i = 2; i < 1024; i++)
    {
        // is this number i am visiting
        // already marked off as composite
        if (aComposite[i] == false)
            for (int j=i+i; j < 1024; j+=i)
                aComposite[j] = true;
    }

}

void print(const int a[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        cout << a[i] << ' ';
    cout << endl;
}

void add(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] + b[i];
}

void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            c[i][j] = a[i][j] + b[i][j];
}

void print(const int C[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << "[" << setw(3) << C[i][j] << "]";
        cout << endl;
    }
}

//template <int rows, int cols>
//int dotProduct(int (&A)[rows][cols],
//               int (&B)[rows][cols],
//               const int row, const int col)
//{
//    int total = 0;
//    for (int i = 0; i < 3; i++)
//        total += A[row][i] * B[i][col];
//    return total;
//}

//template <int rows, int cols>
//void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
//{
//    for (int i = 0; i < rows; i++)
//        for (int j = 0; j < cols; j++)
//            C[i][j] = dotProduct(A, B, i, j);           
//}

int minimumValue(const int a[], const int capacity)
{
    int minValue = a[0];
    for (int i = 1; i < capacity; i++)
        if (a[i] < minValue)   
            minValue = a[i];
    return minValue;
}

int minimumPosition(const int a[], const int startPos, const int capacity)
{
    int minPos = startPos;       
    for (int i = startPos + 1; i < capacity; i++)       
        if (a[i] < a[minPos])   
            minPos = i;
    return minPos;       
}

void selectionSort(int A[], int capacity)
{
    for (int i = 0; i < capacity; i++)
    {
        int minPos = minimumPosition(A, i, capacity);
        swap(A[i], A[minPos]);
    }
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] - b[i];
}

void mult(const int a[], int scalar, int b[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        b[i] = a[i] * scalar;
}
int foo(int *a)
{
    return 0;
}

template <typename T, typename S>
void swap2(T &a, S &b)
{
    T temp = a;
    a = b;
    b = temp;
}


void print(char s[])
{
        for (int i = 0; s[i]; i++)
            cout << s[i];
}

void print2(char *s)
{
        for (; *s; s++)
            cout << *s;
}

int main() {
    char name[10] = "hello";
    char fred[10] = "hello";
strcmp(name, fred)
    if (name < fred)
    if (strcmp(name, fred) < 0)

if (name > fred)
if (strcmp(name, fred) > 0)

if(name == fred)
if (strcmp(name, fred) == 0)

fred = name;
strcpy(fred, name);

    for (int i = 0; name[i] != 0; i++)
        cout << name[i];
    cout << endl;
    print2(name);
    cout << endl;
    fred = name;
    if (name == fred)
    {
    }
    string b = "hi ";
    b += "there";
    //int A[3][3], B[4][3];
    //dotProduct(A, B, 2, 1);

//    int first = 2;
//    float second = 3;
//    swap2<int, int>(first, second);
//    cout << first << " " << second << endl;
//    int v[10];
//    foo(v);
//    foo(&v[0]);
//    cout << v << endl;
//    cout << &v[0] << endl;
//
//
//
//    //setupLookupTable();
//    //for(int k = 0; k < 1024; k++)
//    //    if (isPrime(k))
//    //        cout << k << endl;
//    //string s, t;
//    //int u;
//
//    //ifstream jin;
//    //jin.open("myfirstinput.txt");
//    //jin >> s;
//    //jin >> t;
//    //cout << s << t << endl;
//    //jin.close();
//
//    //ofstream jout;
//    //jout.open("c:/josh/myfirstoutput.txt");
//    //for(int i = 0; i < 10; i++)
//    //    jout << i << " ";
//    //jout << endl;
//    //jout.close();
//    //system("PAUSE");
//
//   int A[3][3] =
//   {
//       { 4, 6, 0},
//       { 6, 0, 1},
//       { -1,3, 5}
//   };
//   int B[3][3] =
//   {
//       { 0, 6, 0},
//       { 1, 0, 1},
//       { 1, 0, 5}
//   };      
//   int C[3][3];
//   cout << "DP: " << dotProduct(A, B, 0, 1) << endl;
//    add(A, B, C, 3, 3);
//    print(C, 3, 3);
//
//cout << endl;
//cout << endl;
//cout << endl;
//
//   int x[5] = {3,4,5,2,3};
//   int y[5] = {1,1,0,2,3};
//   int z[5];
//   int a[5];
//  
//    // a = x * 6
//   add(x, y, z, 5);
//
//   print(z, 5);
//
//   mult(y, 6, z, 5);
//   print(z, 5);
//  
//   return 0;
}

// iterative approach
void insert(int a[], int capacity)
{
    // yank out the element
    int value = a[capacity-1];

    // shift items over
    for (int j = capacity - 2; j >= 0 && a[j] > value; j--)
        a[j+1] = a[j];
   
    a[j+1] = value;
}

void insertionSort(int a[], int capacity)
{
    for (int i = 1; i <= capacity; i++)
        insert(a, i);

}


to sort an array of size n,
first: sort the array of size n -1
insert element n into that sorted partial result of array n-1

// using recursion
void insertionSort(int a[], int capacity)
{
    if (capacity == 1)
        return;
    insertionSort(a, capacity - 1);
    insert(a, capacity);
}


















// strings
c-strings
C++ strings

char name[10] = "hello";
for (int i = 0; name[i] != 0; i++)
    cout << name[i];

Monday, August 2, 2010

int A[4];

A[0] at 100
A[1] at 104
A[2] at 108
A[3] at 112


A[10]
*(A + 10)
memory location 100 + (10 * sizeof int)
void foo(int p[])
{
    for (int i = 0; i < 4; i++)
        cout << *(p+i);
}

int * bar(int p[])
{
    int B[5] = {5, 4, 3, 1, 6};
    B[3] = 7;
    return p;
}

main()
{
int A[4];
for (int i = 0; i < 4; i++)
    cout << A[i] << " ";

for (int i = 0; i < 4; i++)
    cout << *(A+i) << " ";
cout << endl;

foo(A);
int *q = bar(A);
}

template <typename T>
void swap2(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

// recursive bubblesort
void bubbleSort(int a[], int capacity)
{
    if (capacity == 1)
        return;
    // do one sweep
    for(int i = 0; i < capacity; i++)   
        if (a[i] > a[i+1])   
             swap(a[i], a[i+1])   

    bubbleSort(a, capacity - 1);
}











// dfgdfdfg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;

bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
    return ! aComposite[n];
}

void setupLookupTable()
{
    for (int i = 2; i < 1024; i++)
    {
        // is this number i am visiting
        // already marked off as composite
        if (aComposite[i] == false)
            for (int j=i+i; j < 1024; j+=i)
                aComposite[j] = true;
    }

}

void print(const int a[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        cout << a[i] << ' ';
    cout << endl;
}

void add(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] + b[i];
}

void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            c[i][j] = a[i][j] + b[i][j];
}

void print(const int C[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << "[" << setw(3) << C[i][j] << "]";
        cout << endl;
    }
}

template <int rows, int cols>
int dotProduct(int (&A)[rows][cols],
               int (&B)[rows][cols],
               const int row, const int col)
{
    int total = 0;
    for (int i = 0; i < 3; i++)
        total += A[row][i] * B[i][col];
    return total;
}

template <int rows, int cols>
void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            C[i][j] = dotProduct(A, B, i, j);           
}

int minimumValue(const int a[], const int capacity)
{
    int minValue = a[0];
    for (int i = 1; i < capacity; i++)
        if (a[i] < minValue)   
            minValue = a[i];
    return minValue;
}

int minimumPosition(const int a[], const int startPos, const int capacity)
{
    int minPos = startPos;       
    for (int i = startPos + 1; i < capacity; i++)       
        if (a[i] < a[minPos])   
            minPos = i;
    return minPos;       
}

int selectionSort(int A[], int capacity)
{
    for (int i = 0; i < capacity; i++)
    {
        int minPos = minimumPosition(A, i, capacity);
        swap(A[i], A[minPos];
    }
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] - b[i];
}

void mult(const int a[], int scalar, int b[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        b[i] = a[i] * scalar;
}
int foo(int *a)
{
    return 0;
}

template <typename T, typename S>
void swap2(T &a, S &b)
{
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int A[3][3], B[4][3];
    dotProduct(A, B, 2, 1);

//    int first = 2;
//    float second = 3;
//    swap2<int, int>(first, second);
//    cout << first << " " << second << endl;
//    int v[10];
//    foo(v);
//    foo(&v[0]);
//    cout << v << endl;
//    cout << &v[0] << endl;
//
//
//
//    //setupLookupTable();
//    //for(int k = 0; k < 1024; k++)
//    //    if (isPrime(k))
//    //        cout << k << endl;
//    //string s, t;
//    //int u;
//
//    //ifstream jin;
//    //jin.open("myfirstinput.txt");
//    //jin >> s;
//    //jin >> t;
//    //cout << s << t << endl;
//    //jin.close();
//
//    //ofstream jout;
//    //jout.open("c:/josh/myfirstoutput.txt");
//    //for(int i = 0; i < 10; i++)
//    //    jout << i << " ";
//    //jout << endl;
//    //jout.close();
//    //system("PAUSE");
//
//   int A[3][3] =
//   {
//       { 4, 6, 0},
//       { 6, 0, 1},
//       { -1,3, 5}
//   };
//   int B[3][3] =
//   {
//       { 0, 6, 0},
//       { 1, 0, 1},
//       { 1, 0, 5}
//   };      
//   int C[3][3];
//   cout << "DP: " << dotProduct(A, B, 0, 1) << endl;
//    add(A, B, C, 3, 3);
//    print(C, 3, 3);
//
//cout << endl;
//cout << endl;
//cout << endl;
//
//   int x[5] = {3,4,5,2,3};
//   int y[5] = {1,1,0,2,3};
//   int z[5];
//   int a[5];
//  
//    // a = x * 6
//   add(x, y, z, 5);
//
//   print(z, 5);
//
//   mult(y, 6, z, 5);
//   print(z, 5);
//  
//   return 0;
}

Thursday, July 29, 2010

for (int i = 0; i < n - 1; i++)
    for (int j = 0; j < n - i - 1; j++)
        if (a[j] > a[j+1])
            swap(a[j], a[j+1]);
   
// dfgdfdfg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;

bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
    return ! aComposite[n];
}

void setupLookupTable()
{
    for (int i = 2; i < 1024; i++)
    {
        // is this number i am visiting
        // already marked off as composite
        if (aComposite[i] == false)
            for (int j=i+i; j < 1024; j+=i)
                aComposite[j] = true;
    }

}

void print(const int a[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        cout << a[i] << ' ';
    cout << endl;
}

void add(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] + b[i];
}

void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            c[i][j] = a[i][j] + b[i][j];
}

void print(const int C[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << "[" << setw(3) << C[i][j] << "]";
        cout << endl;
    }
}

int dotProduct(const int A[3][3], const int B[3][3], const int row, const int col)
{
    int total = 0;
    for (int i = 0; i < 3; i++)
        total += A[row][i] * B[i][col];
    return total;
}

void mmult(const int A[3][3], const int B[3][3], int C[3][3], const int rows, const int cols)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            C[i][j] = dotProduct(A, B, i, j);           
}

int minimumValue(const int a[], const int capacity)
{
    int minValue = a[0];
    for (int i = 1; i < capacity; i++)
        if (a[i] < minValue)   
            minValue = a[i];
    return minValue;
}

int minimumPosition(const int a[], const int startPos, const int capacity)
{
    int minPos = startPos;       
    for (int i = startPos + 1; i < capacity; i++)       
        if (a[i] < a[minPos])   
            minPos = i;
    return minPos;       
}

void subtract(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] - b[i];
}

void mult(const int a[], int scalar, int b[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        b[i] = a[i] * scalar;
}
int foo(int *a)
{

}
int main() {
    int v[10];
    foo(v);
    foo(&v[0]);
    cout << v << endl;
    cout << &v[0] << endl;

    v[i]
    *(v+i)

    //setupLookupTable();
    //for(int k = 0; k < 1024; k++)
    //    if (isPrime(k))
    //        cout << k << endl;
    //string s, t;
    //int u;

    //ifstream jin;
    //jin.open("myfirstinput.txt");
    //jin >> s;
    //jin >> t;
    //cout << s << t << endl;
    //jin.close();

    //ofstream jout;
    //jout.open("c:/josh/myfirstoutput.txt");
    //for(int i = 0; i < 10; i++)
    //    jout << i << " ";
    //jout << endl;
    //jout.close();
    //system("PAUSE");

   int A[3][3] =
   {
       { 4, 6, 0},
       { 6, 0, 1},
       { -1,3, 5}
   };
   int B[3][3] =
   {
       { 0, 6, 0},
       { 1, 0, 1},
       { 1, 0, 5}
   };      
   int C[3][3];
   cout << "DP: " << dotProduct(A, B, 0, 1) << endl;
    add(A, B, C, 3, 3);
    print(C, 3, 3);

cout << endl;
cout << endl;
cout << endl;

   int x[5] = {3,4,5,2,3};
   int y[5] = {1,1,0,2,3};
   int z[5];
   int a[5];
  
    // a = x * 6
   add(x, y, z, 5);

   print(z, 5);

   mult(y, 6, z, 5);
   print(z, 5);
  
   return 0;
}

Wednesday, July 28, 2010

// dfgdfdfg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;

bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
    return ! aComposite[n];
}

void setupLookupTable()
{
    for (int i = 2; i < 1024; i++)
    {
        // is this number i am visiting
        // already marked off as composite
        if (aComposite[i] == false)
            for (int j=i+i; j < 1024; j+=i)
                aComposite[j] = true;
    }

}

void print(const int a[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        cout << a[i] << ' ';
    cout << endl;
}

void add(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] + b[i];
}

void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            c[i][j] = a[i][j] + b[i][j];
}

void print(const int C[][3], int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << "[" << setw(3) << C[i][j] << "]";
        cout << endl;
    }
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        c[i] = a[i] - b[i];
}

void mult(const int a[], int scalar, int b[], int capacity)
{
    for (int i = 0; i < capacity; i++)
        b[i] = a[i] * scalar;
}

int main() {
    //setupLookupTable();
    //for(int k = 0; k < 1024; k++)
    //    if (isPrime(k))
    //        cout << k << endl;
    //string s, t;
    //int u;

    //ifstream jin;
    //jin.open("myfirstinput.txt");
    //jin >> s;
    //jin >> t;
    //cout << s << t << endl;
    //jin.close();

    //ofstream jout;
    //jout.open("c:/josh/myfirstoutput.txt");
    //for(int i = 0; i < 10; i++)
    //    jout << i << " ";
    //jout << endl;
    //jout.close();
    //system("PAUSE");

   int A[3][3] =
   {
       { 4, 6, 0},
       { 6, 0, 1},
       { -1,3, 5}
   };
   int B[3][3] =
   {
       { 0, 6, 0},
       { 1, 0, 1},
       { 1, 0, 5}
   };       ;
   int C[3][3];

    add(A, B, C, 3, 3);
    print(C, 3, 3);

    x = 6, y = 9, i++, p++;

cout << endl;
cout << endl;
cout << endl;

   int x[5] = {3,4,5,2,3};
   int y[5] = {1,1,0,2,3};
   int z[5];
   int a[5];
  
    // a = x * 6
   add(x, y, z, 5);

   print(z, 5);

   mult(y, 6, z, 5);
   print(z, 5);
  
   return 0;
}
// add two matrices
int A[3][3];
int B[3][3];
int C[3][3];
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
        cout << C[i][j] << " ";

int i =0;
++i

this means: (i = i+1, new value of i)

i++
this means: (temp = i, i = i+ 1, temp)

while(i = 0, i < 10, ++i)
    cout << i << " ";


void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

main()
{
    int x = 7;
    int y = 8;
    swap(&x, &y);
    cout << x << " " << y << endl;
}