PDA

View Full Version : Beauty of Programming I

OkIDaN
02-08-2005, 12:06 AM
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:

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:

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.

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:

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:

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:

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:

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:

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:

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.

Mhtml
02-08-2005, 03:34 AM
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.

OkIDaN
02-08-2005, 04:25 AM
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.

WA
02-08-2005, 04:57 AM
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. :)

OkIDaN
02-08-2005, 05:07 AM
Ok, WA. Im reposting.

Mhtml
02-08-2005, 05:31 AM
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.

Brandoe85
02-08-2005, 06:51 AM
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.

KeZZeR
02-08-2005, 09:46 AM
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 :p He's pretty clever :D I'll follow in his footsteps.... one day :rolleyes:

OkIDaN
02-08-2005, 11:02 AM
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.

rmedek
02-08-2005, 11:16 AM
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.

aman
02-08-2005, 03:15 PM
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. :p

jkd
02-08-2005, 10:03 PM
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.

jkd

Mhtml
02-09-2005, 04:10 AM
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.

OkIDaN
02-09-2005, 05:30 AM
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.

JPM
02-09-2005, 07:47 AM
Seriously! Someone writes a Good article and the first reply he gets is
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.

jkd
02-09-2005, 07:50 AM
I'll close this thread if the comments remain irrelevant. There was potential behind it initially. Keep the personal comments to pm's or email, not the forum. CF is a friendly community, regardless of who posted flamebait first.

codegoboom
02-10-2005, 12:18 PM
A simple program that does a simple job, prints a spiral of numbers.

What then, may I ask, might one do with this spiral? :o
:D

tboss132
02-10-2005, 02:54 PM
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;

...

I devote the first part of the series to my friend Radjab, havent seen him so long.

Good stuff buddie, good stuff. :thumbsup:
Never mind these "genius seniors"

ghell
02-10-2005, 03:39 PM
What then, may I ask, might one do with this spiral? :o
:Din theory theres a lot you can do with it.. but.. errm... nothing springs to mind.. but then i made something similar to this when i was bored a few years ago and now i use it to change 1d arrays to 2d arrays (which can be pretty handy for string manipulation :))

MHTML you shouldn't really post that someone's post is simple just because you find it simple (well.. the same effect could probably be written simpler).. but i mean.. if you dont have anything nice to say..something comes next.. cant remember what :p

i cant see the point in this thread but i think it is cool and shiney anyway :thumbsup: nice 1 OkIDaN

jakenoble
02-13-2005, 09:48 PM
Respect to the guy from Azerbaijan, nice post.

I am interested in the use of a spiral of numbers?

codegoboom
02-28-2005, 06:38 AM
Spirals: something out of classical cryptography, maybe... ;)

S R E H P
S N A R I
P O R T C
O U T E N
S I T I O

warhammerdude20
03-05-2005, 04:17 AM
An argument with a monitor, good luck winning that fight :D

raf
03-05-2005, 03:31 PM
It's apparently "not done" but i also don't see what's so hard about it. I also don't realy see why this sollution would be so beatyfull.
Simple sollutions for a complex problem are beautyfull.
Complex sollutions for a simple problem aren't.

Anyway, i'd be more intrested in a comparison between your friends code and yours (and maybe some other guys there approach on this problem) in terms of
- performance,
- flexability (a more parametrised version that can handle a variable number of elements (numbers, letters, dots, colours, whatever), shapes, directions etc)
- usability (for encryptionpurposes(computing hashes for the diagonals or using them as matrixes to compute the complement or whatever), for dynamically generating imiges (by replacing the numbers by colours or shapes or whatever)
- ...