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.

Thursday, 18 October 2012

So, we have had our first assignment in C++, we had to implement a chained hash table using all the tools we had learned so far. I opted for a Vector of Vectors in my implementation, why I didn't use an Array of Vectors is beyond me, but it made sense at the time! I found it quite easy writing the functions to add, search and print the contents of the hash table, however, the nightmare arose when I got to my delete method, I'm sure each of us had a different solution to what sounded like an easy function! Firstly I had to start thinking in Vectors, which vector do I delete from, which one is my method ACTUALLY deleting from and what do I do to fix it! In the end I opted for the erase() function that is in-built in the vector library, after many "program aborted : core dumped " errors I worked out that my function was trying to delete from a value that was outside my vector, I.e. I was trying to erase a vital piece of memory from some other poor program that was minding its own business! After sorting that issue, the crashing stopped and I thought my method was working as it should, however after adding a value, checking it was there, deleting it, then re-checking my program kept saying that the value was still in the table, oh noes! I spent a ridiculously long time trying to work out what was going on, it was only after several coffees and visits to the smoking area that I had the brainwave, what if the delete WAS working at my search was broken?? Indeed, this was the case, I had defined the boolean it uses in a global scope, so it never got reset to false, ever, what a tube! I'm not sure if any of the rest of the group had a problem like this, but it was a bit of a pain to de-bug! This assignment brought into sharp focus the concept of Errors, my code compiled 90% of the time, once I sorted out my missing semi-colons etc (Syntax errors), but it also crashed at least 50% of the time after compilation, these were down to my delete method trying to delete from locations that it shouldn't (Exception Errors). I also had some bizzare functionality that I hadn't planned for, my first implementation of my user input validation appeared to work, but it turned out it only worked for the first incorrect entry and not for the subsequent inputs (Logical Error). I suppose I could have implemented some Try-Catch blocks around the high risk areas of my program, such as adding or deleting from the hash table, so if by some chance a dodgy value did get through, the program would stop and display a meaningful error, rather than half a terminal of gobbledegook! I only ever used Try-Catch when I was dealing with files in JAVA, but from what I have seen in C++ it is pretty similar. Have to remember to #include stdexcept at the top though, if I remember correctly JAVA didn't need to import a separate library for exception handling. There are 6 different types of errors that can be generated by stdexcept, 2 of which have a further 7 different exceptions, as shown below:





So we see that std:exception is the parent class, with sub-classes of it thrown depending on the type of error that is encountered. I spent a lot of time lurking in the logic_failure -> out_of_range section, not sure about the rest of the guys!
Going back to the files in JAVA, I find it far easier to implement in C++, there was just something really awkward about creating a file reader or writer object, then creating a new reader/writer then you could actually work with your file, it was messy and I always ended up overwriting when I should have been appending and vice versa. the C++ implementation is neat, tidy and a breath of fresh air from JAVA. You import the fstream library then call it on what ever file you wish to open. Depending on how you call the fstream object you can perform different tasks as shown below:

• ios::out - opens to write output.
• ios::in - opens to read input.
• ios::app - opens to append output at the end of an existing file.
• ios::trunc - truncate the existing file.
• ios::ate - open without truncating, data can be written anywhere.
• ios::binary - treat the file as binary format rather than text, so the data may be stored in a nontext format.


This also brings us nicely to the concept of Object Orientated Programming, in JAVA, it was forced, like I mentioned about the file reading. A lot of things in JAVA seemed very clunky, but once you got your head around the fact that everything had to work in classes it kind of made sense. Now that we have been working in C++ I can see why we were shown JAVA first. If we had done it in reverse, I think that classes and OOP would have been far more difficult to grasp, with lots of bad habits getting in our way. Its now well and truly burned in our minds : Encapsulation, Inheritance and Polymorphism. With inheritance in C++ you can create a base class with its own constructor, then you can make a new class that inherits all of the methods from the base class. For example, you could have a base class called Animal, then create sub classes for dog and cat. The base class Animal would have a constructor for say age, weight and colour which would be inherited by dog and cat, but the dog could have new parameter, such as bark and the cat could have another such as temperament (will it scratch your eyeballs out with no warning?). The classes for this could look as such : Class Animal then you would have Class Dog : Public Animal and Class Cat : Public Animal. This is pretty similar to JAVA, you just had the word EXTENDS instead of the ":" giving Public Class Dog EXTENDS Animal for example. Inheritance in C++ also introduced us to the concept of "overloading" Here we could have a function in a class that had the same name such as bark(), but, you could have ANOTHER function, with the same name, but with different parameters such as bark(parameter 1) and bark(parameter 1, parameter 2). This was an awesome concept when it was shown, I personally had never heard of overloading before so this was quite interesting to look up. Overriding was also introduced at this point, similar to Overloading but instead of having different arguments for each type, you have an IDENTICAL function in the sub class, same name, parameters, arguments etc. So when this function is called it completely ignores the function from the base class and uses the function from the sub class instead. 

All in all it has been a VERY busy week, we had our first assignment to hand in for C++ and we also had our first assignment to hand in for Algorithms and AI, that was a nasty nasty assignment and I, and may others, felt very under prepared to answer it, especially since it is worth 10% of our module grade, I will be happy if I even passed it, nevermind the distinctions which seemed so easy to achieve in first year. However, I can see some nice overlapping coming up, we have covered Space Complexity, Time Complexity and Big O Notation in AC12007 already, so, hopefully this module will solidify what we have learned(or tried to) already.

Apologies for this being uploaded slightly late.

Duncan

Friday, 5 October 2012

Thinking back on last weeks sudoku solver, a headache arises from the depths of my guts, as it was a painful act to start programming in a language that "you sort of know", but you actually don't. Its so similar to Java, and yet, its quite different. We all programmed the sudoku solver, and yet we all bs'd our way through it in our own individual styles. The functionality is the same naturally, but the attempts of breaking c++ were all quite different. Part one did not vary too much, as we all based our work on the file input of the game of life example. Part two however, varied.
There were brute force attempts and inefficient brute force attempts, to which I must count my own code.
There was the efficient Jekabs-Duncan style, and the lets-see-who-breaks-first Ron style. Rihards was somewhere in between. The naming of the code style should hopefully give away the robustness of the codes. If mine was a house, it would have followed the chaos theory and collapsed at the mere sight of a butterfly. It was quite instabil, and only seldom "wanted" to solve the sudoku, and only if no butterfly's were present.
The problem causation is my lack of planning and the consequently bad structure which leads to something that's overly complicated and can neither be fixed nor read - ever again. It all makes sense once I see someone else's code, but up until then, my brain is convinced that that's the way to go.
Now that I look back, I would structure the program much differently, or so to say, I would give it a structure at all. It would take me a quarter of the time and might actually work.

This past week, we looked at strings, vectors and hash tables.
The strings were surprisingly the same as in Java. I, as a programmer, never used too many functions from the String class but always the same ones. That's why I cant too precisely say what the similarities are between the Java functions and the c++ functions, but what I can say about the ones I use, they are all present and syntactically the same or very similar.
I think the "find_first_of" and so on, methods quite handy, and should I ever have to program a search for words or text in a text, I will know instantly which functions to take. I don't know who came up with those, but I like the geeky flair that lies around them, such as saying: take us, we are commands with one purpose, you know you want to.
The String stream library is a function that is bound to be useful one day. As soon as any file or input will ever be processed, that will come in handy and be irreplaceable.
This week we also talked about classes and objects in c++, and of course their relation to classes and objects in object oriented Java. To this, I can only say that, in Java we got introduced to classes very well, and getting the concept of classes and objects in c++ was an automated process. The only difference is that c++ needs to know of the existence of a method or class before its called the first time. In fact, now that I look at Java again, I wonder why the Java compiler copes with that at all. It makes sense to "tell" the program what it has in store, and I have no difficulty getting my head around that concept.
The next big thing we handled this week was the hash table. While I was aware that hash tables existed, until now, I never took care to learn how they work. The concept of the hash table is quite easy and I think its a handy way to organise data along a self chosen scope. Getting the key function right could probably become a pain at times, but in general this seems like something I might use one day when there is a lot of data to organise into some filing system. When I say that I might use it one day, I admit that the whole time, I was speaking of a linked or chained hash table. I don't actually get the purpose of a simple hash table, also because I think it should be called a hash array. I'm excited to start programming on the task for this course, which is to create a chained hash table. Maybe my scum-bag-brain will even have the courtesy of allowing me to plan the program before starting to fill the screen with bs. Enough of the cursing now, on to the next topic:
We also looked at vectors. Now that's something I can do in my sleep, if I didn't kill dreams like that instantly. Vectors are arrays, the initialisation is a bit simpler as in Java, as you don't have to actually create an object of the class array first, but otherwise its the same thing. I would always rather use an array than a chained hash table if I don't have loads of data, but it depends on the situation and especially the potential of growth that will help make the decision. The point is, if you don't have loads of data, or the data cannot efficiently be separated into categories for the hash table, you could end up having a hash table with very long arrays or vectors in each entry, which is not going to be effective.

In our other two courses, we went into more detail in Bash scripting and time complexities of code. The bash scripting seems like a creature we're awaking, that should rather be sleeping. It seems to be a very powerful programming language, but compared to the relatively-close-to-English Java or c++, bash is a pain handling with symbols and commands. Bash appears to me to be a programming language designed by lazy programming beasts. They found it unworthy to write a single word out, as they could easily remember the abbreviation, and now we're stuck with one letter commands. The time complexity calculation remains to be an issue of confusion, and stepping through code for hours seems rather like a punishment than being taught. A new approach would be highly appreciated, not another 3-hour marathon stepping through programs using non-logical tables on unreadable whiteboards explained with far-too-easy questions to those that are still awake. Other than that, we're all set. I can see how using bash scripting, we could easily call c++ scripts and so do quite powerful things. We would then eventually be able to write our own system applications and viruses, not that we wanted, but we could.

Monday, 1 October 2012

Tut 2

We as a group found this lab (The Sudoku Task) more challenging & more interesting than the previous ones. It was a very interesting task, though we did not really complete it fully. We started making that and then we speculated what would be the most officient way of developing it - nevery really get to a working solution. I guess we would have been more hands on - on this project if we knew that time would really be so short.

The other things like datastructures & logic concepts didn't seem that hard to us - as if you know these in other languages (like JAVA - you know them). Makes some of us think - why most highschools teach PASCAL instead of C++.

Other courses didn't really relate with the things taught in C++. I whish there would be as much programming in other courses as there is in C++.