When I first saw this BASIC program in eighth grade I already think programming is quite interesting:
10 PRINT "ENTER A:";
20 INPUT A
30 PRINT "ENTER B:";
40 INPUT B
50 PRINT "THE SUM IS :";
60 PRINT A+B
Ooh it is a calculator of some sort! The computer is doing what I tell it to do!
I can even make it run conditional code (IF-THEN-ELSE), and loops and stuff. The computer is my slave to follow my orders! I felt somewhat empowered by that.
BASIC is not very fast, but easy. Also, it only has GOTO and GOSUB statements for blocks of code. I felt more empowered when I can write self-contained functions and procedures in Pascal. Oh and in those days I only program in DOS.
Besides your perhaps boring homework exercises, do you write your own programs for fun?
I did.
Some programs I did, most has to do with math.
4 function calculator. Harder than you may think. When you hit a +,-,*,/ key, I must turn the argument from a string to a number, and keep track is this first argument or second argument?
I also have to make sure you don't enter wacky things other than digits and the decimal point.
RPN calculator. I forever praise the reverse-polish-notation in hand held calculators. Though anti-intuitive at first, it lets you evaluate complex expressions with ease, without all that parentheses. It is also easier to implement. I have a stack, when an operation is selected, just drop two arguments, calculate the result and push it right back. This can handle operations for a typical scientific calculator. I implemented exponents with exponential function and natural logarithm... Now how do you evaluate 00? Then I found out it is undefined. When I know how to program in Delphi, I ported that over to modern Windows GUI.
Function Plotter At first I must recompile for different functions. Later I learned the rather difficult grammar things that I implemented a parser to evaluate a general function. Another hurdle to overcome: coordinates. In math we start (0,0) at center but my graphics library starts (0,0) at the upper left. I also have to scale it. Computer graphics lessons are handy to implement this. It should have the zooming and good stuff like that, but I never bothered to implement it.
Graph 3D. I can't afford Mathematica, but I like to see z=f(x,y) graphs! I did it on my own. But how do you put a 3D graph on 2D? Wow, a ton of matrix calculations are needed to turn 3D coordinates into 2D. Then I discovered an OpenGL library. Wow, it can draw real fast.
I also implemented simple board games.
Connect 4 Easy to implement, just an array. check horizontal, vertical and diagonal. When a friend played it he discovered a bug. It won't announce a win when a connect 5 occurs. Ok I changed that to >= 4 instead of =4.
Mastermind a few secret pegs are selected and you supposed to guess it, and the guy holding the secret pegs may tell you how many you got exactly right and how many you get right color but wrong position. I first used colorful DOS 16 color to implement. Later I turned it into graphics version, that is my first Visual Basic program.
I also use programming to solve puzzle games.
Game of 24 A game played by drawing 4 cards from a deck, and J=11, Q=12, K=13. You are supposed to write an equation so that the cards equal 24. For example, if you have 1,2,3,4. The solution is 1x2x3x4. How do you have your computer solve it? Brute force trial and error works. But it is not very trivial to program it to try all cases. An elegant and faster (almost) solution is to break down the problem into several smaller problems. Take away 1 card, let's say the 4. If you can make 28, 20, 6, 96 out of the 3 remaining cards you get 24. We keep breaking the problem down recursively.
My most recent (still a while ago) fun program is Sudoku. Folks on the train play this game tirelessly. This can be solved by trial and error and backtracking. Just try any legal numbers , if no more possible numbers, then retract back. Easy to describe, a bit involved to implement. Hardcore computer scientists will tell you this is not the quickest approach. But heck it is pretty quick to solve a 9x9 grid.
Oh I also did a ton of little applets for my book, including a demo of Archimedes' π approximation and Reimann sum illustration...
Awaiting the next fun (doable) programming assignment for myself.
No comments:
Post a Comment