The image is difficult to see if you just look at it as is. You should 
click the image to get the full size image. But you knew that.

This is a computer program written in tiny-c. Sort of. It's actually the 
lines of a computer program written in tiny-c that have been sorted by 
treed.tc, another computer program written in tiny-c. We've mentioned 
treed.tc before1 but today we're concentrating on cuiscnt.tc. Why one 
would want to sort the lines of a computer program is perhaps best left 
for my analist (sic) to splain. But I digress.

It may be too much to ask ... but I will anyway. The numbers on the left 
of each line are the original order the lines were in before they got 
sorted. Please unsort the lines (in your head of course) so that you 
make the line with the 1 on the left the first line, the line with the 
number 2 on the left the second line. You get the picture.

Lines that start with /* are comments in tiny-c. They don't actually 
cause the computer to do anything. They're just there to help the reader 
understand what's going on. So our program is named cuiscnt.tc, it was 
written and revised in early December 2008 by yours truly.

I can see this is going to get messy so here's the complete listing, as 
it is presented to the computer.

/* cuiscnt.tc - 12/3,4/8 - lrb

cuiscnt

[ /* 0

/* "lazy man's" way to count the number of rods

int c(9),t(0),tt,i

c(0)="10 orange       x  8   =    80" 
c(1)=" 9 boo          x  8   =    72" 
c(2)=" 8 brown        x  8   =    64"
c(3)=" 7 black        x  7   =    49" 
c(4)=" 6 green        x  8   =    48" 
c(5)=" 5 yelloh       x 10   =    50" 
c(6)=" 4 alizarin     x 12   =    48" 
c(7)=" 3 light green  x 18   =    54" 
c(8)=" 2 red          x 24   =    48" 
c(9)=" 1 wood kuller  x 41   =    41"

for (i=0;i<10;++i)

[ /* 1

atoi(c(i)+18,t);pn(tt=tt+t(0))

] /* 1

] /* 0

In as few words as possible, this is what the program does: it adds the 
numbers in the column that starts with 8 and ends with 41 and prints out 
on your screen the intermediate sums. The final number printed is 144 
which you are invited to manually verify is what those numbers add up to.

The comment that has the word "lazy" in it tells you a little about the 
author, that would be me. It's in quotes because most people would feel 
it would have been far easier to add the stupid numbers than to write a 
computer program to do it. And they would have a point. But I wanted to 
be sure the sum was correct (I really really wanted to be sure how many 
Cuisenaire rods I had because the towers my brother and I have been 
building keep falling down and the rods tend to scatter and I've spent 
an embarrassingly large amount of time hunting for errant pieces which 
tend to finally come into view underneath far away places you could 
swear the laws of motion precluded).

cuiscnt is the main (and only) "function" in our program. It was chosen 
for obvious reasons: We're counting Cuisenaire rods here so let's name 
the main "function" accordingly. Functions in tiny-c start with their 
name. This name is followed by a left bracket which in turn is followed 
by a bunch of code that actually implements what the function does. A 
final right bracket closes off the definition of our function.

We "declare" a few "variables" that we are going to need to get the job 
done. Every "variable" used here is an integer variable. These hold 
whole numbers, like 81, 541 etc. The "array" c(9) is actually 10 distint 
integer variables, c(0), c(1) ... c(9).

We stick the "text strings" that describe our various Cuisenaire rods 
into computer memory in the next 10 lines of code.

We want to "loop" 10 times, once for each of the rods. This is what the 
"for" statement that you see next does. Notice the new set of left and 
right brackets that block off all the stuff we want to do during the 
"loop."

OK. Here comes the nitty gritty. And this is why we named this blog post 
atoi and itoa ... If you're a C programmer, atoi is already burned into 
your vocabulary and I could stop right here and go back outside and 
finish off that gutter clean-up work. But, I expect you are not a C 
programmer and I've got some splaining to do.

The central problem here is that we're dealing with "character data," 
not "numbers," and, since we're interested in adding up numbers, we've 
got to somehow turn the "characters," like "8" and "41", into their 
numeric form, ie. 8 and 41, so the computer can add them.

Enter atoi.

Did anybody google on atoi or itoa when I put up my skeletal post? No? 
Yes?

It's short for ascii to integer. It's a "builtin" "function" in tiny-c 
(and in C, tiny-c's big brother). This means you can use it without 
writing it yourself. It's in the "standard library," along with, by the 
way, pn (print number), which we will be using shortly.

atoi takes two "arguments." The first "argument" is the "character 
string" you want to convert to a number and the second "argument" is the 
integer variable where you want to put the number. The first "argument" 
here is actually an "expression" which the computer computes as i 
changes from 0 to 1 to 2 ... to 9. c(4) for example is the place in 
memory that holds the "string" " 6 green        x  8   =    48" and if 
we add 18 to this place we are pointing at the place that holds the 
space character just to the left of the 8. The variable named t will get 
the converted number.

After each number is put in t, the variable tt gets updated. tt holds a 
running total of our Cuisenaire rod count. The "library function" pn is 
"fed" the new tt and does what it does best, namely, it prints the 
number on the screen.

This is what you see when you run the program which we have now (thank 
God!) splained.

8 16 24 31 39 49 61 79 103 144

OK. It's time for a Quiz. 

What would get printed on the screen if the atoi line read  

atoi(c(i)+28,t)

instead of 
   
atoi(c(i)+18,t)

Extra extra credit:

How 'bout

atoi(c(i)+0,t)

which of course could be written more simply as

atoi(c(i),t)

Footnote:

1. treed.tc is an updated version of tree.tc. It adds one new feature, 
namely input via a file. This makes data entry a whole lot easier.