Friday, December 5, 2008

atoi and itoa ...

Commentary coming ...

9 comments:

Tinyc Tim said...

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. Buy 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 tree.tc, another computer program written in tiny-c. We've mentioned tree.tc before 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.

Click here for more.

Tobee 'n DeeDee said...

I read half way through the "more" and then said, "self, this is way over your head!" Soree, I tried, doh, I reelee did!

Tinyc Tim said...

Thanks for trying. This is not easy to explain. Hopefully Max, who has programming experience, will follow it.

Chip Bradley said...

Tinking ...

Chip Bradley said...

I can see now, after reading "more," that we have two quiz questions (one of which is an extra credit question) which both relate your interesting cuiscnt.tc program -- which itself has been sorted by the tiny-c program tree.tc.

With adding (hypothetical in this case) block lengths together in an "accumulative" fashion -- using a ascii (text) to number transformation process, I can now see that the number 18 was a good clue for me to try to come up with the two answers for the two quiz questions. I appears the tiny-c program is taking the string (enclosed in the quotes) and making note of the fact there are (in the ACTUAL strings) 18 spaces BEFORE reaching (and noting) the all important number for the accumulative addition process).

Quiz #1 Question:

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

atoi(c(i)+28,t)

instead of

atoi(c(i)+18,t)?

Quiz #1 Answer:

80 152 216 265 313 363 411 465 513 554

The reason for this is because the number "pointed to" in the ascii (text) string would be the LAST number in each string. These values for t are then summed for each accumulated answer. It is worth noting though that 18 would be the more appropriate number to use, but for quiz purposes, I can see why you picked 28.

Extra extra credit:

How 'bout

atoi(c(i)+0,t)

which of course could be written more simply as

atoi(c(i),t)?

Extra extra credit answer:

1 3 6 10 15 21 28 36 45 55

This answer (in the line above) would indicate we are dealing with an accumulative set of t values that look like the following (which mirror the text value in the FIRST column)

1 2 3 4 5 6 7 8 9 10

1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
15 + 6 = 21
21 + 7 = 28
28 + 8 = 36
36 + 9 = 45
45 + 10 = 55

Is this tinking correkt?

Tinyc Tim said...

I read your answer and immediately began to compose in my head a response which was top heavy with remarks of praise. Indeed, you basically nailed this. However, technically speaking, you really only got Quiz #1 exactly right. Just to be ornery, I'm going to modify the program slightly so what you gave for the answer to Quiz #2 would be correct and then ask you to tink again and tell us what the unmodified program actually outputs.

for (i=9;i>=0;--i)

[ /* 1

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

] /* 1

The stuff in the "loop clause" is saying: initialize i to 9, do the stuff between the brackets, test i to see if it's still greater than or equal to 0 and, if it is, "autodecrement" i by 1. That --i thing is equivalent to i=i-1 (which is not to be confused with a statement of equality but is what we call an "assignment statement.") In English, take 1 away from what i holds and then put this lower value in i.

Chip Bradley said...

Tanks for preesheatin my Quiz #1 response and for giving me a hint on how Quiz #2's response would have been correct given your changes to the original code.

I think I see what I did wrong. I was paying too much attention to the value of the blawk lengths (from 1 to 10) rather than the 10 possible values of c BEGINNING WITH c(0) and ending with c(9). Those values represent the blawk kategories beginning with the LONGEST blawk and ending with the smallest wood kuller blawk.

So, because we are asked to list the pn values based on:

atoi(c(i)+0,t)

also represented as:

atoi(c(i),t)

the correct listing should have been:

19 27 34 40 45 49 52 54 55

because

10 + 9 = 19
19 + 8 = 27
27 + 7 = 34
34 + 6 = 40
40 + 5 = 45
45 + 4 = 49
49 + 3 = 52
52 + 2 = 54
54 + 1 = 55

Max

Tinyc Tim said...

Ten intermediate sums get printed, as the loop goes thru 10 steps.
I spoze knowing that all variables are initialized to 0 in tiny-c might be useful to know.
You obviously understand well the basic idea of what's going on here. Even your summing 1 thru 10 (in that order) was essentially what the program does (modulo the order of summing). And with your now realizing the numbers are to be added starting with orange's length, not wood kuller's, you are within .5 Angstroms of the absolootly korrek output.
Sincerely,
Your pain in the patoot analitic brother,
TcT

PS - By the way, did it ever occur to you to cheat by saving the source code in a file and running the tiny-c interpreter on it? This of course would require that you are able to locate the interpreter, know how to run it, know how to feed it the program cuiscnt.tc and finally know how to invoke the cuiscnt function. You probably have tinyc.exe lying around somewhere. I'll write something up on how to do all the above so, for right now, since I expect you can finally nail this final buglet without resorting to "cheating" anyway, disregard this rambling and see if you can solve this with your head.

Chip Bradley said...

Well, that little buglette is bugging me enough to cheat (which for some reason does not feel like full cheating) so that I can run the tiny-c interpreter to see what the 10 (not 9) numbers are.

It sounds like you may be thinking about (or have already written( a guide for me on how to do all that

BUT

can I see if I can do it wouthout the guide yet? I thinl the paragraph in your last comment may be enough for me to dig this one out of the woodwork.

I have just gone for a 3-mile run, photographed the first measurable snow (and made a short movie of it) but have not showered or had brakfust (that's how that young Kenyan woman said 'breakfast' in the McDonalds commercials in the Beijing Olympics) yet, so my answer may not come until 9:30 pm. Sowee.