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

No comments:

Post a Comment