Go Down

### Topic: Every so often I am stunned by a post. (Read 10396 times)previous topic - next topic

#30
Thanks.

#### nickgammon

#31
##### Mar 19, 2012, 11:05 am

For me, the most peculiar thing about C is that the missing condition in
Code: [Select]
`for(;;)` evaluates to true.

Yeah good point. Although if it didn't, it would be pretty useless. I suppose the logic is: Have a starting statement, or nothing. Have a condition, or no condition (in which case keep going). Have an end-of-loop statement, or nothing.
Please post technical questions on the forum, not by personal message. Thanks!

#### GoForSmoke

#32
##### Mar 19, 2012, 03:09 pm

"C code for I want to be fairly obtuse"
Code: [Select]
`int indx = 0;  while(! indx [outstr] == 0) {  indx[outstr] = indx[instr];  indx = indx + 1;}`

LOL!

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### retrolefty

#33
##### Mar 19, 2012, 03:58 pm

lol its really both

It's not unlike a soldering iron, a useful tool if used properly, but it can definitly burn you if your not careful.

Lefty

#### cyberteque

#34
##### Mar 20, 2012, 04:02 pm
How do this kind of recursion in C?

Code: [Select]
`Procedure Tree; {(x, y: integer; dir: real; level: integer)}Var   xnew, ynew: integer;Begin  If level > 0 Then    Begin      xnew := round(level * Scale * cos(dir)) + x;      ynew := round(asp * level * Scale * sin(dir)) + y;      PenSize(level, level);      MoveTo(x, y);      LineTo(xnew, ynew);      tree(xnew, ynew, dir + inc + Randomize(2), level - 1);      tree(xnew, ynew, dir - inc + Randomize(2), level - 1);    EndEnd;{Tree}`

This draws tree's!
Quite nice ones as well!
In 3D I have 1 on the X/Z plane and another on the Y/Z plane, other wise they disappear into a vertical line.

I use this kind of recursion with linked lists a lot.

#### AWOL

#35
##### Mar 20, 2012, 04:05 pm
Recursion is simple, but it can be a RAM-hog, so beware.
Code: [Select]
`unsigned long factorial (unsigned long n) {  return (n == 0) ? 1 : factorial (n - 1)  * n;}`
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Jantje

#36
##### Mar 20, 2012, 04:08 pm
Yust write in C like.
The only methods I see problem with are:
PenSize(level, level);
MoveTo(x, y);
LineTo(xnew, ynew);
Because there is no pure c alternative. If you uses Visual studio there are but I wouldn't know any alternative for Arduino.
Bets regards
Jantje

How do this kind of recursion in C?

Code: [Select]
`Procedure Tree; {(x, y: integer; dir: real; level: integer)}Var   xnew, ynew: integer;Begin  If level > 0 Then    Begin      xnew := round(level * Scale * cos(dir)) + x;      ynew := round(asp * level * Scale * sin(dir)) + y;      PenSize(level, level);      MoveTo(x, y);      LineTo(xnew, ynew);      tree(xnew, ynew, dir + inc + Randomize(2), level - 1);      tree(xnew, ynew, dir - inc + Randomize(2), level - 1);    EndEnd;{Tree}`

This draws tree's!
Quite nice ones as well!
In 3D I have 1 on the X/Z plane and another on the Y/Z plane, other wise they disappear into a vertical line.

I use this kind of recursion with linked lists a lot.

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

#### cyberteque

#37
##### Mar 20, 2012, 04:13 pm
Quote
Recursion is simple, but it can be a RAM-hog, so beware.

Oh hell yeah!
Ages ago I wrote a "Galaxy" builder, around the galactic centre it randomly places stars, then puts planets around the stars, moons around the planets.
A friend of mine was messing around with the build dialogue.
I wasn't really paying much attention at first, till I noticed he was punching in really big numbers for star density, that got multiplied by (100 * random(100))!!

He says, "gee, this is getting slower!"

This was on a Mac IIci, with only 8M of RAM!

#### cyberteque

#38
##### Mar 20, 2012, 04:24 pm
With nootropics TVout library

MoveTo(x, y);
LineTo(xnew, ynew);

turns into

TV.draw_line(x,y,x,y);
TV.draw_line(x,y,xnew,ynew);

#### GoForSmoke

#39
##### Mar 20, 2012, 05:01 pm
When you call a function it saves the return address on the stack and any variables local to the function go on the stack so when a function calls itself over and over you better have more than 2k for everything!
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### cyberteque

#40
##### Mar 21, 2012, 12:45 am
Back when I was writing business software for Apple II, how much RAM you think we had left out of 48K?

I mainly use my Mega's for stuff like this.

#### GoForSmoke

#41
##### Mar 21, 2012, 07:31 am
I cut my teeth on less than that but it's in range for 1980 or so. The main thing is to constrain how far the code should need to go. For small machines I can suggest Forth-79, it's amazing how much you can do with it in tight space.

With some systems it seemed I could write small useful programs with an executable 100's of bytes long and then by the mid-80's on the preferred setups it was seeming like the easy minimum was 8K without taking extra steps to cut the code size down. The more memory we got, the much more got used for extras that count less.
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### Jantje

#42
##### Mar 21, 2012, 01:48 pm
I started off on a commodore 64K.
That was plenty of memory in those days.
Can you understand how puzzled I am having 8 GIG of memory now and I still feel like more
The recursive code uses 2 integers that makes up 8 bytes plus 2 bytes for the return address makes 10 in total. In 1 K of memory that is still a recursion of 102 deep.
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

#### cyberteque

#43
##### Mar 21, 2012, 02:05 pm
I know what you mean!

Some of my code I've run under linux now I have like 10 times the RAM my Mac+ had...

So tell me, how does

Procedure (value:integer);

differ in C from

Procedure Calc(var:value:integer);

In the first case, "value" gets pushed onto the stack, in the second, the address for "value" gets pushed onto the stack and you don't have to worry about making "Calc" a function?
ie

result := Function Calc(value);

The other thing that I'm still coming to grips with is records!

How do I do rcords here in Arduino?

#### AWOL

#44
##### Mar 21, 2012, 02:09 pm
Quote
How do I do rcords here in Arduino?

Do you mean "struct"s ?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up