Thursday 8 November 2012

Nov 8th OOP344 challenge

This is my version of the three functions

void prnBits(unsigned int Val){
  for (unsigned int i=(1<<(sizeof(Val)*8-1));i;printf("%d", !!(Val & i)),i>>=1); // in one line
/* original function
  unsigned int m = 1 << sizeof(Val) * 8 -1;
  while(m){
    printf("%d", !!(Val & m));
    m = m >> 1;
  }
*/
}

char* bits(unsigned int Val){
  unsigned int m = 1 << sizeof(Val) * 8 -1;
  char* ret=new char[sizeof(Val)*8+1];
  int i=0;
  while(m){
    ret[i]=!!(Val&m)+48;
    m>>=1;
    i++;
  }
  ret[i]=0;
  return ret;
}
// write the above function so it works like this:
// cout<<bits(2345)<<endl;

void bitDump(void* memAddress, unsigned int memSize){
  int i;
  int* a=(int*)memAddress;
  for (i=0;i<memSize;i++){
    prnBits(a[i]);
  }
}

Thursday 1 November 2012

OOP344 Queue class functions

I would write the two functions as follows:
Again, the header file Fardad has provided needs to be modified so that it returns by reference.

int& Queue::operator[](unsigned int index){
  Node* tempNode =_head;
  int i;
  for (i=0; i<index%size();i++){
    tempNode = tempNode->_next;
  }
  return tempNode->_data;
}

unsigned int Queue::size(){
  Node* tempNode =_head;
  unsigned int count = 0;
  while (tempNode){
    tempNode = tempNode->_next;
    count++;
  }
  return count;
}

Revised version of Amir and Saeid's code - OOP344

Changed Fardad's function signature so that returned data is a reference to the original, thus able to be modified

//queue.h
int& operator[](unsigned int index);


Simplified version of Amir and Saeid's operator function, removed extra variables and LOC. Their other functions remain the same.

//queue.cpp
int& Queue::operator[](unsigned int index){
  Node* Ind = _head;
  int i;
  for (i = 0; i < index % _size; i++){
    Ind = Ind-> _next;
  }
  return Ind-> _data;
}