Sort three variables: Difference between revisions

m
(→‎{{header|Kotlin}}: Add Fortran.)
Line 134:
The example source uses F90 mainly to enable the contained routine SWAPC though it could be made into a separate subroutine. Further F90 facilities would allow the definition of a "generic" SORT3 routine, with a lot of syntax that would enable a SORT3I2, SORT3I4, SORT3F4, ''etc.'' routines for INTEGER*2, INTEGER*4, REAL*4, ''etc.'' to be selected for each case. A suitable pre-processor scheme could perhaps generate these variations, but alas, it is not standard. There would be similar requirements for a SWAP routine for each type of parameter, for alas, Fortran does not define a SWAP statement. The temporary variable needed for the SWAP process is defined rather intimidatingly as having the size of the largest of the three parameters, a facility possibly not available until F90. Rather than have this redefined for each invocation of SWAPC (where it would be the larger of the two parameters) it is defined once in SORT3. However, all three parameters should be the same size, or risk truncation. Using a great deal more syntax (or, as standardised in F2003) it is possible to have character variables be resized on each assignment to them to accommodate the length of text being assigned.
 
For convenience in setting up the two examples, an array is used to hold the test data. The subroutine is not invoked with an array parameter, it is invoked with three separate elements of the array. The DATA statement initialising the array looks to be the transpose of the desired ordering, because of the way Fortran orders elements in storage. </lang Fortran> SUBROUTINE SORT3(X,Y,Z) !Perpetrate a bubblesort in-line.
CHARACTER*(*) X,Y,Z !Just three to rearrange.
CHARACTER*(MAX(LEN(X),LEN(Y),LEN(Z))) T !Really, they should all be the same length.
1,220

edits