Saturday, August 21, 2010

Dishes

I've been trying to find "homework problems" to test myself. One of the tests in the book I'm reading was to count all the instances of each letter in a string, and then print out how many time each letter showed up.

This kind of stumped me. My puny non-programmer brain immediately thought of the brute force method - scan the entirety of the text 56 times (a-z and A-Z) and test for each instance of a letter and save that to an individual variable (like numberofa, numberofb, numberofc... etc).

While mindlessly doing the dishes this morning my brain zoned out and made some very jarring connections.

So, I've always known that letters on the screen are numbers that the program knows to convert to a character. Each character on the keyboard (plus non-visible things like tabs, newlines, shift, etc) have ASCII character codes, which (to my understanding) is a standard across all modern machines.

Ok so, what if I treated the letters as numbers? I could scan each character and get the numeric result - but would I just have a bunch of individual variables named "numberof1, numberof2, numberof3"? No. I'd use each number as a position on an array. So my array, call it count[], could have each element of number X incremented each time that same number X is found in the text.

Alright (my brain said, as I scrubbed potato off a pan), that would work, but it's really goofy looking because I'd have a big array that would be unfilled up until I get to the ASCII number range of letters and punctuation. Is there a better way to do this? Well, internally the program doesn't care how empty an array is, or how much space is wasted, but I'm trying to learn good habits. What if I was smart about it and made some assumptions?

Well, I can assume that I only care about letters and punctutation - this means the lowest number I'd be interested in is 33, which is an exclamation point. What I could do is turn that 33 into 0, by subtracting 33. Then it would become the first element (count[0]) in my array. The next number, 34 (AKA double quote) would also be subtracted by 33, and become element 1 (count[1]). Each found number would have 33 subtracted from it. Even better, if the program does come across something lower than 33, I could put a line in that rejects negative numbers (same for numbers too high, just in case).

I might be only decreasing my total array by 32, but it seems like an important lesson to learn just in case. The real victory here was figuring out how to do this with an array, instead of a bazillion individual variables.

Well... maybe the real victory is the knowledge that my brain is on my side - as long as I do something mindless from time to time.

Sunday, August 8, 2010

more programming, less starcraft

I sat down with my C studies for the first time in several days.

I'm really trying to internalize how things look in memory, and what happens when I declare variables and set values.

Like if I say:

int x = 5

the program (or compiler, I'm not sure which terminology is correct here) picks a spot in memory, and sets that spot in memory to 5. What I'm trying to internalize are the more complex notions of this. For example, if I have a computer where each address has one byte of memory, AND an integer on this computer is two bytes, then in memory I'd see:

Memory Location: 0000 0000
Memory Location +1: 0000 0101

So when I try to look at this variable or use it somehow, the program knows that it needs to take BOTH memory locations into account.

Asking for the value of x means the program has to start at the memory address it lives at, and take that byte and the next as well.

I think stuff like this is super important to internalize because not all computers/compilers are the same, and when you work with microprocessors those details are likely very important.

Sunday, August 1, 2010

literature

Ana's mom gave me a $25 gift card to Barnes & Noble. I went there this morning to search for something interesting but discovered that their usually large programming/computer section was rather puny. I wonder how much longer big book stores are going to last (B&N, Borders, Walden's, etc).