Showing posts with label Chapter resume. Show all posts
Showing posts with label Chapter resume. Show all posts

Sunday, March 5, 2017

Chapter Summary

Arrays - The Spinal Final Frontier




Normally when I write a post like this, I would start at the beginning of the chapter. Or, if there has been a challenge that was of particular interest to me, I would first write about that. This time around the end marks the beginning of this summary. It was particularly challenging to solve it, and very interesting, because I learned how to use 3D-Arrays while working on it. 

You might wonder why I used 3D-Arrays in the first place. It was an accident of sorts ... I wouldn't have, had I paid closer attention to what was asked, while copy-writing the tasks to the header portion of my source file. Nowhere does it mention to use a 3D array, let alone two. Instead of taking a second look, I started planning it all out, then started wondering how in the world I should be able to use 3D arrays in a program. 

I flipped back in the book as there was mention of 3D-arrays. All of 10 lines and a picture showing one such array and its definition: double seats[3][5][8]; This particular line affirmed my believe that indeed the challenge involved usage of this sort of array ... All the more reason for me not to go back to the challenge page to check, instead I started to work on it. 

The first thing I tried to figure out how to display such an array with test data. I simply initialized the array in main at the time to the example chart and went from there. This wasn't too difficult to figure out. First of all, if you need one loop to do whatever it is that is to be done with a one-dimensional array, and two for two-dimensional ones, it would take three for a three-dimensional one. So far so good. So to get my program to display something was the easy part. Working out how to display the rows and sections information, as well as the free and booked seats, this was another story.

It is exactly that problem at which I failed in my Tic-Tac-Toe game ... The problem is quite similar in nature in both challenges. Around the board there could have been a display of coordinates instead of just the board. After managing to get it to work in the last challenge, I debated with myself whether to update the game code but I decided against doing it. The main reason for that decision is that every challenge solved shows progress in one way or other. I would ruin this, and instead of being able to say, yes I was able to improve, I would look at code that has been improved. No, that wouldn't be worth it at all! But back to this last challenge.

After having the display part in place and the update code for the auditorium, as well as the prices and the seating chart in two separate files to be read in, the next major challenge was to add up the prices ... This was indeed a challenge to be overcome in itself. My idea was to loop through the seats array to add up all the booked seats, then use that information to add up the sum of sold places with the information stored in the seat prices array. This seemed to work at first. 

When simply displaying the stats, without having booked additional seats, it would output the correct result. That is to say that, since I still used the example data with a number of 265 booked seats, and my first example file had 12.00 as price throughout, the sum: 3180 was correct. Once I booked another space, the output differed greatly. Instead of simply adding 12.00 to get 3192, the output was 3240 and how is that to happen? Probably because of the way I used a certain ternary, maybe something else, I simply couldn't figure it out. After rewriting it and adding some changes it finally worked, and in the end it didn't even matter, as the program would start out with a blank seats files anyway. And with all blanks and adding up from the beginning, even after numerous bookings, the results were always correct. 

I could have saved myself lots of time by paying more attention to both my plan for the program, as well and in the first place to the things that really had been asked, which didn't involve 3D-arrays. I'm still glad that this mistake has happened. How else would I have come in touch or experiment with such arrays? It was a valuable lesson, and I learned quite much from it, so it payed off doing it.

Even without the last two challenges it took quite some time to finish this chapter this time around. I can't pinpoint why this is as arrays, one-dimensional and even the 2D ones, aren't that much more complex than anything I have encountered up this chapter. Which as experience shows says nothing when it comes to challenges like Monkey Business, which was far from being monkey business. This one haunted me for the longest time, almost like the oft mentioned Tic-Tac-Toe game, short of finishing it I failed and had to redo it all. 

There is of course always the option to skip a challenge but this would hurt in the long run. As I think I mentioned in one of my earlier entries, there are numerous challenges that are similar in principle. If you are able to solve one, than it is certain that you can also solve the another that is similar in nature but not the same. This chapter is a good example for that, and how cleverly the challenges have been selected by Mr. Gaddis. Being persistent is the key for solving most of the problems. The reward is some challenge that is both a welcome change of pace as well as a reward once solved.

The chapter may be ended but the topic of Arrays is still going to follow me into the 8th lesson. I'm curious how this one will turn out to be and what I will learn from it. What I don't wish to know at this point is how the challenges of this upcoming chapter are looking like. And even less how much time they'll take me to solve them! All that matters to me for now is that things will go a little smoother once time has come to work on the actual challenges whatever they will be. 

For now I will call it a day and wish all my fellow learners who still struggle with this chapter that they soon be finished! See you all in a week, or way less than that, with new code. I, at least, look forward to it and hopefully you do too. *winks*

Thursday, January 19, 2017

Functions, Functions Everywhere

/* AD:SNIP ------------------------------------------------------------


 (Picture copyright: My own work, but you are welcome to distribute it freely or use it as desktop background if you happen to like it.)

First things first, I promised and keep my promises always, that I would advertise for a free german RPG set in the Star Trek Universe. (Most of the regular visitors do  happen to speak English and some other languages as well. As long as you don't speak Melmacian, you should be understood). There are a number of very friendly mostly adult players at the beginning, in between, or way above and beyond their thirties, but also some young players from 15 up to 30. They all are dedicated Star Trek and Star-Wars fans! There is also a wonderful radio belonging to this project, as well as an IRC-Chat for you to hang out there. It is a Chat-RPG after all! (The chatroom is also the place you will find me on an almost regular, spelling daily - basis.) 

You can find the project here: https://www.st-city.net/
The RPG homepage can be found here: http://www.startrek-redemption.de/ueber-uns/
The radio homepage can be found here: https://isn.fm/
And last but not least the IRC-Chat: https://www.st-city.net/chat/
As well as the address for Clients such as mIRC: http://www.startrek-redemption.de/chat/

Klaas, Lady, _7th, On_Tour_Casio, all the regular players, would love to see you pay a visit and maybe stay for a while to play along, listen to the radio, or just be a friendly addition in our friendly chatroom and to have fun with the team, and myself!
----------------------------------------------------------- AD:SNAP */

Finally I got done with all the programming challenges of chapter 6 ... Time to celebrate! Now, how were these challenges? Difficult? Well, some of them were, some were very easy, even fun to write code for, and the last one was nothing short of mind-numbing.

When I read what was to do, and it said there in the heading of the Challenge: Group Project, whether I should just leave it be. Then I thought it wouldn't hurt to round-off what I learned during this chapters challenges, so I went to work away on it. More than once during the three plus some hours it took to write this one, the song the nameless character in the Action-RPG "Two Worlds I" sings spooked through my head. In this game, almost every time it rains, the character starts to sing: "Its raining, its boooooring, the old man is snooooring ...".

I still finished it, and maybe it was a good decision. I learned about another way of overloading a function. In this case this was not wanted nor asked for. In Programming Challenge 6.25, in the function prototype, I declared some default arguments. I did this in another challenge as well where it was asked for, which was Programming Challenge 6.14.

I declared them at the end of the prototype as it should be, but when I called the function in main, all I got was a failure that the function is overloaded, and that the same function is used elsewhere. So, what was the problem then? I declared the default arguments, and the first time around I used some variable names that I already used in one of the other functions. So I changed them for the default arguments, which didn't get me anywhere.

The solution I found was that I had to use the functions default arguments not only in the prototype header, but also in the functions definition as well. I can't remember reading or having learned about that in the sub-chapter that covers default arguments. In the end everything worked and this is what counts. My final thought on this last project is: While the programming challenge asking for overloading functions dealt with exactly that, this one I would christen "Function Overkill", as the challenge asked for writing separate functions for each and every thing that is asked for by the user.

The challenge to solve I liked the most in this chapter was the Rock Paper Scissors Game. I think I wrote something along the lines of - "My first game ... be it ever so humble", yet I think that being a wee bit proud about it is in order, I let you as my readers judge on that one.

On the whole my understanding of loops and their inner workings, as well as my love for the ternary operator, or as I call it in my source-code comments "Conditional statement", grew deeper, as well as my understanding how and when to use it. In the game project I wanted to use it three times, as it offered itself as a perfect solution to circumvent lengthy if-else blocks. The one and only time I actually were able to use it to full effect was in the function which determines the winner of each round.

The other two places I wanted it to use was to display the winning choices. As I said, it would have been a mere 4 lines of code for each of the other two functions. But the problem was that every time the function got in scope, and the winner or winning choices were to be displayed, the program displayed all three conditions, which looked like this:

   * R-P-S Master has chosen O Rock - O Rock beats Scissors % O < R-P-S Master has won
   * R-P-S Master has chosen [] Paper - [] Paper beats Rock [O] R-P-S Master has won
   * R-P-S Master has chosen %< Scissors - %< Scissor beats Paper %> |-_ R-P-S Master has won

I thought I could shift around the problem by introducing some if/else statement, but that didn't work. So I let this idea be and just wrote the if/else clauses instead. I guess it was just the nature of entering a function block that saw all conditions as true as far as winning conditions are concerned, and displayed them accordingly. Although the ternary, taken outside any function, did work as I wrote them.

That is the strange and funny things, I would not say shortcomings of C++ when it comes to functions, that I have learned. For the game itself, which wasn't very challenging at all, it was the first time that I chose to write sort of a prototype, see what works best, then take the things working over to version two, and then to assemble the final version. I also used to use my Mindmanager to create a map for the project, to get some starting ground to work off of. In the final version the map deviates in several points, but I guess this is a natural process. I didn't update the map every time for every change, as this would have taken time away, and unnecessarily so, from writing the game itself.

In bigger projects, in my own future game as well, this approach of not updating any reference material will be dropped. It is easy to lose track of what is and isn't done, and this is something I wouldn't want to risk. Now, after this wall of text, here is something for the eye, the mindmap for the Rock Paper Scissors game.



Doing this chapters challenges I also noticed that I slavishly held myself back in not writing any  functions unasked for to solve the problems. That is up to around Challenge 22, where it is specifically asked for to write one's own functions. I can't remember having held back in any of the previous chapters in using that what I learned in the specific chapter. Deviating a little here and there, making it more fun to write code for, or maybe to amuse people who happen to learn and read or take my code as blueprint for their own work, I find that acceptable. As reference I name the Overloaded Hospital Challenge. I use names of Piers Anthony's Xanth series Characters, Com Passion, Jenny Elf (making for a very tiny hospital bill), Che Centaur, and others. 

I don't have to answer to anyone, there are no teachers, or professors who mark my work and decide whether I pass or fail a challenge, this is why I allow myself this little luxury. I do write and learn this privately to gain knowledge in my language of choice, to solve that, which I learn for some time in the future: Writing and publishing my own game. The only judgment I accept is that of you, that is my readers, who happen to pass by once or come by to visit my blog on a regular basis. Because the fruits of my work are there for you to take after all, and in that respect I make sure that all challenges do what is written, bound to what is asked for in terms of functions, loops, whatever else is asked for, and that the output is correct. In the end it is up to you whether my code is worth something, or does help solve your problems, or overcoming any obstacle you have to face. Just DON'T USE IT FOR YOUR HOMEWORK ASSIGNMENT! You have been warned, I don't take responsibility for your course score dropping below sea-level.

Now, all that said, and many more things I think should probably be said leaving it unsaid, I now look forward to chapter 7. Arrays. I don't know how steep the learning curve will be, or how difficult arrays are in relation to what I learned in the past 3-4 months. But then, does it matter? I look at it this way, as I did since Starting Out with C++, another word or words in my vocabulary list, allowing me to write shorter programs, in a less roundabout way. And there are always some difficulties so it goes double that it does not really matter whether this one or the next, or the final chapter proofs to be more difficult.

So, with this, I wish all of my fellow learners that they never give up, are able to overcome their current problems, so they, too, are able to taste the sweet fruits of success when overcoming a particularly difficult challenge or problem!