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!

Question In Regards To

Status
Not open for further replies.

Lookingforhelponline

Technical User
May 10, 2012
5
US
Hello again. I made it back after writing a new code by myself, but I need help once again in executing a function. Pretty much I am trying to do something along the lines of:

Code:
D=x+y+z

DO x=1,20,5
DO y=1,20,5
DO z=1,20,5
ENDDO
ENDDO
ENDDO

MIN(Dall) [if Dall would be all of the values of D calculated]

Where all possible combinations of x,y,z are used from the ranges I provided and all possible values of D are calculated, so that I can have the smallest value as my output from that range of data. My issue is I don't know how to make that happen or actually break the code down further into something else entirely to get the same result.

Thanks in advance for your time and consideration.
 
Code:
program min_xyz                                                                           
                                                                                          
  implicit none                                                                           
                                                                                          
  integer,parameter :: N = 1000                                                           
  integer           :: x, y, z, cnt, D, Dall(1:N)                                         
                                                                                          
  cnt = 0                                                                                 
                                                                                          
  do x = 1, 20, 5                                                                         
  do y = 1, 20, 5                                                                         
  do z = 1, 20, 5                                                                         
                                                                                          
    cnt = cnt + 1                                                                         
                                                                                          
    D = x + y + z                                                                         
    Dall(cnt) = D                                                                         
                                                                                          
  enddo                                                                                   
  enddo                                                                                   
  enddo                                                                                   
                                                                                          
  print *, minval(Dall(1:cnt))                                                                                                                                          
                                                                                          
end program min_xyz

Gordon Shumway
 
Or, a little less heavy on memory, if you just need the minimum value of D and not the complete matrix:

Code:
function min (x, y, z)
    implicit none
    integer :: x, y, z, DMin

    DMin = 2000000000    ! just a very big number 
    do x = 1, 20, 5
    do y = 1, 20, 5
    do z = 1, 20, 5
        DMin = min (DMin, x + y + z) 
    enddo
    enddo
    enddo

    Print *, DMin
end function

Where all possible combinations of x,y,z are used ... and all possible values of D are calculated

... but you do not do this when you select an increment of 5 in the loops (??)

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
What's a funny problem!
The answer is 3.
No need to twist these strange loops ;)

Incidentally, there are only 64 elements in Dall array and max value of x+y+z is equal to 48...
 
Yes of course. I thought that this simple formula was just a placeholder for a more difficult thing.



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top