Pages: 1 2 [3] 4 5   Go Down
Author Topic: Every so often I am stunned by a post.  (Read 4314 times)
0 Members and 1 Guest are viewing this topic.
Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19035
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For me, the most peculiar thing about C is that the missing condition in
Code:
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.
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

LOL!

So, what's your point?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do this kind of recursion in C?

Code:
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);
    End
End;{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.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26471
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Recursion is simple, but it can be a RAM-hog, so beware.
Code:
unsigned long factorial (unsigned long n)
{
  return (n == 0) ? 1 : factorial (n - 1)  * n;
}
Logged

"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.

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1922
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
    End
End;{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.


Logged

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 -

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1922
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-eek
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
Logged

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 -

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26471
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I do rcords here in Arduino?
Do you mean "struct"s ?
Logged

"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.

Pages: 1 2 [3] 4 5   Go Up
Jump to: