Thursday, October 30, 2008

Learning Computer Programming - Page 6

We are now getting a feel of how to tame the computer. Some important notes:
  • The computer is very foolish. It does not understand any other thing except a few words and symbols. Hence, while reading a programming book, use only the words and symbols taught, and do not assume any thing more.
  • The computer does not know what it is doing. It is entirely up to you to write whatever statements you need and get the work done by the computer.
  • We have chosen the names of variables as sum_till_now and next_number, but these could be anything of your choice.
  • Empty lines or spaces in the program is just for the nice look and feel. It does not mean anything to the computer.
Okay. Now, if the problem would have been to add from ¨1 to n, n to be available at the time of execution,¨ the program would be something like:

1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now = 0;
5. int next_number = 0;
6. int last_number;
7.
8. printf(¨What is the last number?¨);
9. scanf(¨%d¨, &last_number);
10.
11. while (next_number < last_number)
12. {
13. next_number = next_number + 1;
14. sum_till_now = sum_till_now + next_number;
15. }
16.
17. printf(¨The result is %d\n¨, sum_till_now);
18. }

Statement 9 reads a number from input and puts it in the variable last_number. If you execute the program, it will ask for the last_number, which you have to normally type from the key board.

Now that the scanf statement is in your bag, let's attempt a program for adding a few numbers to be input at the time of execution. Program should stop adding and display the result when 0 is input. The program is given below.

1. #include <stdio.h>
2. int main()
3. {
4. int next_number = 1;
5. int sum = -1;
6.
7. while (next_number != 0)
8. {
9. sum = sum + next_number;
10. printf(¨Enter the next number?¨);
11. scanf(¨%d¨, &next_number);
12. }
13.
14. printf(¨The result is %d\n¨, sum);
15. }

Conclusion

You now have got a feel of what programming is and how to tame it. Remember that this was just to show you the approach. You have a long and fascinating way to go. While studying programming, give enough time to understand all the constructs you come across, and never skip a single word from your programming book.

Happy programming! Please post all your queries and feedbacks at http://groups.google.co.in/group/sanjaypatel?hl=en

Please give a little effort to recommend this article to all who need this.

Previous

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6

Learning Computer Programming - Page 5

Coming back to our program. Let's try to execute it manually, using our old rough pad. After statement 4, our rough pad looks like this:


After statement 5, our rough pad becomes:


Now to statement 7. It is a while statement. The condition is next_number < 10, which is true because next_number is now 0 (see our rough pad). Because the condition evaluates to true, execution enters the next code block, i. e. to statement 9. After statement 9 is executed, our rough pad looks like:


Then comes statement 10. After statement 10 is executed. The rough pad looks like:


Now, after statement 10 is executed, execution again continues from the while statement, i. e. statement 7.

In statement 7, the condition evaluates to true, because next_number is now 1, which is less than 10. So, execution enters into the next block, i. e., statement 9 is executed. After execution of statement 9, the rough pad looks like this:


Then, statement 10 is executed. Then execution goes to the while statement, i. e. statement 7.

If you carry on this process, after sometime, you will find that the condition will become false. The rough pad would be looking like this then:


At this point, the next code block, i. e. statements 8 through 11 will be skipped and execution will continue from statement 12. Statement 12, being blank, statement 13 will be executed, which will display sum_till_now, which contains the desired result.

Done! This explains the use of loop. If now you are asked to write a program to add up to 10000, or 1000000, you need only to change at one place, i. e. in the condition in the while statement.

Previous | Next

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6

Monday, October 27, 2008

Learning Computer Programming - Page 4

You must have observed that many steps in this program are similar and repetitive. Particularly, statements 7 to 14 are quite similar. It would be great if we could express it in some better way. Something like below.

1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now;
5.
6. sum_till_now = 1 + 2;
7. Repeat the step below till we have added 10:
8. sum_till_now = sum_till_now + 3;
9.
10. printf(¨The result is %d\n¨, sum_till_now);
11. }

See statement 7 above. We have expressed it in plain English, which the computer does not understand. What it understands is something perfectly formal, as below:

1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now = 0;
5. int next_number = 0;
6.
7. while (next_number < 10)
8. {
9. next_number = next_number + 1;
10. sum_till_now = sum_till_now + next_number;
11. }
12.
13. printf(¨The result is %d\n¨, sum_till_now);
14. }

Statements 7 introduces the while statement. Let's understand how a while statement works.

Whenever the while statement is executed, the condition in the statement is evaluated. If it is true, the code block (i. e. the set of statements in the curly braces) following the while statement is executed. After this, in stead of moving to the next statement, the execution again continues from the while statement.

If the condition in the while statement evaluates to false, the following code block is skipped, i. e. execution continues at the next statement after the code block.

Previous | Next

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6

Learning Computer Programming - Page 3

Now, let´s teach this to a computer, i. e. write a computer program for this, using the C language. The program is given below. Note that the line numbers are given just for our reference. They are not part of the program.

1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now;
5.
6. sum_till_now = 1 + 2;
7. sum_till_now = sum_till_now + 3;
8. sum_till_now = sum_till_now + 4;
9. sum_till_now = sum_till_now + 5;
10. sum_till_now = sum_till_now + 6;
11. sum_till_now = sum_till_now + 7;
12. sum_till_now = sum_till_now + 8;
13. sum_till_now = sum_till_now + 9;
14. sum_till_now = sum_till_now + 10;
15.
16. printf(¨The result is %d\n¨, sum_till_now);
17. }

Let's understand the logic.

We shall not dig into the syntax, or grammar of C, and just try to understand the logic.

A program is executed step by step, from top to bottom.

Statement number 4 is similar to your drawing a box named sum_till_now in rough pad. Computer uses its internal memory as its rough pad. sum_till_now is called a variable. Similar to a box in your rough pad, a variable can hold a value.

See statement 6. It is called an assignment statement. ¨=¨ is called the assignment operator. When an assignment statement is executed, whatever is in the right side of ¨=¨ is evaluated and put into the variable in the left side. That means, after executing statement 6, the value of sum_till_now becomes 3.

Statement 6 can be read as ¨1 + 2 is assigned to sum_till_now.¨ Never confuse ¨=¨ with the equality operator as in common mathematics. Both are different.

Statement 7 is also an assignment statement. Evaluating the right side (i. e. sum_till_now + 3) results in 6 (remember that sum_till_now is now holding 3). So, 6 is assigned to the left side, i. e. sum_till_now.

Hence, after execution of statement 7, sum_till_now holds 6.

Carrying on, after execution of statement 14, sum_till_now holds 55, which is the desired output.

Done. But the program seems long. What if the problem were to add from 1 till 10000!

Previous | Next

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6

Wednesday, October 22, 2008

Learning Computer Programming - Page 2

Let's write a program for adding the numbers from 1 to 10 (i. e. 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10).

Let's first try to do it ourselves, manually. Be patient not to miss a single step written below. Every word below is quite important.

We shall be using a rough pad and pencil. The rough pad will be used to scribble the intermediate results that we need to remember temporarily while solving the problem. As you shall see, in stead of scribbling on the pad directly, we shall be drawing some boxes and write inside the boxes. We shall give unique names to the boxes so that we can refer those.

So, start following the steps below.

Add 1 and 2 and note the sum in a box in your rough pad. Name the box as, say, sum_till_now. Or you can choose any name you like. So, your rough pad now looks like this:


Add 3 to the value stored in sum_till_now, and write the result back in sum_till_now. Note that as the box sum_till_now can contain only one value at a time, and hence you have to erase or cross the previous value. So, now your rough pad looks like:


Now, add 4 to the value stored in sum_till_now, and write the result back in sum_till_now. After this, your rough pad would look like:


Repeat these steps till you have added 10, and then your rough pad looks as below.


You get the desired result written in sum_till_now.

Previous | Next

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6

Monday, October 20, 2008

Learning Computer Programming

I regard computer programming as the most interesting subject to learn on the earth. But to my dismay, most of the freshers I come across exhibit programming skills much below my expectations.

I think this happens because students miss the right approach to learn programming. Also, surprisingly, most of the programming courses I have seen begin abruptly, without addressing this first.

So, in this article, I will attempt to give a needed start to the students, so that they catch the train smoothly. Everyone who is thinking to learn programming should read this article.

What is Programming?

In simple terms, programming is teaching somebody how to do a task. Obvious examples are
  • teaching a child how to add some numbers
  • teaching a housewife how to prepare some recipe
  • any teaching under the earth that you can imagine
For programming human beings, we use some language, such as English, French, Hindi. For programming computers, we use some language the computers understand. Some examples of computer languages are C, Pascal, Python, Java.

Human beings are very intelligent and hence human languages like English are extremely complex. The same statement might mean different things in different scenario in English. In contrast, computers are the stupidest beings in the world, and so the language used to teach them are quite simple. So, for a human being like you, learning a computer language is really easy and fun. And, once you know one computer language, learning another is as easy as knowing few more words.

Enough theory. Now, let´s dive into and attempt some computer programs. We shall be using the C language. However, you need not know C. Of the programs that I shall be presenting, just focus on whatever we discuss and ignore the undiscussed parts. Our objective here is not to learn programming, but to learn the approach to learning programming, so that you can learn programming from any other course material smoothly.

Next

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6