Sunday, January 11, 2009

Hooray for C!

___So, as part of my ongoing foray into the world of Ubuntu (the version of Linux I'm currently using) I spent most of yesterday getting a simple C program to work. This might be the best time to note that, while Java (the programming language I'm fluent in) was built with the motto, "We can do anything that every other computer can do." C seems to have been built with the motto, "We can do anything your computer can do." As a result a Java manual has a very specific list of function and a very strict protocol as to how those functions are called and what they will return. Any C manual I've found, however, reads something like a list of things that it might be capable of. For example: (In Java) Calling the Integer.parseInt(String):int will return a 32bit signed integer to the program. (In C) Calling Math.parseInt(String):int can return anything from a 32 to 64 bit number that can be either signed or unsigned based on your processor, operating system and your computers implementation of C. As one might imagine, the difference between a nice 32bit signed integer and a 64bit unsigned integer is somewhat extreme. As a result, while making this "simple" program yesterday, the greater part of the day was spent finding out how any of the functions I used actually worked on my system, but I digress.
|
___The simple program works something like this. It's purpose is to guess the number that you are thinking of. It first asks you for the top most number in the range of numbers to guess from by making you choose the next highest power of two for that number. ie. if you want the computer to guess between 0 and 100 you would type seven for the next highest power of 2. 2^7 = 128 The computer then guesses at the middle of your chosen range and asks you if that was the number that you are thinking of. If it is the right number the computer wins, if it wasn't, the computer asks whether your number is higher or lower than that number (thereby cutting the range of numbers to guess from in half.) The computer then guesses at the middle of this new range and asks it that number is your number and so on until the computer finds your number.
|
___In theory, this program is very simple it really only needs 5 basic functions and two data types: a function to get input from the user(Q for question), a function to send output to the user(P for print), a looping function(L for loop), a function to test the user's response(T for test), a function to jump around the code(J for jump), a number data type(N for number) and an alphabetical character data type(C for character). The program then works like this:

range(N) = "What power of 2 would you like to guess to?"(Q)
currentGuess(N) = range / 2
as long as the computer hasn't guessed the right number(L)
   response(C) = "Is currentGuess your number? (y/n)"(Q)
   if response = y(T)
      exit the loop(J)
   if response = n(T)
      range = range / 2
      response = "Is your number higher or lower? (h/l)"(Q)
      if response = h(T)
         currentGuess = currentGuess + range
      if response = l(T)
         currentGuess = currentGuess - range
loop
"I win! Your number is currentGuess."(P)
response = "Would you like to play again? (y/n)"(Q)
if response = y(T)
   goto the start of the program(J)
if response = n(T)
   exit the program

___In Java this might take me five minutes to code. In C it took me over four hours, during which I had four different bowser windows open for reference, one text editor open for coding, one terminal window open to run the code, and one file browser open to make sure all of the files were in the right place.
|
___At the end of that period in time, sitting satisfied with the laptop perched on my legs I took to the time to revel in the finished product and then made the easy mistake of thinking about what I had actually accomplished. The entirety of this story can effectively be compressed down to:
|
___I coded for four hours and made a computer guess the number I was thinking of.
|
___Hooray for me?

1 comment: