General Programming


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

  1. Five steps to writing an effective program
  2. Data sorting
  3. Data searching
  4. Recursion

Any questions mail me at general@teentwo.8m.com


1.Five steps to writing an effective program

The art of creating a sucesfull program lies in these five steps :

"Design of the algorithm: This is the stage where the problem to be solved is established and the best solution is proposed, also create schematic diagrams of the different proposals.

Coding the algorithm: This stage consist of writing the program in the programming language.

Translation to machine language: This is the creation of the object program, in other words, the written program as a sequence of ones and zeros that can be interpreted by the processor.

Test the program: This step is underrated andmostly forgotten, after the translation of the program into machine language, test the program.

The last stage is the elimination of detected faults. The correction of a fault normally requires the repetition of all the steps from the first or second."

From the 1996 "Assembler Tutorial of the University of Guadajara"

Any questions mail me at general@teentwo.8m.com


2.Data sorting

Sorting is not used very much in general, but as soon as you start using database related procedures it helps to sort your data in one way or the other.There are a few ways to sort data, the easy, the slow and hard, and the fast way.

  1. Bubble sort
  2. Selection sort
  3. Insertion sort

There are of course other sorting methods like quicksort; mergesort and much more, these sorting methods are more complex.

Bubble sort :

This is the easy sorting method, it compares the first number with the second, if the second is smaller than the first, it swaps them, else it just ignores it, and goes on. It continues to tests the number with the following number.

Ex.
1 5 3 8 7 4 6 9 2
Test the 1 and the 5, The 5 is larger leaves the 1 and continues with the 5.
1 5 3 8 7 4 6 9 2
Test the 5 and 3. The 3 is smaller so it swaps the 5 and the 3.
1 3 5 8 7 4 9 2
Test the 5 and the 8, The 8 is larger leaves the 5 and continues with the 8.
1 3 5 8 7 4 9 2
Test the 8 and 7. The 7 is smaller so it swaps the 8 and the 7.
1 3 5 7 8 4 9 2
Test the 8 and 4. The 4 is smaller so it swaps the 8 and the 4.
1 3 5 7 4 8 9 2
Test the 8 and the 9, The 9 is larger leaves the 8 and continues with the 9.
1 3 5 7 4 8 9 2
Test the 9 and 2. The 2 is smaller so it swaps the 9 and the 2.
1 3 5 7 4 8 2 9
It is the end, it goes back to the first number.

Now it has stepped through all the numbers once, these steps are continued till there are no more numbers left to swap. Here are the rest of the steps.

1 3 5 4 7 2 8 9
1 3 4 5 2 7 8 9
1 3 4 2 5 7 8 9
1 3 2 4 5 7 8 9
1 2 3 4 5 7 8 9

Selection sort :

This sorting is easy to understand but harder to program. Selection sort seeks the smallest element and places it in front. Now the first number is sorted. It continues by the second number and goes through the array looking for the smallest one and swap them, now the third number, and so forth.

Ex.
5 3 8 7 4 6 1 9 2
Looks for the smallest and swaps it with the first one.
1 3 8 7 4 6 5 9 2
Now the first number is sorted so it starts by the second one and looks for the smallest and swaps it with the second one.
1 2 8 7 4 6 5 9 3
1 2 3 7 4 6 5 9 8
1 2 3 4 7 6 5 9 8
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 7 9 8
If the smaller number is on the correct place it just leaves it there.
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

As you can see this method of sorting is much faster than bubble sort since it only has to go through all the numbers once to look for the place number.

Insertion sort :

Insertion sort starts by comparing the first two elements and then places the smallest one in the correct position. Then it looks at the first 3 elements and puts the smallest one in the correct position relative to the previous numbers. It does this by moving the element on the right to the left until it reaches the correct position.

Ex.
3 4
1 7 6 0 9
Find the correct position for 4. Now the first 2 numbers are sorted
3 4 1
7 6 0 9
Find the correct position for 1.
3 1 4 7 6 0 9
1 3 4 7 6 0 9
Now 1 is in the correct position. Now the first 3 numbers are sorted
Find the correct position for 7
1 3 4 7 6 0 9
Find the correct position for 6. Now the first 4 numbers are sorted
1 3 4 6 7
0 9
Find the correct position for 0. Now the first 5 numbers are sorted
0 1 3 4 6 7 9
Find the correct position for 9. Now the first 6 numbers are sorted
0 1 3 4 6 7 9
Now all numbers are sorted

This is the fastest sorting method of all since it puts a number in its correct position the first time.

Any questions mail me at general@teentwo.8m.com


3.Data Searching

Before you can start searching for something all the elements has to be sorted. There are two methods of searching namely:

  1. Linear Searching
  2. Binary Search

If the array of elements you are using is small (1...20) it is faster to use linear searching but when you use a large array (21...1000) it is better to use binary searching.

Linear Searching

Linear searching starts from the front of the elements and starts comparing all the numbers until the number has been found.

Ex. You have to find 5

1 3 5 6 12 15 18 19 25 33 35 39 41 43
Check the first element.
[1] 3 5 6 12 15 18 19 25 33 35 39 41 43
It is not the correct number check the 3
1 [3] 5 6 12 15 18 19 25 33 35 39 41 43
It is not the correct number check the 5
1 3 [5] 6 12 15 18 19 25 33 35 39 41 43
It has found the number in 3 steps.

Binary Searching

Binary Search goes to the middle of all the elements and then checks if the middle element is the correct one if not, it tests if the correct one is smaller or bigger than the middle one. If it is smaller it goes to the middle of the smaller half, if it is bigger it goes to the middle of the bigger half. This process is repeated until the element is found.

Ex. You have to find 5
1 3 5 6 12 15 18 19 25 33 35 39 41 43
First checks the middle element
[1 3 5 6 12 15 16] 18 [19 25 33 35 39 41 43]
Five is smaller then 18 so it goes to the smaller half
[1 3 5] 6 [12 15 16]
Five is smaller then 6 so it goes to the smaller half
[1] 3 [5]
Five is larger then 3 so it goes to the larger half
5

This is a very effective method of searching but unfortunately it only works if all the elements are sorted.

Any questions mail me at general@teentwo.8m.com


4.Recursion

Recursion is a concept. The word recursion is from the verb to recur, wich means to occur again, to repeat. A procedure or function that calls itself is said to be a recursive procedure or function.

When you started programming you were probably told not to us a function name with in itself, causing you to rule out any possibility of recursion making it a hard concept to understand.

Recursion is used in math quite often ex. n!
n! = 1 if n=0
else
n! = n * (n-1) * (n-2) * (n-3) * ... * 1 if n>0
Consider 4!, because n!>0 we use the second part of the definision
4! = 4 * 3 * 2 * 1 = 24

This description of n! provides a different definition for each value of n. That is the definition of 2! is 2 * 1, the definition of 3! is 3 * 2 * 1 and so forth.
Thus n! can be expressed as n! = n * (n-1)! wich is using recursion.
4! = 4 * (4-1)! = 4 * 3! = 4 * 3 * (3 - 1)! = 4 * 3 * 2 * (2-1)! etc.

Of course recursion is not limited to math. Computer languages such as Pascal that support recursion gives the programmer a powerful tool to solve certain problems by reducing it's complexity.

When writing a recursive procedure or function there are 2 rules to keep by...
Never use it in a LOOP!
Always make sure there is some value at wich the recursion will stop.
IF you keep by theses rules you will never go wrong in recursion.

Any questions mail me at general@teentwo.8m.com

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Tuesday, June 27, 2000

1