Friday, January 6, 2017

Solutions to Problems

Solutions to Problems


Today I was working on, and were able to finish, Programming Challenge 6.12. So far so good, but I ran into a problem early on when starting to write the code for it. It seemed straight forward, since it is basically a repetition of the three Challenges before that. Yet, there was some little thing not present in these past Challenges that is of importance here. While in the other two only whole numbers were good to go, this one specifically asks for decimal scores.

I was going int at first for parts of the code, which was causing an infinite loop when trying to enter 4.7 for instance. So I changed the function's headers partways. My thought process there was that it is all just a matter of passing the values to two other functions, that would by themselfs only return an int value.

Going this way was causing numerous warnings. Of course, I could ignore these, they are only just that, warnings, right? No, I don't think that would be a good way to think or go. Yes, technically this is the sort of warning telling you that converting, or rather demoting, 'double' to 'int' is going to cause data-loss. Why risk loss of data? Thinking in a grander scheme, if this is a warning, then things could potentially go wrong. And Murphy's Law says: "Anything that can go wrong, will go wrong."

So I changed all my int's in the function header to double, except the last two in the findLowest and findHighest functions. Again, instead of 5 or 10 warnings, i now only got two to deal with, meaning the situation has become better, but still there was this error. Next thing i thought i would try, since the problem is revolving around the return type: 'int', was to magically promote 'int' to 'double'. Since it is double all the way, this should work, but didn't. It wasn't about trying to promote it to 'double' so as to see that x is this value, y is that value.

By design this does not work, so I had to find another way, and the first thing that came to mind was to demote it by typecasting the return value from double to int. Which didn't work very well, so I experimented and found that if i were to use return (int)lowestRefScore would do the trick. And it did. A discovery that I will keep under consideration in situations like these.

In other aspects writing the code for this Challenge was a good way of learning what else can, and will, go wrong. For instance overlading a function with one argument, without being told what exactly was going on. The cause, so far as I can tell, was declaring a variable that caused this behaviour. Things that don't work like declaring a variable with the same name already present in the function header, which will cause an error, or declaring a variable that is in some ways connected to the functions, is going to cause another. Example being, say i would write: int getJudgeData = 0; in main of this last Programming Challenge, it would cause five errors, even without running the programming through the debugger before. And even in some cases this doesn't show any errors. Only when using the IDE debugger to step through the program, you would get a message like: "'error C2082: redefinition of formal parameter 'lowestRefScore'".

So there are numerous ways to run into a trap, and it is good when it happens while one still learns. The effect of finding things out this way, and learning how to solve it, is invaluable and not so easily forgotten.

Colors


One final thing i'd like to write about is the use of colors, or the lack thereof in the published codes. I considered adding them, but ultimately decided against it. I am well aware that there are numerous people who are color blind in one way or another, so I would cause them problems if ever one would find their way to my blog sometime in the future. But it doesn't hurt to publish a picture of my IDE, or does it?




Now, it is back to my IDE, to solve some more Challenges, while listening to Avantasia, Boney M., 真黒毛ぼっくす『酔いどれ東京ダンスミュージック』 (Which would translate to roughly True Black Hair Box - Drunken Tokyo Dance Music, or Enka or even  Haitian gospel/inspirational music (Chorale DEG - Testimony) whatever there is out there to listen to, i listen to it and love most of what i find that is not chart, and does not distract me too much. With that, I wish my visitors a pleasent night, or morning, or whatever else you currently have. And to my fellow learners, good luck in overcoming your errors and mistakes.

P.s. If you happened to visit my google+ space, you might have noticed the picture in this header. No, this is not from some Anime, or gotten from somewhere else, this is Gnade Uns and her demonic cat, my own design. They may, or may not, star in my game to be - just as a hint of things to come.

No comments:

Post a Comment