Print

Print


Hi there,

I am just getting acquainted with Fortran, and I have a problem for which I was unable to find a solution anywhere. The task is very easy. Suppose we have a grid, and a number, and we need a subroutine gives us the index of the gridpoint that is closest to this number. For example:

   grid= (/1,2,3,4,5/)
   x=2.8
   call num2ind(x,grid,indx)

This should put 3 into indx whre indx is a scalar. So far this is piece of cake. But now suppose that I want this procedure to work with arrays as well. For example:

   x_array=(/1.2, 2.8, 4.3/)
   call num2ind(x_array,grid,indx)

should put (/1,3,4/) into indx where indx is a conformable array.
Now the problem is that I want to use this subroutine for both scalars and arrays. First I thought that the ELEMENTAL statement is exactly what I need, but it requires that all dummy arguments be scalars. But in my case the dummy argument for grid needs to be an array. So I would need something like a partial elemental function, where the first and last arguments are elemental, but the middle one is "fixed".

I would very much appreciate any idea - other than write a separate subroutine for scalars and arrays:)

Thanks,
Andras