Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Beauty of Programming I

    Beauty Of Programming I
    ================
    by OkIDaN

    This is just a monologue on beauty of programming. I had this idea to start the series for a while, now here it is. Im not sure next parts will come. But anyway...

    What inspired me to write this? A simple program that does a simple job, prints a spiral of numbers. Like:

    1 2 3 4 5
    16 17 18 19 6
    15 24 25 20 7
    14 23 22 21 8
    13 12 11 10 9

    Here is the code that does that:

    Code:
    uses crt;
    const
        n=11;
    var
      a:array [1..n*n] of integer;
      b:array [1..n,1..n] of integer;
       n1,k,i,j,d,s:integer;
    begin
        clrscr;
        for i:=1 to n*n do
         a[i]:=i;
        for j:=1 to n do
         for i:=1 to n do
           b[j,i]:=0;
        n1:=n;
        i:=0;
        j:=1;
        d:=1;
        k:=0;
    
        while k<sqr(n) do
         begin
    	 for s:=1 to n1 do
    	  begin
    	      i:=i+d;
    	      k:=k+1;
    	      b[j,i]:=a[k];
    	  end;
    	 n1:=n1-1;
    	 for s:=1 to n1 do
    	  begin
    	     j:=j+d;
    	     k:=k+1;
    	     b[j,i]:=a[k];
    	  end;
    	  d:=-d;
           end;
           for i:=1 to n do
    	begin
    	 for j:=1 to n do
    	  write(b[i,j]:5);
    	 writeln;writeln;
    	end;
    end.
    This program was written by my good friend Rajab back in school time. He always was a better programmer than me, and probably now too. Just take a look at the code and try to understand it. Its pretty easy to understand pascal for a programmer with even a little bit of experience. Just try to understand the code, I know it can seem hard to understand, all the incrementation and variable assignations can cause some hard attention to understand the whole code. But take your time.
    I kept this program inside my folders for years just as a nostalgie of those school times when I started with pascal, and since then never looked at it. But when I was browsing these files while ago, I was astonished, surprised, amazed with the beauty and clearness this code shines with. Look at the technique, so creatively found and so easily implemented. Im still like woah at this little piece of code.
    It creates a 2 dimensional array 'var b:array [1..n,1..n] of integer;' to store the numbers there, and starts to manipulate it so easily and so boldly. I will annotate the main loop:

    Code:
    while k<sqr(n) do
         begin
    	 for s:=1 to n1 do
    	  begin
    	      i:=i+d;
    	      k:=k+1;
    	      b[j,i]:=a[k];
    	  end;
    	 n1:=n1-1;
    	 for s:=1 to n1 do
    	  begin
    	     j:=j+d;
    	     k:=k+1;
    	     b[j,i]:=a[k];
    	  end;
    	  d:=-d;
           end;
    This is the heart of the program. What do we got so far till the code? An array 'a' of 121 elements, where a[n]=n in all cases; 2d array b, where b[j,i]=0 in all cases initially (one of the weaknesses of pascal, you cant assign to an undefined, not created variable);and some other variables that we will use during the main part of the code.

    Code:
    while k<sqr(n) do
    begin
    ...
    end;
    This is how loops are declared in pascal. If you want to do more than one operation you have to state the begin and end points. This loop will continue until k = or > than 121, thus while k<121. Anyway:

    Code:
    for s:=1 to n1 do
    	  begin
    	      i:=i+d;
    	      k:=k+1;
    	      b[j,i]:=a[k];
    	  end;
    This loop will proceed for "n1 times", thus 11 times. Lets walk through the code watching the variables change:

    i:=0+1=1
    k=0+1=1
    b[1,1]=a[1]=1
    -next loop
    i:=1+1=2
    k:=1+1=2
    b[1,2]=a[2]=2
    This continues till:
    b[1,11]=a[11]=11

    I think some of you are still not getting it, just look. Remind a chess board, of 8 horizontal lines which are marked from 1 to 8, and 8 vertical lines which are marked from a to h. Now b[j,i] is a coordinate in chess board where j is horizontal line, and i is vertical line. There is only one difference, horizontal lines are reversed. In place of 8 - the upper line in chess board now it is 1, in place of 7, it is 2. And the size of the board here isnt 8x8 but 11x11. And all of the fields in the board are storing a value, like chess fields store pawns, rooks, etc. This is a simple example of 2d array.
    In the upper code we assigned data (numbers) to all of the fileds in first (j=1) horizontal line. Now we are going on to second line...Hah! Gotcha! How do you think we will assign the next number to lets say b[2,1], what will the number be there? Surely not 12, because it is a spiral, and the field that has to store number 12 isnt b[2,1] but is b[2,11]! And then b[3, 11] will be 13, etc. This means we are going to flip to go down the last vertical (h in chess, 11 in here) and assign values to its fields. I hope Im clear enough, i just love this code so much. So, here is how we do it:

    Code:
                  n1:=n1-1;
    	 for s:=1 to n1 do
    	  begin
    	     j:=j+d;
    	     k:=k+1;
    	     b[j,i]:=a[k];
    	  end;
    Uhmm, what is that n1:=n1-1; line there? I wonder what could that mean...OH! I got it, in the first loop we had to assign values to 11 fields to the right, so we had to take 11 steps. That is why the loop continued only 11 times. Now it will continue 10 times. Because we have already assigned the upper right field (b[1,11]), now we have to go only 10 fields down. Lets follow the code:

    j:=1+1=2;
    k:=11+1=12;
    b[2,11]=12;
    Heh, now you seem to start understand what it is all about, right? I remains 11 while j increases.
    j:=2+1=3;
    k:=12+1;
    b[3,11]=13;
    And so on, until the lower right field is set:
    b[11,11]=21;

    Now come the line that is the heartbeat of the program, this is the line that made me go whooah again and again:

    Code:
    d:=-d;
    This is the line that came from the fingertips of a mans so creative so taleented. It just decreases the program size by approximately two other loops, saves space, makes the program run faster, makes it clear, beautiful and just great! This is a simple variable assignation, but so creative and so in place.
    What was d? d was always 1, now it is -1. And the heart loop sees that k is still not 121, but some 21 and goes onto next loop. Lets follow everything from the start again:

    Code:
    for s:=1 to n1 do
    	  begin
    	      i:=i+d;
    	      k:=k+1;
    	      b[j,i]:=a[k];
    	  end;
    Loop will continue 10 times, as we still have 10 empty fields in this horizontal, yes we are going on assigning values to the bottom horizontal, we are just following the spiral:
    i:=11+(-1)=10;
    k:=21+1=22;
    b[11,10]=22;
    Now look at this beauty, we are going left on the bottom horizontal just like in spiral. Beauty of programming and creativity of talented mind make me crazy everytime. This loop will continue till:
    i:=2+(-1)=1;
    k:=30+1=31;
    b[11,1]=31;

    This last one assing to the left-bottom field of our 2d board the next number in incrementation trying to form a spiral. Now lets look at the next part one more time:

    Code:
    	 n1:=n1-1;
    	 for s:=1 to n1 do
    	  begin
    	     j:=j+d;
    	     k:=k+1;
    	     b[j,i]:=a[k];
    	  end;
    	  d:=-d;
    n1 becomes 9 as there is one less space now to be written in current horizontal/vertical. The deincrementation of n1 is not accidentally place right between the loops, not before or after any of them. Coz the only deincrementation happens when going either down or up vertically from the previous horizontal assignations. It happens so because the starting point of the spiral was a horizontal move, and we filled horizontal line. Just think about it. So:
    j:=11+(-1)=10
    k:=32;
    b[10,1]:=32;
    Now are are going upward on the first vertical line (a in chess).
    j:=10-1=9;
    k:=33;
    b[9,1]:=33;
    This goes on till:
    b[2,1]=40;
    2,1 because if you remember we have our b[1,1] board written and we wouldnt like to overwrite it, coz it can **** up everything.
    Now again,
    d:=-d;
    This changes d into +1 again, Now its time to go right and then down, which need +1 incrementation, wheras when the program will start going back after one more loop left and then up it will use -1 again.

    That is mainly what I wanted to tell you about the beauty and creativity of my friends code that was written 3-4 years ago in my high school.

    Now, I couldnt sit tight, coz I love programming and always love to test my skills upon basic programming which needs a lot of thinking and creativity. I decided to rewrite Rajab's code...in perl...and not with 2d array...but with 1 dimensional normal array. Yeah, it took me a few hours to write a small program. 90% of time I was just staring at the monitor and thinking. And it is smaller than the 2nd one. Though, taking in mind that pascal needs all variable declarations before their assignments I would fairly say that mine and my friends program are nearly the same size. So, here it is:

    Code:
    my @spiral;
    my $n=0;
    my $i=1;
    my $loop=9;
    my $l=1;
    
    while ($l <= 81){
      $x=$n+$loop*$i; #$x=9
      while ($n != $x){
        $n=$n+$i;
        $spiral[$n]=$l;
        $l++;
      }
      if ($i>0){$i=$i+8;}else{$i=$i-8;}
      $loop--;
      last if ($l > 81);
      $x=$n+$loop*$i; #$x=81; $n=9;
      while ($n != $x){
        $n=$n+$i;
        $spiral[$n]=$l;
        $l++;
      }
      if ($i>0){$i=$i-10;}else{$i=$i+10;}
      #$n=81; $i=-1; $x=72 - next loop ^
    }
    
    $i=1; $n=1;
    while ($i <= 81){
      print "$spiral[$i]\t";
      if ($n == 9){
        print "\n\n";
        $n=0;
      }
      $i++; $n++;
    }
    Now I leave this one to your own brain, I think it will be harder to understand variable manipulations here. Coz in the end it needs more variables to support the code than the one with 2d array.
    Here are web-links to programs stated here:
    http://okidan.binaryshadow.org/spiral.pas.txt - the pascal source code
    http://okidan.binaryshadow.org/spiral.exe - the pascal executable, I think will work in all windows.
    http://okidan.binaryshadow.org/spiral.pl.txt - the perl source code

    Hoped you liked my monologue, and I hope to continue the Beauty of programming series in the meantime. You can get this paper from http://okidan.binaryshadow.org/beauty1.txt as well. Visit my website for my perl programs and other stuff: http://www.freewebs.com/okidan/

    I devote the first part of the series to my friend Radjab, havent seen him so long.
    Last edited by OkIDaN; 02-08-2005 at 10:34 PM. Reason: Firstly removed by the author after Mhtml's post. Then readded after WA's post.

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Errr, that's supposed to be complicated? I guess if you had only just began to program sure, maybe. What's the point of this thread? Seems like pointless self promotion to me.
    Omnis mico antequam dominus Spookster!

  • #3
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Errr, that's supposed to be complicated?
    Seems like you couldn't really read through the last perl program and understand the variable manipulations. It really seems so...

    I guess if you had only just began to program sure, maybe.
    Seems like pointless self promotion to me.

    What's the point of this thread?
    Sharing my thoughts and code about the beauty of programming.

    Seems like pointless self promotion to me.
    I 'don't' respect your opinion.

  • #4
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Hi guys:
    I think this is just a case of misunderstanding. I'd like to apologize on behalf of Mhtml if he was mistaken about the intent behind your post. Misunderstanding, it happens.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #5
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, WA. Im reposting.

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    No need to appologise for me George, if I misunderstood the intention than I appologise myself.

    Seems like you couldn't really read through the last perl program and understand the variable manipulations. It really seems so...
    I see, well the thing about that is I myself have been programming since 1993. That's roughly 12 years, 2/3 of my life. You see my dad is a graduate of physics and maths, computer programming (it was basically just C and Fortran IV back then) was a required skill in those courses so once I showed an interest in programming I had an experienced programmer right there to help me on my way.

    I have spent nearly 2 years now on a single project, it's an RTS game engine and a promotional game for the engine. I've written it entirely from scratch in C++, Assembly (mainly for manipulation of the GPU) and my own scripting language developed specifically for the engine. Keep an eye for it in the later half of this year, it'll be my ticket into a large software house for sure. Could you possibly do that on your own? Do you know where to start? All of it from scratch, the API abstraction, the physics, rendering, AI, scene management?

    I don't like people telling that I can't possibly find something easy because they don't find it easy.
    Omnis mico antequam dominus Spookster!

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by Mhtml
    I have spent nearly 2 years now on a single project, it's an RTS game engine and a promotional game for the engine. I've written it entirely from scratch in C++, Assembly (mainly for manipulation of the GPU) and my own scripting language developed specifically for the engine. Keep an eye for it in the later half of this year, it'll be my ticket into a large software house for sure.
    Sounds interesting but difficult, I'd like to hear more about this

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    No need to appologise for me George, if I misunderstood the intention than I appologise myself.


    I see, well the thing about that is I myself have been programming since 1993. That's roughly 12 years, 2/3 of my life. You see my dad is a graduate of physics and maths, computer programming (it was basically just C and Fortran IV back then) was a required skill in those courses so once I showed an interest in programming I had an experienced programmer right there to help me on my way.

    I have spent nearly 2 years now on a single project, it's an RTS game engine and a promotional game for the engine. I've written it entirely from scratch in C++, Assembly (mainly for manipulation of the GPU) and my own scripting language developed specifically for the engine. Keep an eye for it in the later half of this year, it'll be my ticket into a large software house for sure. Could you possibly do that on your own? Do you know where to start? All of it from scratch, the API abstraction, the physics, rendering, AI, scene management?

    I don't like people telling that I can't possibly find something easy because they don't find it easy.

    My cousin owns the most popular open source graphics engine He's pretty clever I'll follow in his footsteps.... one day

  • #9
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see, well the thing about that is I myself have been programming since 1993. That's roughly 12 years, 2/3 of my life.
    I have been programming for a few years, and im much younger than you. And I dont like when professionals walk around and comment everything as "easy" and useless just because they have 12 years of experience.
    I have spent nearly 2 years now on a single project, it's an RTS game engine and a promotional game for the engine. I've written it entirely from scratch in C++, Assembly (mainly for manipulation of the GPU) and my own scripting language developed specifically for the engine.
    That is pretty good. I am new to the world of programming when compared to you. But programming isnt my work, i didnt study it in high school or university. I learned it by myself, It is more like a hobbie that im too much bound to. Neverthless, I have already made a few steps, written my own original chess engine in perl, not even taking a look at any of the existing chess engines in C. I don't think there was one for perl before that. And I have written it, developed the idea and all of the algorithms myself. Yeah, perhaps its not much impressive to YOU (you are a professional, you know), but this is what I counted as my first step into the world of programming. I have created a console based online one-one-one strategy game many other things, where finding ways of implementing the idea was much harder than actually doing it. Were your first steps like that? Could you do it?
    Could you possibly do that on your own? Do you know where to start? All of it from scratch, the API abstraction, the physics, rendering, AI, scene management?
    Please, don't try to frighten me with terms. Im not the one you are thinking I am. And yes, if I was in professional programming, if I wan't spending half of my lifetime at work, if I finished a software engineering department somewhere aboard, I would probably have completed much more efficient and harder projects. And what have you been doing all those 10 years...

    I don't like people telling that I can't possibly find something easy because they don't find it easy.
    I don't find easy what? I've stated in the beginning that im basing the whole article on a simple program. This is more a philosophical paper than a technical. But regarding the last perl code, which creates a spiral based on a 1d array, I can tell that the variable manipulations are pretty difficult to understand even for an experienced eye (you are an exception, of course). It is one of those that are easier to create than to read and understand.

  • #10
    Senior Coder
    Join Date
    Nov 2003
    Location
    Minneapolis, MN
    Posts
    2,879
    Thanks
    2
    Thanked 65 Times in 56 Posts
    Regardless of the reasons posted, I have to say that is one entertaining read. I know nothing at all about programming (except I did get a decent handle on BASIC way back in the day) and even I'm in love with Pascal now.

  • #11
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    I have spent nearly 2 years now on a single project, it's an RTS game engine and a promotional game for the engine. I've written it entirely from scratch in C++, Assembly (mainly for manipulation of the GPU) and my own scripting language developed specifically for the engine. Keep an eye for it in the later half of this year, it'll be my ticket into a large software house for sure.
    Seems like pointless self promotion to me.

  • #12
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    This is ridiculous. Nobody at these forums needs to show off to prove their point. Mhtml, your post was irrelevant to the thread. I have no doubt half the people here are genius, and the other half are pretty darn bright. There is absolutely no need to try to prove it, or to put others down when they want to share something they think is cool. I'm sure when you first started programming, something like what Okidan posted was fascinating.

    In any case, let's lose the hostility and egoism (everybody here, not just one person), and realize that we're all here to learn; whether you've been learning for 12 years or 2 days doesn't make a difference.

    Your friendly moderator,
    jkd

  • #13
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Geez, can't anyone see the similarities between my post and the original? There was an underlying point to my post. I'm hardly going to hijack the thread and go into my own abilities, I was just making a less elaborate self plug in terms of what I understand which is exactly what the original poster has done, the addition of "check out these documents on my website" in this post made it sound more like a self promotional thing which is where this started. He ranted about how great everything was and I disagreed, am I not allowed to have my own opinion? Am I not allowed to back up my opinion? I'm far from a genius or professional, I'm only 18 and have no formal qualifications at all, and I'm pretty sure that I stand nowhere near yourself Jason in terms of ability. But this isn't what I started to say in this thread, which is that I had thought it was a self plug for his website that's all, which I guess everyone else seems to not believe. If he didn't try to tell me I couldn't possibly have understood it then it wouldn't have happened, granted I guess I'm supposed to be the bigger man and just walk away or just not get into these situations to begin with. But you have to admit I'm not the first or the last of the "senior" members who will get into a situation like this.
    Last edited by Mhtml; 02-09-2005 at 04:20 AM.
    Omnis mico antequam dominus Spookster!

  • #14
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had thought it was a self plug for his website that's all
    Are you just ignoring the whole article just because it has a link to my site at the bottom? And anyway, im not selling anything or promoting any propaganda. I am programming free, open source stuff and put it on my website to share. And that is why I have the right to put a link back to my website if im sharing my thoughts, my knowledge. And I dont care if you appreciate it or not.

    I'm supposed to be the bigger man and just walk away
    You wont be a bigger man if you go away. Because I didn't start it. You did. And you would be a much bigger man if you didnt make two pointless posts in this thread.

  • #15
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Seriously! Someone writes a Good article and the first reply he gets is
    Code:
    Errr, that's supposed to be complicated?
    Thats just not right.... As well as I also think many did find it a bit complicated, not everyone has been programming since they were 5.
    <JPM />


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •