Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,109 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,703 people online right now. Registration is fast and FREE... Join Now!




Infinite Loop somewhere in Fortran Code

 
Reply to this topicStart new topic

Infinite Loop somewhere in Fortran Code

bobby19
21 Nov, 2007 - 11:03 PM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 2


My Contributions
Hi, I am writing a function that takes in a real data array and a key partition value, which will rearrange the array so that elements with a lower value than x are placed before the elements with values >= the partition value. It then returns the index of the last element of the array with a value less than the partition value.

We were given some pseudo-code which uses the idea that we start from the left and right and simultaneously switch elements using two loops excited together.

CODE

integer function partition_data( xdata, xmiddle )
    implicit none

   real, dimension(:) :: xdata
   real :: xmiddle !INTENT?

   integer ::  l = 1
   integer ::  r = 16
   real :: temp
    
    do
       if (xdata(l) < xmiddle) then
          if (l == r) then
             partition_data = l
             exit
          end if    
          
             l = l + 1
          
       else
          
          do
             if (xdata(r) >= xmiddle) then
                if (l == r) then
                   partition_data = l - 1
                   exit
                end if
                
                r = r - 1
                          
             else
                
                temp = xdata(l)
                xdata(l) = xdata(r)
                xdata(r) = temp
                exit  
             end if
          end do
          
                
          
       end if
    end do
          
  end function partition_data


With the xdata array

xdata = (/ 0.00392, 0.0251, 0.453, 0.667, 0.963, 0.838, 0.335, 0.975, &
0.796, 0.833, 0.345, 0.871, 0.0899, 0.888, 0.701, 0.735 /)

The xmiddle was set to 0.5 in the main test program.

When I compile, I seem to get an infinite loop and I cannot find where it is.

If anyone can spot where I went wrong, that would be great.

Thanks in advance!

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Infinite Loop Somewhere In Fortran Code
22 Nov, 2007 - 07:50 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well think about this: what is your exit condition?

I only see one way to get out of the outer loop:
CODE
       if (xdata(l) < xmiddle) then
          if (l == r) then
             partition_data = l
             exit !!This will exit the do-loop
          end if    


course it has been a long time since I last used fortran.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:30PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month