Tuesday 19 November 2013

Templates Practice

Hey Guys,
I will strongly suggest you to practice this thing. I thought that I "knew" how to build a template but everything changes when you have to actually sit and do it, those little mistakes will come up and you will be able to fix them before a test comes up (at least that's what I say to myself to be more confident :) ).
The point here is: Have you done the workshop for this subject? (https://scs.senecac.on.ca/~oop344/pages/workshops/w8.html) I found it very useful and hopefully will help you out to clear some thoughts.
Here is the answer built for this scenario and most important: Reviewed by professor!

//Design and code a template named Array for classes that manage one-dimensional, contiguously stored lists of objects of type E. 
//The objects are by default of type double. 

template <class E = double>
class Array{
private:
   //Upon instantiation, an Array object receives the number of type E objects in the array.
   E* data;
   unsigned int arraySize;
   E dummy;
protected:
public:

   /*
   Since the template dynamically allocates space for the specified number of elements,
   your design includes a copy constructor, assignment operator and a destructor.
   */

   //Constructor:
   Array(int);
   //Destructor:
   ~Array();
   //Copy constructor:
   Array(const Array&);
   //Assignment operator:
   Array& operator = (const Array);

   unsigned int size() const;//an inline query that returns the number of elements in the array.
   E& operator[](int i);
   /*- an inline subscript operator that returns a reference to element i, 
   where the first element is at index 0. 
   If the value of i is outside array bounds, your operator returns a reference to 

   a dummy element of type E. */

  //CHALLENGE:
   friend void sort(Array<E>& a); // sorts the elements in an Array object. Receives a references to an Array object and sorts the elements in ascending order

};

template<class E>

Array<E>::Array(int n) : arraySize(n)
{
   data = new E[n];
}

template<class E>
Array<E>::~Array()
{
   delete [] data;
}

//Copy constructor:
template<class E>
Array<E>:: Array(const Array& b) : data(0)
{
    *this = b;
}

//Assignment operator:
template <class E>
Array<E>& Array<E>::operator = (const Array b)
{
   arraySize = b.size();

    if(data)
      delete [] data;

   data = new E[arraySize];
              
   for(int i = 0; i < size; i++)
       data[i] = b.data[i];
}

//an inline query that returns the number of elements in the array.
template <class E>
unsigned int Array<E>::size() const
{
   return arraySize;
}

   /*- an inline subscript operator that returns a reference to element i, 
   where the first element is at index 0. 
   If the value of i is outside array bounds, your operator returns a reference to 

   a dummy element of type E. */
template <class E>
E& Array<E>::operator[](int i)
{
   if( i >=0 && i < arraySize)
   {
      return data[i];
   }
   else
      return dummy;
}

//// sorts the elements in an Array object. Receives a references to an Array object and sorts the elements in ascending order
template <class E>
void sort(Array<E>& a)
{
   for(int i = 0; i < a.size(); i++)
   {
      for(int j = 0; j < a.size() - 1; j++)
      {
         if(a[j] > a[j+1])
         {
            auto t = a[j];
            a[j] = a[j+1];
            a[j+1] = t;
         }
      }

   }
}

Thursday 24 October 2013

Lists on C++

Hey Guys!

I'm tempted to actually call my website Dummies using C++, but it may have been taken already by someone else smarter than me :P. However, all smart people started from scratch and were dummies as well at some point (that's my saying, it keeps me learning new tricks or things from others all the time).

I'm aware that I haven't posted in a while about OOP, many things in my personal life has changed and has "distracted" me of this super intensive subject, but using the time wisely during reading week at school, has allow me to share with you this subject on detail. I read the book twice, then went online to different sites and found this good guy who shows some codes really useful and clear to follow (http://www.youtube.com/user/PaulProgramming?feature=watch).

I have built and compiled the following code with lots of comments so you can easily follow it.

First my list.h:

#ifndef _LIST_H
#define _LIST_H

class List{
private:
   //typedef is a trick to abreviate the work, instead of doing: node *Name ... you simply type: nodeP (That's how I called it )
   typedef struct node
   {
      int data;
      node* next;

   }* nodeP; //after the curly brackets and *, you place the name of how you will call the copy of the struct
  
   nodeP head;
   nodeP curr;
   nodeP temp;

public:
   List();
   void AddNode(int addData);
   void DeleteNode(int delData);
   void PrintList();
};

#endif


THEN my list.cpp

#include "list.h"
#include <cstdlib>
#include <iostream>
using namespace std;

List::List()
{
   head = NULL;
   curr = NULL;
   temp = NULL;
}

void List::AddNode(int addData)
{
   nodeP n = new node; //new node pointer called n, which will be pointing the new created node
   n->next = NULL; //node at the end of the list should point to NULL
   n->data = addData; //filling it with new data
  
   //General case:
   if(head != NULL) //if head is pointing to a subject, we have a list intact
   {
      curr = head; //points to the froint point of the list
      while(curr->next != NULL) //Are we at the end of the list?
      {
         curr = curr->next; //we will point to the following pointer of the list
      }
      curr->next = n; //next element will point to n, N is our new Node pointer
   }
   else //if we dont have a list
   {
      head = n; //n will be the front
   }

}

void List::DeleteNode(int delData)
{
   nodeP delPtr = NULL;
   temp = head;
   curr = head;

    //the data that its on the node that the current node is pointing to is not equal to the data that we are passing
   while(curr != NULL && curr->data != delData)
   {
      temp = curr;
      curr = curr->next;
   }
   if(curr == NULL) //end of the list
   {
      cout << delData << " was not in the list \n" ;
      delete delPtr; //clean our memory
   }
   else
   {
      delPtr = curr;
      //following two lines patches the whole on the list
      curr = curr->next;
      temp->next = curr;
      if(delPtr == head)
      {
         head = head->next;
         temp = NULL;
      }
      //now we remove the node to be deleted
      delete delPtr;
      cout << "The value " << delData << " was deleted\n" ;
   }

}

void List::PrintList()
{
   curr = head;
   while(curr != NULL) //pointing somewhere on the list
   {
      cout << curr->data << endl ;//will output the data of the current element
      curr = curr->next;
   }
}

AND FINALLY my main.cpp to test the code:

#include "list.h"
#include <cstdlib>
using namespace std;

int main (int argc, char** argv)
{
   List Kyno;

   Kyno.AddNode(31);
   Kyno.AddNode(22);
   Kyno.AddNode(15);
  
   Kyno.PrintList();
  
   //Delete a random value
   Kyno.DeleteNode(22);
   Kyno.PrintList();

   //Delete a value that its not on the list
   Kyno.DeleteNode(8);
   Kyno.PrintList();

   //Delete the first value
   Kyno.DeleteNode(31);
   Kyno.PrintList();

   return 0;
}

IF You want to learn about Stack Lists besides the book, take a look at this video, it almost shows the step by step as it is on our book.  http://www.youtube.com/watch?v=KQ8qXi4sLj0

Friday 13 September 2013

Exploring GitHub, IRC and concept of Branching

Good day fellas!

We started our adventure by jumping to three new concepts/systems that will become our daily activity. It may look silly or old fashion to use the IRC program which it was created when I was a teenager (yeap, that gives you a good idea of how old I'm) but the usage of it has been significantly changed during time.

GitHub is an interesting instrument that until now I'm aware of his existence and seems to be a great tool. I tried to practice the 15 minutes warm up, but it made some crashes on my browser, hopefully you guys didn't have to suffer like me and finish the whole mini training. Our professor gave us an intensive course of how to use it and the meanings of each commands... The question is: Will be able to keep on mind all of them? Obviously not (unless you have an amazing memory) but it seems that only while practicing we will be able to capture it. He then jumped us to the concept of branching and we did some examples of how important is to keep notes of in which section you started the branch or section that each of us will be working once we know our teams and project to resolve.

Speaking of practices, Have you started or done yours? I spent some minutes after class with my peers White and DD and some of them seems to be easy to resolve, except for the "last one" which in our case was creating the function that eventually breaks the whole program. So I will be spending some hours during this weekend refreshing my concepts and hopefully be in better shape next week.

Is the same case for you? Wishing you a nice weekend and have fun!

Kyno

Friday 6 September 2013

Bienvenue To OOP344

High expectations exist when someone says that is a student in Computer Programming; it seems like the whole world is on your shoulders and the security of future systems depends of you.

The introduction to this program goes smoothly and building up in each of our minds a logical and creative way of building our solutions to hypothetical cases, a very good approach from veteran professors. However, on this third semester I'm facing the adrenaline and high enthusiasm of a young and potential professor: Joseph Hughes .

I hope to achieve a decent performance during this period by doing lots of practises that later on will allow me to prove my abilities of a great programmer.