Tuesday, 23 October 2012

Int *info = new int [10^6];

Currently it seems that the second assignment is not going to be too complicated, as it is similar to the first hash table assignment, though it’s too early to judge, as I have not yet started to work with templates, and it is known that mystical things may appear when porting your application to support templates.

A quite useful thing that we learnt last week is ‘Templates’, which gives the ability to re-use code, by allowing to pass various data types that aren’t pre-defined. This can replace function overloading. As an example I could mention a function that saves everything passed to it to a file. In this case you could use function overloading or, you could just use templates and have only one function. This principle is the same for classes when trying to implement containers – a class which works with multiple data types, having ‘fields’ with no specific data types specified. An example could be a class which compares, multiplies and divides two fields. To use this class with doubles we would need separate fields for each data type, also function overloading would be necessary. 

Another useful thing that we learnt is pointers, which are just a variable that contains a memory location to a different variable. Pointers can be used in several different ways. I usually use pointers when using a method it is needed to modify an array which is a field, as it is just the easiest way  an array from a method, as returning the pointer of the array is the easiest way to return this data structure. Knowing that a pointer is just an address in the memory we can increment the pointer to get the next element in the memory which may be useful as values that are stored in an array occupy consecutive memory locations. One of the most useful operation that you can do with pointers, in my mind, is passing them to a function. You would use this when there would not be a need to create a new copy of the objects passed to a function, for example a very large array. You also can use pointer to an array to be able to change its size in the constructor, when it is defined as a field. You might use an array of pointers to store pointers to char values that form a string.

Also besides pointers we were taught about references, which basically is just creating another name to a variable, just an alias for a variable. Although during the lectures it was said that pointers and references are similar, I disagree as the differences outweighs similarities. If I would need to decide between using an reference or a pointer I would probably decide to use a pointer as it has a wider range of options. Opposite to references, pointers can be declared without initialization and they can be reassigned, so they point to a different memory address. Either it is my exhausted brain or I really cannot think of an example right now where using a reference would be better than using a pointer.
Regarding the data structure topics that we discussed this week I did not find anything new about the big O notation, as it is just a bit of extensions of primary school knowledge. The big O notation is a way of characterizing a function that represents space / time complexities to a general solution by removing constants & smallest functions, by just taking the dominant function, so it would be possible to compare multiple complexities from different algorithms. 

Comparing the complexities of arrays, vectors, chained hash tables and open addressed hash tables I have to say that in the big O notation they all are the same though each of them might take up more / less time as the big O notation states only the dominating function, so there is no chance to see small differences between these complexities if using big O notation, which would not be good in cases when you have to work on a project that has a small memory or which needs to run for a very precise time.

Of course AC12007 first week contents are similar to the big O notation topic that we had on Thursday, as it is what we learned there.
Although each week is different and it is hard to predict what will happen, I assume that we will finish off C++, get some extra knowledge on things that we might use in our assignment and slowly switch to C.

Sorry for the weird structure of sentences and paragraphs, as I was not able to go through the whole text again.

1 comment:

  1. A very good chatty style. You cover lots of points naturally without it reading like a textbook. You have put a lot of effort in to this.

    I wonder if you still think the assignments are the same now you have started it? Both templates and exception catching can be tricky when you first try them, although I presume you have done so before?

    Templates are one of my favourite C++ concepts. They are just so useful. A lot of the methods that I find myself reusing, like a method to print all container types. You seem to have a good understanding of this

    Your overview of pointers is very complete. I am impressed. I think most people think there is a relationship between pointers and references. Popular wisdom seems to agree with me as there is a wikipedia article on the subject: http://en.wikipedia.org/wiki/Reference_(C%2B%2B)#Relationship_to_pointers

    I am glad that what you have learned in AC21007 is useful in this module. We have tried to make this semester of 2nd year as linked up as possible.

    ReplyDelete