Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FORTRAN PROBLEM 1

Status
Not open for further replies.

Somnion11

Technical User
Jul 23, 2014
2
GB
Hello I'm new here i just needed some help with a program i am working at. I take a specific data apply some boundary conditions and then on the results i try to sort it in ascending order. I try to print my results all the way so i can know where I'm wrong if I'm wrong. My problem is that even though my original array doesn't contain any zero elements my sorted one does and a lot of them. Any help would be appreciated. Here is the code

DO k=1,927550
IF (RADIUSabs(k) <= 1000000 .and. RADIUSabs(k)/=0) THEN
count2=count2+1
END IF
END DO
write(6,*) count2
DO k=1,927550
IF (RADIUSabs(k) <= 1000000 .and. RADIUSabs(k)/=0) THEN
RADIUSabs1(k)=RADIUSabs(k)
WRITE(6,*) RADIUSabs1(k)
END IF
END DO
WRITE(6,*) "THIS IS RADIUSABS UNSORTED WITH LESS ELEMENTS"


DO k=1,count2-1
DO m=k+1,count2
IF(RADIUSabs1(k)>RADIUSabs1(m)) THEN
temp=RADIUSabs1(m)
RADIUSabs1(m)=RADIUSabs1(k)
RADIUSabs1(k)=temp
END IF
END DO
END DO


DO k=1,count2
write(6,*) RADIUSabs1(k)
END DO
write(6,*) " =sorted array"
 
Change your initial loop to
Code:
DO k=1,927550
   IF (RADIUSabs(k) <= 1000000 .and. RADIUSabs(k)/=0) THEN
      count2=count2+1
      RADIUSabs1(count2)=RADIUSabs(k)
   END IF
END DO
write(6,*) count2
And remove your second loop. Say you counted 20 non zero values. What the second loop does is it loops through the first 20 values copying the non zero values to the second array in the same index positions so you not only have zero values in the same positions, but everything beyond position 20 will retain their original value (which, in your case is probably zero).

The code for your selection sort is OK but not the most efficient way of doing it - it would be better if you only did one exchange at the end of the selection
Code:
DO k=1,count2-1
   ! To start, assume k is the lowest
   low = k
   DO m=k+1,count2
      ! Don't do exchange here - just save the index of the lowest
      IF(RADIUSabs1(low)>RADIUSabs1(m)) low = m
   END DO
   ! Found the lowest: now do the exchange
   IF (low /= k) THEN
      temp=RADIUSabs1(k)
      RADIUSabs1(k)=RADIUSabs1(low)
      RADIUSabs1(low)=temp
   END IF
END DO
 
Thank you very much it worked!I've been stuck on that for weeks. Cant thank you enough
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top