INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

FORTRAN PROBLEM

FORTRAN PROBLEM

(OP)
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"

RE: FORTRAN PROBLEM

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 

RE: FORTRAN PROBLEM

(OP)
Thank you very much it worked!I've been stuck on that for weeks. Cant thank you enough

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close