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*

No comments:

Post a Comment