Sunday, December 9, 2012

How To Build A Programme

Okay i just passed Algorithm & Programming [QBASIC] mid-semester test, the questions weren't too complicated but the time is pretty tight. So i choosed 2 question, one which was medium level and the easiest one. I want to explain the medium one. There was a song, sung like this :
4 bebek turun, mati 1 tinggal 3
3 bebek turun, mati 1 tinggal 2
2 bebek turun, mati 1 tinggal 1
1 bebek turun, mati 1 tinggal induknya
The number of duck which down to the street is a dynamic number refering to what user input, so it can be any number. Every time they down to the street there always be death for one of them *what a pity*, that's why i call it "Duck Genocide" application. So here's the troubleshooting.

1. Find the pattern

Every time you have a case of programming you should find the main idea of the programme, the pattern is really important. In this case is a decreasing number of duck population, every line is looped refering to first number of duck. Decreased line by line and when we have no duck left, there's only their mom. Per line there's two dynamic value, the duck number every time they down to the street and the one after the duck death. So mathematically *what the fuck do i say about math #Geez!* :
The First Line, Duck Number - Line  = Left Duck
Next Line, Left Duck - Line = Left Duck
The Latest Line, Left Duck - Line = Mommy Duck
On the other way, line also can be called by looping time. We can prove it with the song,
Line / Loop 1, 4 bebek turun, mati 1 tinggal 3
Line / Loop 2, 3 bebek turun, mati 1 tinggal 2
Line / Loop 3, 2 bebek turun, mati 1 tinggal 1
Line / Loop 4, 1 bebek turun, mati 1 tinggal induknya

2. Collect Variables We Need

There's a static data, there's the dynamic one. You have founded the pattern, what things we need to accommodate the dynamic value which are proccessed. The first duck number per line and duck number left after the death. We need two variables, just call it "A" for the first duck number per line and "B" for the other one. One more thing, "N" for the number which is input and "i" for the looping time / line number.

From the pattern we had there's a gap, "A" variable is doubtful, it should be one but there is more. We should join them, the first line is the duck number which is input by user and the decreased one from the next and latest line. To join them together on decreasing loop without loosing input value by user then we should increase it while decrease it, haha it's pretty confusing. 
4 bebek turun, mati 1 tinggal 3
3 bebek turun, mati 1 tinggal 2
2 bebek turun, mati 1 tinggal 1
1 bebek turun, mati 1 tinggal induknya
See the number i bold, if we just decrease it then the proccess will immedeately decrease the first number which is 4 to 3 so the song will be started with 3 ducks and in the same time "A" shouldn't reach null number while "B" does in looping. That's why "A" should be increased while being decreased. The formula should be like this, A = N - i + 1 and B = N - i meanwhile "B" and "A" almost have the same formula so we can change "A" formula to this A = B + 1 but "B" should be processed before "A" so computer will recognized the "B".

3. Mix It!

You have the pattern and the variables you need. So let's mix it! We build it using QBASIC.




No comments:

Post a Comment