Thursday, January 12, 2017

A couple of days ago I was sick which caused a lack in updates. I bet, if this hadn't happen I would already be working on Challenge 6.24 instead of 6.18 which is next. But, what can you do? When I felt better to sit down and start with Challenge 6.16 I just couldn't figure out how to write the code for the function that would take care of the calculation. I blamed this on not being fit, my head was virtually empty, so all I managed to do was writing the bare-bone framework of the program. This took about 12 minutes, including input, declaring and initializing variables, and what else makes up the initial parts of the program to work with.

Then when it was time to implement the actual workings, I felt totally empty, like I had never written a single line of code ... ! The loop wouldn't work as it was supposed to, the implementation of the algorithm that was supposed to take care of the calculations delivered all the wrong results, I felt like I could cry. I started over and over, one time I almost had it, then saw that it worked with only simple numbers but didn't deliver correct results on the whole, commented out lines, several of them, it was a mess. Empty the function, rinse and repeat.

I was ready to openly admit my being beaten by this Challenge, when I started feeling better so much so that I started doing what should have been done in the beginning:

* Plan ahead
* Look at the material you are dealing with, and what the problem actually is
* Collect information, take what you can
* Sit down, analyze the algorithm
* Calculate an example
* Then do a mind-mapping session to plan it all out
* THEN sit down and write the code 

Even without these preliminary steps what it is, translated into "problem to solve" words, is in principal a compound interest problem. Only that it is the increase and decrease of population and not money to deal with. But even this simple ray of inspiration that entered my mind didn't help me to get my code done, or getting me a step closer to solve a small part of the problem. In the most roundabout way you can imagine I tried to figure out such simple steps as accumulating value x by y. ... As mentioned before, it was like I had never written a single line of code. 

The code kept fighting me with claws and teeth, as if it didn't want to be written by poor old me ... *  But I would not give up! I fought back, and eventually succeeded to at least get the uninfluenced rate of births to accumulate and display. This was the breakthrough I sorely needed. I started to see light at the end of the tunnel again. At least I got me somewhere. The rest was then easy, and step by step the final program grew and saw the light of day.

One of several things I learned the hard way in this case is, that without planning before doing, which is most important in such situations, and if  I don't feel like being able to write a code, step away and let it be for awhile.

The situation in total, and looking at Programming Challenge 5.17 that I solved today, which is another case of those programs that practically wrote itself without doing much thinking, it is that I come to appreciate functions more and more. Yet my attempts to use their full potential are still most clumsy, but will improve step by step the more I use them in the future. The thing I like the most is that it gives freedom to work on one part of the code, add small things to another, then get back to the function(s) and work on yet a third part. (Which approach can lead to me getting lost in code by loosing track of what it was that I wanted to do and where). Lastly I have come to like and appreciate functions for the simple fact that, by simply switching the call to the function with another, say in main, the output changes, without having to move a whole block of code to a different position.

Of course I have also seen a months worth of possible problems when it comes to dealing with functions. Unintentional overloading, redefining a function in another part of the code, how seemingly different function headers would lead to a duplicate function that already exists, and many more such things that would be either displayed in the IDE, or at least in the debug log. It has been a lot of lessons learned and stored in memory, that much is certain.

On a final note, I hope from the last code on, this Chapter's challenges ahead of me will all be downhill from the steep climb of Challenge 6.16 on down. And by the time, and if luck is on my side, Tony Gaddis new/old book Starting out with Graphics in C++ will arrive at my place by the time I finished this chapters challenges.
With this I wish all of you success in overcoming your programming challenges, and that you be able to avoid all the traps and pitfalls I stumble into lots of times. Don't give up, and keep climbing!

* No, I don't give away my age! O.k. ... you convinced me, here is a hint: I was turning 8, the year Chernobyl blew up. And here is a challenge for Beginners who happen to enter my playground: Write a program that calculates my actual age and outputs it to the console. *winks*

No comments:

Post a Comment