I reported the definite bugs; but the remaining one is just me, I think. Apparently something I thought was equivalent is not, as I get the same error with three compilers. I will figure it out when I have time. The exercise has been fun and productive, but I am apparently having a brain burp. I thought both of these sorts would work. I know it will be something simple but I am not seeing it:
program main
! bug: ifort calls sorti(1) twice for each invocation
implicit none
integer,parameter :: isz=100
real :: rr(isz)
integer :: ii(isz), jj(isz)
integer :: i
character(len=*),parameter :: x='(*(g0,1x))'
!write(*,*)'HUGE=',huge(0) ! HUGE= 2147483647
call doboth( [100,100,500,300,100,400,500,200] )
call doboth( [0,0,0,0,0] )
call doboth( [100,500,300,400,200] )
call doboth( [100,500,-300,0,400,200] )
call doboth( [600,300,200,100,0,-100,-200,-300] )
write(*,x)repeat('=',80)
write(*,*)'initializing array with ',isz,' random numbers'
CALL RANDOM_NUMBER(RR)
jj=rr*450000.0
! use the index array to actually move the input array into a sorted order
ii=jj
ii=ii(sorti(ii))
write(*,*)'checking if values are sorted(3f)'
do i=1,isz-1
if(ii(i).gt.ii(i+1))then
write(*,*)'Error in sorting reals small to large ',i,ii(i),ii(i+1)
endif
enddo
write(*,*)'test of sorti(3f) complete'
ii=jj
ii(sorti(ii))=ii
write(*,*)'checking if values are sorted(3f)'
do i=1,isz-1
if(ii(i).gt.ii(i+1))then
write(*,*)'Error in sorting reals small to large ',i,ii(i),ii(i+1)
endif
enddo
write(*,*)'test of sorti(3f) complete'
contains
subroutine doboth(ints)
integer :: ints(:)
integer,allocatable :: iii(:)
iii=ints
write(*,x)repeat('=',80)
write(*,x)'INPUT',iii
iii(sorti(iii))=iii
write(*,x)'LHS ',iii
iii=iii(sorti(iii))
write(*,x)'RHS ',iii
end subroutine doboth
function sorti(ints) result(counts)
integer :: ints(:)
integer :: counts(size(ints)), i
integer, save :: calls=0
! WARNING: this sort is slow as mollasses, but if fun to write in one line
! relative sort first take account of duplicates
counts=[(count(ints(i) > ints) + count(ints(i) == ints(:i)), i=1,size(ints) )]
calls=calls+1
write(*,x)'CALLS',CALLS,'VALUES',counts
end function sorti
end program
I added a few things that push modern features and even though I was totally content with using fpm(1) dependencies I made a stand-alone version in app/main.f90 if any non-fpm users want to start with my version (which does not use the logic above with the bug) in
if anyone else wants to play. Ignore the bug.f90 and blocky.f90 files. So good for now (unless someone wants to explain why my first sort in above does not work :>).
There are some interesting ideas discussed above that would be interesting to implement. I am curious to compare the app/main.f90 version to some of the C++ versions if any classmates are brazen enough to post in hostile terroritory
So several bug reports, and the M_random and chal github repositories and some fun posting later I am taking a break from worrying about it for now, but I learned I did not know something I thought I did, which is always useful. I originally just thought it was a good puzzle to play with while I drank a cup of coffee.
PS: concerning some of the comments regarding something like a spreadsheet or shell in Fortran mentioned above:
I have used a program for years (as have several other hundred people) that is a fortran shell with a full expression parser, graphics, steam tables, integration, differentiation, several command styles including a purely functional one (even IF is a function, but it can execute named blocks of code as well as expressions) that I still prefer over gnuplot, R, MATLAB, and so on. Even today a big advantage (it has not changed radically since the early 90’s) is that you can load Fortran functions into it directly without creating bindings; and the people that use it have a LOT of those, and the data formats (binary and text) that it can read are very simple for Fortran programs to write, so Fortran is a totally adequate language for that type of stuff, it’s just that most places have not maintained or developed them. Some other old tools are still around and have an impressive amount of functionality, I also used a nearly complete FORTRAN77 interpreter interactively for years, so I am not such a hater of implicit typing as some are. Interactively, it is quite handy. Unfortunately, that is gone (I think). I still don’t know anything else where I can say to take the output of two runs and produce deltas on temperature versus pressure tables, and anywhere where the delta is > a threshhold make automatically labeled plots showing the curves to the left axis and the delta to right axis with a single command, and play back everything including mouse selections from a journal file (which can also be used to verify exactly what operations were performed on the data) in any of those other tools (and pedigree is critical to me) so I don’t think I am going to switch anytime soon.