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