Archive | Programming RSS feed for this section

C++ reference

29 May

Here is a free C++ reference for those who want to learn. If you already know c++, and want to make a very quick revision this PDF is an excellent source. It is just 300 pages, so it is very suitable for refreshing your information quickly. If you have never programmed before then this book is not for you.

XML explained in Arabic

22 May

I have created two video tutorials to explain what XML is and how the world has benefited from it. The videos are in Arabic, I hope you like them both.

XML explained (Arabic)

XML applications (Arabic)

Learn to think like a computer, to code like a guru [Part 3]

19 May

The last time I ended my post with two simple questions, and some of you have provided perfect answers, and I was really happy about that. But I still need to make my promise and answer them for you. For the first question which was “What Jazari was going to do if he can’t find resident evil 5 disk on the table?” The answer is that “Jazari was going to hang, since Jazari was not going to be able to execute the command as the disk is not on the table, and as we said before if a computer or a robot failed to execute a command it will hang”

Now for the second question which was “what can we do in this case?” The answer is that we are going to add an extra if command to check if “resident evil 5” is on the table or not, and accordingly Jazari will pick up the disk or not. We will also add an if command at the end of the sequence to check if Jazari is holding a disk in his hand or not and accordingly un-grasp the disk or just say in a metallic voice “Sorry no games found on the table”.

To make it easier to understand I have included the below two figures. The first figure is the command sequence (which we will refer to from now on as pseudo-code, and the second figure illustrates the pseudo-code (this type of diagrams is called flow-chart, so from now on we will use the term flow chart).

Click to enlarge

Let’s explain the pseudo-code, as you can see it looks a bit similar to the pseudo-code we have written in the previous post, As Jazari will move 10 meters, turn left, and then will move 5 meters to reach the table has the games on its top. By this point Jazari will take a look at the table to see if killzone 3 disk is on the table or not, and since it’s not on the table Jazari will execute the commands between “begin else” and “end else”. Which contains another if command that asks Jazari to check if the “resident evil 5” disk is on the table or not. Since the “resident evil 5” disk is not on the table “Jazari” will execute the commands between the “Begin else” and “End else” (line 4.b.1.b.1) which is simply nothing. Finally Jazari will return back to me by moving 5 meters back, turning right, and then moving 10 meters back. At this time Jazari will check if he is holding a disk in his right hand or not. In our case he is not holding any disks so it will execute the commands between “Begin else” and “End else” which is saying using his robotic metal voice “Sorry sir, No games found on the table”.

Robots and computers can execute nothing or simply the “empty command sequence”, the execution of nothing is simply moving on to the next command.

Pretty simple isn’t it? I know that things are becoming a bit more complex now, but the key to understand this is to remember the two rules we have explained so far “Sequence” and “Branching”. Till now we are using them like Mechano pieces, we gather them together to build something useful. So whenever you think its complex look at each mechano piece separately and make sure you understand it very well and move on to the next piece. So take your time to understand each and single command separately, the move to the next command, and don’t forget to use the flow chart to relate commands to each others. keep trying until your mind accepts the way computers and robots think.

Click to enlarge

I think in this example all the pseudo-code is simple and clear except command number four, it looks a little complex. We will need to break it apart to make it easier to explain and understand. Take a look at it in the flow chart you can see that it’s a normal if command with the normal left and right branches (the Begin if, and Begin else). Now look down left branch, as you can see it hold a single “if” command which has its own left and right branches. This was possible because as we said before the “if” command is treated by Jazari as a single command, and since we can put any command we want between “Begin If”, “End if” , “Begin else” and “End else” we can normally put an “if” command without any problems.

When an if command is put between “Begin if”, “end if”, “begin else”, or “end else” of another “if” command, it is called a nested if. Nesting can be up to any level which means that you can have an “if” inside an “if”, inside another “if” which is inside another “if”, etc…

Now as you understand the nested if, let’s agree on a numbering system we will use in our pseudo-code. We will start the count with one, and for each following command we will increase the count by one. This means that if we have four commands we will start by one and end at four. Let’s apply this on our pseudo-code. Please take a look at the diagram and notice that we have eight commands. For the “If” command left and right branches, For the right branch (“begin if” and “end if”) we will add an “a” after the number given to the “if” command and start counting from one for each following command between “begin if” and “end if”. Please take your time to look at the below diagrams to keep following up with me.

Now for any command written between “Begin else” and “End else” (left branch) we will add a “b” after the number give to the “if” command and start counting from one for each following command between “begin else” and “end else”. Again take a look at the diagram to understand what I mean.

Now take a look at the nested if, and make sure you understand its number properly, try to apply the numbering rules mentioned and the common sense. I hope it is simple enough.

What have learned during this post

  1.  A new computer vocabulary: pseudo-code, and flow-chart
  2.  The fact that computers can execute empty commands by doing nothing and moving on to the next command
  3. Nesting concept for the “if” command
  4. How to number the commands in a pseudo-code

 Your assignment
Now as an assignment I want you to write Pseudo-code and draw the flow chart for the following case. I want Jazari to bring me a cold beverage from the fridge, if there was no cold beverage in the fridge Jazari can bring me my favorite “Baklava” which it is in the fridge too, and if was no “Baklava” in my fridge I want Jazari to return back to me and say “Sorry sir, You don’t have any food in your fridge. You might consider to ask for a pizza delivery”.

I can’t stress how important it is to make this assignment, as it will help your mind think like a computer which is needed if you want to be an excellent coder one day. Keep trying an don’t give up easily, even if you think you can’t do it correct, continuous trials will challenge your mind which is really desirable in any learning process. If you faced any trouble with this assignment feel free to contact me.

Learn to think like a computer, to code like a guru [Part 2]

17 May

So I’m back from work and ready to have some fun, I guess I will enjoy some play-station 3 games. Since I have already mastered how to give orders to my robot “Jazari, using the “Sequence” rule, I will ask him to get me “Kill zone 3” disk.

  1.  Move 10 meters forward
  2. Turn 90 degrees left
  3. Move 5 meters forward
  4. Grasp killzone 3 disk from the table using right hand
  5. Move 5 meters backward
  6. Turn 90 degrees right
  7. Move 10 meters backward
  8. Un grasp killzone 3 from right hand

Okay, I will load this  “sequence” of orders into Jazari, and wait for him to bring me the game. Until he is back please read the above steps carefully and make sure you don’t have any problems with them.


Time is ticking

It seems that “Jazari” is taking a bit longer than it should to get me the disk. I will go to check what’s wrong with him. I can see that Jazari is standing right infront of my disks not doing anything at all. what could have gone wrong? I’m pretty sure that the sequence is correct. Anyways I’m standing right infront of my discs, I will grab the disc myself.

Jazari has hanged

I can’t find my “Killzone 3” disc, ohh I have just remembered that I gave it to a friend last week, that’s okay I can play “resident evil 5” for today. Now I understand why Jazari has stopped working. He couldn’t continue the execution of the sequence as he couldn’t execute one of the commands in the sequence.

If a computer or a robot failed to execute a command in a sequence, it will not continue to execute the next commands and it will keep waiting in it’s place forever without doing anything which is known an a hang.

Okay, now we need to think what to do to avoid such a hang. We need something more than just a sequence of commands that are executed from top to bottom. Something that enable us to make Jazari execute a specific set of  commands only under certain conditions. Here comes the second rule we found in Jazari’s manual, “Branching”.

Branching can be defined as the ability to execute a specific set of commands when certain conditions are met, if these certain conditions are not met then this specific  set of commands will never be executed.

Let’s rewrite our order to Jazari  using both rules, sequence and branching. But first let me push the reset button to restart Jazari, and pull it back to my living room.

1- Move 10 meters forward
2- Turn 90 degrees to left
3- Move 5 meters forward
4- If killzone 3 disk is on the table
Begin if
Grasp killzone 3 disc from the table using right hand
End if
Begin else
Grasp resident evil 5 disc from the table using right hand
End else
6- Move 5 meters backward
7- Turn 90 degrees to right
8- Move 10 meters backward
9- Un grasp the disk from right hand

Let’s see what Jazari will do this time. Let me push that button and wait. That has worked, in less than 20 seconds Jazari is back with resident evil 5 disk and has dropped it on my living room carpet.

here we have asked Jazari to move to the table where I put my games, then take a look and see if Killzone 3 disk is on the table or not. then execute the commands between “begin If” and “end if” when killzone 3 disk is on the table otherwise it will execute the commands between “begin else” and “end else”. after that it will return back to my living room and leave the disk from his hand (which could be killzone 3 or resident evil 5 according to the availability of killzone 3 on the table). In our case Jazari has returned with resident evil 5 as killzone 3 is not available on the table.

You might have noticed that branching has changed the order of execution as the command “Grasp killzone 3 disc from the table using right hand” was not executed although it is located before “Grasp resident evil 5 disc from the table using right hand”. 

To better understand branching we will draw an illustrative diagram to show why its called branching and to show the execution direction as it is no longer from top to bottom.

Click the image for a high resolution picture

As you can see the execution is moving from top to bottom, until it reaches the “if” command (The area highlighted in green) by then there are two branches for Jazari to choose which one of them to execute, he checks if the killzone 3 disk is on the table or not. Since I have lent the killzone 3 disk to a friend the execution direction will change to the left branch where Jazari will pick up resident evil 5 disk (which I’m 100% sure its on the table). after then Jazari  will execute the next commands (under the green area) normally in order.

Now what If the killzone 3 was on the table? The execution direction was going to change to the right branch where Jazari was going to pick up killzone 3 disk, then Jazari  will execute the next commands (under the green area) normally in order.

As you can see from the diagram, I have highlighted the branching area with green, also I given the “if” command a single number in the sequence. I did that because the “if” command is treated as one block, which means that it will always be executed. The “if” command execution is simply the execution of either the commands between the “begin if” and “end if” when the condition is correct or execution of the commands between the “begin else” and “end else”, when the condition is not correct.

The if command takes this form, and it must be always written that way, anything written in bold must always be written. as I have just said the “if” command is a single command although it is written on multiple lines.

If certain condition is correct
Begin if
Put here the list of commands you want to execute if the condition is correct
End if
Begin Else
put here the list of commands you want to execute if the condition is not correct
End Else

how can an “if” be a single command while it’s written on multiple lines?
To answer this question I will ask you this question, is the below a single command or multiple commands?

10 meters

What if I wrote it that way on a single line

Move 10 meters forward

Is it a single command or multiple commands?

Well, computers and robots don’t see things the way we humans see, for example computers and robots don’t know what lines are. They don’t even care if a command is written on a single line or multiple lines. In fact computers and robots come from the factory with a limited set of commands that the factory engineers have put into them, for example, we have seen the “move” command that make Jazari moves forward or backward. Where do you think I got move from? I got it from Jazari’s catalog.

Now I’m telling you that Jazari’s engineers have indicated that the “if” is a single command, so from Jazari’s point of view it is a single command. Train your mind to accept this fact as you are here to learn how to think like a computer.

To help your mind accept the fact that “if” is a single command always imagine it as drawn below, As you can see it looks like a single block now surrounded with dotted line. so when you want to use an if command you will just put that block, then write down the commands you want between begin if, end if , begin else, and end else. I hope that helps.

If command

Again remember that when you include the “if” command in a sequence you either include it all or nothing at all. There is no such a thing as an “if” without “begin if”, “end if”, “else”, “begin else”, and “end else”. The max you can do, is to write or not write any commands between “begin if”, “end if”,  “begin else” and “end else”.

Why I’m stressing so hard on that “if” is a single command?

First let’s assume that  “resident evil 5” disk was not on the table.

1- what Jazari was going to do? Why?

2- what can we do in this case?

Think about these questions and i will provide the answer in the next post. But If you were able to answer both questions then congratulations, your mind is starting to think like a computer.

Learn to think like a computer, to code like a guru [Part 1]

15 May

So, you are thinking about becoming a programmer, if so then this post is for you. Many people that I know who have started to learn programming just walked away after a few days from starting to learn. By talking to them they said that programming was very hard to understand and it was very hard for them to build a working application. The problem was that almost all of them have started to learn programming using a high level programming language like C# or Java. They just got overwhelmed from day one with lots of details and jargons that they couldn’t handle.

As I have also noticed that those who were able to survive and continue learning are not able to solve relatively simple programming assignments. From my point of view that was expected. The question is why did that happened to them, and how it can be avoid. For the first question the answer is that they don’t understand the mechanism of how computers think and work, they don’t understand the logic that computers use to think. For the second question the answer is that they first need to learn (in plain English) the computers thinking logic, before they can start to learn any programming language at all.

In this post and the several coming posts (GOD only knows how many of them there will be) I will teach you how computers think. In order to learn the computer thinking logic I have asked a friend of mine to lend me his Robot, as robots think the same way computers think. So please welcome “Jazari” the robot.

Please welcome Jazari

Please welcome Jazari

Now as we have a robot, we need to make use of him, it doesn’t now make any sense to own a robot and not ask him to do our tasks; at the beginning I’m suggesting to ask him to get me some deserts from the fridge. Okay, let me ask him to do.  “Jazari, would you please get me some Baklava from the fridge”, as you can see below “Jazari” is not moving at all. Let me check its manuals.

Ahaaa the manual says that our orders to “Jazari” need to follow three simple rules otherwise it will not work at all, those rule are listed below.

  1. Sequence
  2. branching
  3. Repetition

So what does that mean? For now I will explain what sequence is. A Sequence is an ordered finite list of commands that the robot or a computer will execute one by one from top to bottom. Now let’s see how to ask “Jazari” the same question but in a way he can understand.

  1. Move 10 meters forward
  2. Grasp the fridge door handle with right hand
  3. Pull the fridge door handle
  4. Un grasp the fridge door handle
  5.  Grasp the baklava plate with right hand
  6. Grasp the fridge door handle with left hand
  7. Push the fridge door
  8. Un grasp the fridge door handle
  9. Move 10 meters backwards

Although that might sound very stupid for a human, but for computers and robots it always had to be like that, a sequence of a very primitive commands that explain every single step to complete needed to a task. Let’s assume that I have forgot the command number 8, in this case the robot would have moved backwards while still grasping the fridge door handle, dragging the fridge to my living room.

So for today we have learned that computers and robots are not as smart as we think, we have also learned the correct way to give orders to a computer or a robot. Now as I feel thirsty after eating the baklava, would you please ask “Jazari” to get me a cup of tap water, just write the sequence and send it to me. While writing it don’t focus on the words as much as you focus on simplifying the commands for “Jazari” to be able execute them one by one, and remember that it is a must that each line should contain a single command for “jazari”.