How to Arrange Numbers in a Descending Order

Arranging numbers in an ascending or descending order is a really simple task. You can use them to create a list of top and bottom scorers. All you have to do is remember a few small tricks. Take a look.
  • First, we need to read the numbers at the time of execution by adding inputs manually or creating an input file. The data file really helps when there are too many numbers to sort.
  • We set a variable called flag to 0. Then run a do loop to check each number from #1 to number #count-1 against its consecutive number, where count is the total numbers of numbers. If a number is smaller than its consecutive number, we switch them., Notice that this loop has to keep running till the desired order has been reached. Therefore, the flag variable is set to 1 every time a number in a wrong place is encountered. Once all numbers are in proper order, the flag resets to 0 and we exit the loop.
  • For an ascending order, simply reverse the inequality signs.
  • Optional: To read inputs from a file really saves a lot of work. For this, I created a .dat file with all my numbers. To create such a  file, simply make a text file with only a number in each line. Make sure there are no extra spaces before or after the numbers. Then save it and rename it as descendnum.dat or anything else you fancy. The numbers are read from the file by using a do loop. An interesting thing is that the do loop does not know how many numbers a file will have. So, we set a count variable to count this number. We give an arbitrarily large number of repetitions to the do loop (9999 here) and exit the loop once the data file is read by using end=10 in the read statement. This means that the program directly jumps to the statement labelled 10 once all the data is read. This is a very good way to read any fortran file actually as you do not have to specify how many variables you will be reading.I have also added some format statements. But, that again is optional.
Take a look at this video to understand just how many repetitions of the loop can be required.



Now for the Fortran Program,

 !To arrange the given numbers in a descending order
program descendnum
    implicit none
    integer n,a(9999),flag,temp,i,j,count

    !To read upto 9999 numbers from a source file. This part is optional you may substitute this with simple read and write statements to read inputs at the time of execution
    n=9999
    count=0
    open (1, FILE='descendnum.dat')
    do i=1,n
           read (1,*, end=10) a(i)
           count=count+1
    end do
      
         !Displaying the input from the file; you can leave the formatting part
         10 write (*,20)"Displayed below are ",count," numbers read from the file"
         20 format (A,I4,A)
    do i=1, count
        write (*,*) a(i)
    end do

    !The actual program    
    do
        flag=0
        !To swap consecutive numbers in the array that are in reverse order
        do i=1,count-1
            if (a(i)<a(i+1)) then
                temp=a(i)
                a(i)=a(i+1)
                a(i+1)=temp
                flag=1
            end if
        end do
        if (flag==0)  then !Exiting the loop and displaying results
            write (*,*) "The descending order is"
            write(*,*)
            do j=1,count
                write (*,30) "(",j,")", a(j)
                30 format (A,I4,A,I8)
            end do
            exit
           end if
    end do
end program descendnum

Sample Output. Note that equal numbers are displayed one after the other.

No comments:

Post a Comment