A computer program works the same way a child learns to carry out complex mathematical sums from kindergarten to intermediate classes. By acknowledging the process, we can easily understand the concept using which a computer program can be written. Arranging numbers in ascending or descending order are what a child learns in the starting years of schooling. To be able to arrange them in a specific order, he must know the way they must be ordered. In other words, he should know how to differentiate between numbers and tell which one is bigger from the other.
Ascending order
After this, one picks the first number from the series and identifies the smallest of all. To do this, he compares each other number in the given data with the first number one by one. If the given number is the smallest of all, its position is left unchanged and the same procedure is repeated for the second word (leaving the first number).
However, if there is a number smaller than the first number, places of both numbers are interchanged (the smaller number is placed in the first place). The process is again repeated for this number until the smallest number occupies the first place. The procedure is repeated for the second number in the sequence in a similar manner until all the numbers in the data have been arranged.
So, let us write the FORTRAN program to order the numbers in ascending order using this simple procedure:
PROGRAM ASCORDER IMPLICIT NONE INTEGER i, j, n REAL T DIMENSION A(100)
3rd and 4th lines show type specification commands that specify the data types of i, j, n, and T. T is a temporary variable that we will use in the program. Dimension statement here creates an array of 1-dimension capable of holding 100 entries. After this program prompts the user to enter the total number of entries for sequencing, n.
WRITE (*,*) 'Type the value of n' READ (*,*) n
Now make the program ask the user to enter the numbers.
WRITE (*,) 'Type the numbers' READ (*,*) (A(i), i=1,n)
After this run a DO loop from i=1 to n-1 and another from j=1 to n
DO i=1,n-1
DO j=1,n
IF (A(i) .GT. A(j)) THEN
A(i) = T (store A(i) as a temporary number T)
A(i)=A(j) (Let new A(i) be equal to A(j), i.e., smaller number)
A(j) = T (Let the space previously occupied by smaller number given to lagrer number)
END IF
END DO
END DO
Descending order
The above program remains same for descending order. Only change that appears is that we look for the greatest number rather than the smallest one.
DO i=1,n-1 DO j=1,n IF (A(i) .LT. A(j)) THEN A(i) = T (store A(i) as a temporary number T) A(i)=A(j) (Let new A(i) be equal to A(j), i.e., greater number) A(j) = T (Let the space previously occupied by greater number given to smaller number) END IF END DO END DO
No responses yet