Friday 22 November 2013

Set Bits Function

0 comments
Another challenge was given by Fardad today. We have to create a function to set the bits of an integer using the bit pattern of a character and a given position. Here it is:


#include <iostream>
using namespace std;

void setBits(int& number, char ch, int pos){
  for(int i = pos, j = 1; i < pos + 8; (ch&(1<<(j-1))) ? number |= 1 << i - 1 : number &= ~(1 << (i-1)), i++, j++);
}

int main(){ 
  int value1 = 16384; // 100000000000000   int value2 = 32767; // 111111111111111   int value3 = 16511; // 100000001111111   char a = 0xFF;      // 11111111   char b = 0xAA;      // 10101010   char c = 0x83;      // 10000011   setBits(value1, a, 3);   cout << value1 << endl; // 100001111111100, 17404   setBits(value2, b, 1);   cout << value2 << endl; // 111111110101010, 32682   setBits(value3, c, 3);   cout << value3 << endl; // 100001000001111, 16911   return 0; }

Wednesday 20 November 2013

A Class for Bits

0 comments
For this week, Fardad had us code a class that would handle a string that represented a bit pattern and overload the casting operator to have it represent them as integers and floats (at least that's what I thought he wanted). There is two approaches that I am aware of. First, after copying the bit pattern, we can calculate them manually using our code, or we can be lazy and transfer the bit pattern into a variable's bit pattern literally and have it casted to whatever we want it to represent. I went with the latter.

Saturday 16 November 2013

Integer and Floating Point Numbers to Binary

0 comments
For this week, we are to do something for Fardad's class, I don't remember what (something about binary numbers). Anyways, here is my code for converting integers and floating point numbers to its bitwise representation in memory.

Thursday 14 November 2013

Doubly Linked List

0 comments
Here is my implementation of a doubly linked list. More functions will continue to be added for fun.

https://github.com/wongbsn/Codepad/tree/master/DoublyLinkedList

Tuesday 12 November 2013

More on Templates

0 comments
Today in OOP344 we went over the basics for class templates. For review here is a picture taken in class on the rules of creating a class template:

                                            Fardad's beautiful handwriting 

According to Fardad, we will not be learning more advance template concepts such as template specialization and partial specialization. That is unfortunate to hear as templates are conceptually complex and interesting. From what I've learned so far we can create specialized templates which means we can create a special case scenarios for certain types (instead of a previously coded general template) by doing something like this:

template<>   // no template parameters, compiler looks ahead (general template must exist)
class Node<bool>{ ... }; // matches previously defined template for class Node
                                         // uses this one if type is bool

We can also partially specialize a template like this:

template <typename A, typename B> // General template
class Node{ ... };

template<typename A>     // Partially specialized template
class Node<A, A*>{ ... };    // Explicitly declaring additional parameters

It seems that by providing additional parameters after the class name, we create additional rules for using this template. The compiler will always use the best fit template. For the above example, we still have generic type A, but in order to use this template, we need to pass a type along with a pointer of the same type ie. A<int, int*> a;.

Wednesday 6 November 2013

Fun with Data Manipulation

0 comments
Once again, my friend Adam found a good resource for learning more about C/C++ and computer science. It is a series of YouTube videos of Professor Jerry Cain of Stanford University teaching his CS107 class and it is informative and interesting to watch (almost as entertaining as Fardad's lectures). I really recommend students to watch those videos if they are interested in learning about the concepts that were taught up to now but wanted to know more in depth. In regards to these videos, it taught me how floating point numbers are represented bitwise, which is more complex than integers. The videos did a very nice job explaining this. Also, I really like how Professor Cain manipulates the data to show students how information is read and written in memory. By understanding things at a lower level, it makes the process clearer (at least for me). While teaching about types like structs and arrays, he would show examples of stored data represented in bits and bytes and will do various casts, address incrementation/decrematation, and manipulate their value in such a way that the output can be determined by knowing the size of these types. Here's an example:

int arr[5];                 // sizeof(int) = 4, sizeof(short) = 2
                         
arr[3] = 128;            // 3 * 4 bytes each [  |  |  |  ][  |  |  |  ][  |  |  |  ][A |   |  |  ][  |  |  |  ] write at 12th byte
                                 // A = binary 10000000 = decimal 128
                                        
((short*)arr)[7] = 2; // 7 * 2 bytes each [  |  |  |  ][  |  |  |  ][  |  |  |  ][ A |  B |  ][  |  |  |  ] write at 14th byte
                                 // B = binary 00000010 = decimal 2

cout << arr[3] << endl; // It will read this as an integer: 00000000 00000010 00000000 10000000

// Output is 131200

I should also mention that this example was created on the matrix server and the machines are little endian meaning they store the least significant byte in the smallest address first (so the above would normally be read as [  B |   | A ]. Pretty cool stuff.

https://github.com/wongbsn/Codepad/blob/master/Bitwise%20Operation%20and%20Representation/data1.cpp

Tuesday 5 November 2013

About Casting and Data Representation

0 comments
I've decided to try this blogging business a bit more in hopes that it can help me review and understand concepts a bit better and write down special events that may be worth remembering. Today I believe such an event happened at Seneca. During my OOP344 class, Professor Chris Szalwinski came in to inform us that a course on parallel programming will be available next Fall. Parallel programming is apparently a new and growing thing and the idea of working with multiple cores and with the GPU sounds difficult but exciting. This will be one of the courses I will defiantly consider taking once I get back from my co-op position at RBC. Hopefully, everything will go as planned and I will be able to take that course.

One of the things I enjoy doing at Seneca is learning new concepts with friends and classmates. Today, my friend Adam taught me a bit more about casting. He begins by talking about variables and how they contain a number of information that is important to the compiler such as the identifier, type, address, value, scope, and lifetime. When stored in memory, a variable's type is associated with its identifier, value-set, and value-bit representation. One example he showed us was this:

unsigned int x = 1000;
int y = -1;

if(y > x) std::cout << "y is bigger" << std::endl; // This gets called

What happened was that when comparing a signed with an unsigned, the signed will be implicitly casted as an unsigned, making this -1 into 4.2 billion something. This is understood using modular or clock arithmetic and how numbers are represented in bits. To further explain this, he illustrates the difference between static and reinterpret casting. In a static cast, the value is preserved and the bit pattern is changed whereas in a reinterpret cast, the value is modified and the bit pattern is preserved. What happened is this, -1 is not within the value-set of unsigned integers (0 to 4.2 billion) so the compiler cannot fix the value and modify the bit pattern (static cast) so then it will do a reinterpret cast by looking at the association between the bit patterns and values, and then reads it off as a normal unsigned integer.

            Signed                         Unsigned
<-1, ... ... ... 11111111><4.2B, ... ... ... 11111111>
< 0, ... ... ... 00000000>     <0, ... ... ... 00000000>
< 1, ... ... ... 00000001>     <1, ... ... ... 00000001>


Wednesday 30 October 2013

Introduction to Templates

0 comments
Two weeks ago we learned about templates and this week we are to implement a template for Fardad's dynamic array of integer array class. My understanding is that templates are used to abstract code in such a way that a generic datatype is used to account for the multiple instances of a known receiving datatype used within the class. This would allow the class to operate the same for different datatypes. From our previous lecture we used the Stack to point to nodes that store an integer and coded methods to work with that specific datatype. Using templates we could have created a stack to handle any type of data and still operate similarly.

/* 
   This Array of Int Array now works with Char and
   floating point numbers, but not with external 
   functions associated with strings such as strcpy. 
*/

#include <iostream>
using namespace std;

template <class T> 
class IntArray {
    T* _data;
    unsigned int _size;
  public:
    IntArray(unsigned int size) {
      _data = new T[_size = size];
    }
    T& operator[](unsigned int index) {
      if (index >= _size) {
        size(index < 1024 ? index + 1 : index + 1024);
      }
      return _data[index];
    }
    unsigned int size() {
      return _size;
    }
    void size(unsigned int newsize) {
      T* temp = new T[newsize];
      for (int i = 0; i < _size && i < newsize; i++) {
        temp[i] = _data[i];
      }
      delete[]_data;
      _data = temp;
      _size = newsize;
    }
    T* display(){
      return _data;
    }
    virtual ~IntArray() {
      delete[] _data;
    }
};

template <class T> 
class IntArrArr {
    IntArray<T>** _data;
    unsigned int _size;
    unsigned int _width;
  public:
    IntArrArr(unsigned int size, unsigned int width) {
      _data = new IntArray<T>*[_size = size];
      for (int i = 0; i < _size; i++) {
        _data[i] = (IntArray <T>*)0;
      }
      _width = width;
    }
    T Size() const {
      return _size;
    } 
    IntArray<T>& operator[] (unsigned int index) {
      if (_data[index % _size] == 0) {
        _data[index % _size] = new IntArray<T>(_width);
      }
      return *_data[index % _size];
    }
    virtual ~IntArrArr() {
      for (int i = 0; i < _size; i++) {
        if (_data[i]) {
          delete _data[i];
        }
      }
      delete[] _data;
    }
};

int main()
{
    IntArrArr<int> I(5, 4);   
    IntArrArr<double> D(5,4); 
    IntArrArr<char> C(5, 4);  
    int i;
    int j;
    int x = 0;
    
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        I[i][j] = (i + 1) * (j + 1);
        D[i][j] = (i + 0.5) * (j + 0.5);
      }
    }

    cout << "Array of Int Array:" << endl;
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        cout << I[i][j] << " ";
      }
      cout << endl;
    }

    cout << "Array of Double Array:" << endl;
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        cout << D[i][j] << " ";
      }
      cout << endl;
    }

    cout << "Array of Char Array:" << endl;
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        C[i][j] = 'a' + x++;
      }
    }

    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        cout << C[i][j] << " ";
      }
      cout << endl;
    }


    return 0;
}

Saturday 12 October 2013

Linked Lists - copy constructor, depth(), reverse()

6 comments
What a busy week with tests, assignments, and co-op interviews... Anyway, here is my
solution to the copy constructor, depth(), and reverse() function for Fardad's challenge
of the week. Each week things are getting a little bit more interesting. 


/* 
  copy constructor
  
  1. Create temporary node pointer (for cycling) and copy top 
     node pointer of source
  2. Create temporary node pointer (for cycling) for destination 
     and create new Node with source data (this will be the top node)
  3. Mark down the top node
  4. Cycle through the nodes in a way that makes dest 
     become the pointer for the next new node as the data copies over.    
*/


Stack::Stack(const Stack& S)
{
    Node* src = S._top;
    Node* dest = new Node(src->_data);
    _top = dest;

    while (src->_next != 0) {
      src = src->_next;
      dest->_next = new Node(src->_data);
      dest = dest->_next;
    }
}

----------------------------------------------------------------

/* 
  depth()
  
  1. Create temporary variable to mark the level of depth
  2. Create a temporary node pointer to cycle through the nodes
  3. Cycle through the nodes while marking down the depth
  4. Return the depth level   
*/


unsigned int Stack::depth()
{
    unsigned int lvl = 0;
    Node* top = _top;

    while (top) {
      lvl++;
      top = top->_next;
    }
    return lvl;
}

----------------------------------------------------------------

/* 
  reverse()
  
  1. Create temporary node pointer to a new node and pop the 
     first node while retrieving data (this is your new bottom node)
  2. Cycle through all the nodes, popping all the previous nodes 
     and creating new nodes with their data and using a pointer
     to the current instance and immediately assigning it to a
     new instance(this will reverse the order and destroy the 
     previous nodes)
  3. Copy the pointer to the top of your new reversed list
*/


void Stack::reverse()
{
    Node* temp = new Node(pop());

    while (!isEmpty()) {
      temp = new Node(pop(), temp);
    }
    _top = temp;
}

Sunday 22 September 2013

OOP344's Basic Math

0 comments
This is my solution to OOP344's "basicMath" problem. User is suppose to call the program from 
command line with a simple mathematical expression (i.e  bm  a + b). 
**Fixed some errors regarding validation**


/*
   Program: basicMath.cpp
   Date: September 25, 2013
   Author: Benson Wong
   Purpose: To practice coding for command line arguments
*/

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
bool NaN(char* x);

int main(int argc, char** argv)
{
    double a;
    double b;
    a = b = 0;
    bool error = false;

    if (argc == 4) {
         if (NaN(argv[1]) || NaN(argv[3])) {
             error = true;
         } else {
             a = atof(argv[1]);
             b = atof(argv[3]);
             if (!strcmp(argv[2], "+")) {
                  cout << a << " + " << b << " = " << a + b << endl;
             } else if (!strcmp(argv[2], "-")) {
                  cout << a << " - " << b << " = " << a - b << endl;
             } else if (!strcmp(argv[2], "x")) {
                  cout << a << " x " << b << " = " << a * b << endl;
             } else if (!strcmp(argv[2], "/") && argv[3] != 0) {
                  cout << a << " / " << b << " = " << a / b << endl;
             } else
                  error = true;
         }
    } else
         error = true;

    if (error) {
         cout << "bm <number> <+-x/> <number><ENTER>" << endl;
    }

    return 0;
}

bool NaN(char *x)
{
    int i;
    int d;
    bool ec = false;
    i = d = 0;

    if (x[0] == '-') {
         i++;
    }
    for (; x[i] != '\0' && ec == false; i++) {
         if (x[i] == '.') {
             d++;
             if (d > 1) {
                  ec = true;
             }
         } else if (!isdigit(x[i])) {
             ec = true;
         }
    }

    return ec;
}