May 2021 2 471 Report
Fortran 95: sort numbers from an array in descending order?

Hi,

I have program which sorts the nubers from an array in descending order, and shows v(44) and v(77). The array has 100 numbers, and V(i)= i*(100-i).

I have done it this way:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

program array

implicit none

integer, dimension (100) :: V

integer :: temp, z, j, i,p

V=(/((i*(100-i)),i=1,100)/)

p=0

DO I=1,100

z=0

DO J=1,99

IF (V(J)<V(J+1)) THEN

p=p+1

z=z+1

TEMP = V(J)

V(J)= V(J+1)

V(J+1)=TEMP

END IF

END DO

print*, "there have been", z, " swaps in step", i

END DO

print*, "v44)", V(44)

print*, "v(77)", V(77)

print*, "Total swaps", p

PAUSE

end program array

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The program calculates how many numbers have been swapped in total (p), and in every "sub" do loop: do j=1,99.

I get strange (or maybe not!) results: from step 1 to 49, I get: 97 swaps, 95 swaps, 93 swaps, 91 swaps, etc and then from step 50 to 100, 0 swaps.

Is there anything I've done wrong ? Or is there a better way of doing this program.

Thank you


Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments




Helpful Social

Copyright © 2024 Q2A.MX - All rights reserved.