How to resolve the algorithm Generic swap step by step in the Fortran programming language
How to resolve the algorithm Generic swap step by step in the Fortran programming language
Table of Contents
Problem Statement
Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in the given language, it is permissible that the two variables be constrained to having a mutually compatible type, such that each is permitted to hold the value previously stored in the other without a type violation. That is to say, solutions do not have to be capable of exchanging, say, a string and integer value, if the underlying storage locations are not attributed with types that permit such an exchange. Generic swap is a task which brings together a few separate issues in programming language semantics. Dynamically typed languages deal with values in a generic way quite readily, but do not necessarily make it easy to write a function to destructively swap two variables, because this requires indirection upon storage places or upon the syntax designating storage places. Functional languages, whether static or dynamic, do not necessarily allow a destructive operation such as swapping two variables regardless of their generic capabilities. Some static languages have difficulties with generic programming due to a lack of support for (Parametric Polymorphism). Do your best!
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Generic swap step by step in the Fortran programming language
Source code in the fortran programming language
MODULE Genericswap
IMPLICIT NONE
INTERFACE Swap
MODULE PROCEDURE Swapint, Swapreal, Swapstring
END INTERFACE
CONTAINS
SUBROUTINE Swapint(a, b)
INTEGER, INTENT(IN OUT) :: a, b
INTEGER :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapint
SUBROUTINE Swapreal(a, b)
REAL, INTENT(IN OUT) :: a, b
REAL :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapreal
SUBROUTINE Swapstring(a, b)
CHARACTER(*), INTENT(IN OUT) :: a, b
CHARACTER(len(a)) :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapstring
END MODULE Genericswap
PROGRAM EXAMPLE
USE Genericswap
IMPLICIT NONE
INTEGER :: i1 = 1, i2 = 2
REAL :: r1 = 1.0, r2 = 2.0
CHARACTER(3) :: s1="abc", s2="xyz"
CALL Swap(i1, i2)
CALL Swap(r1, r2)
CALL Swap(s1, s2)
WRITE(*,*) i1, i2 ! Prints 2 and 1
WRITE(*,*) r1, r2 ! Prints 2.0 and 1.0
WRITE(*,*) s1, s2 ! Prints xyz and abc
END PROGRAM EXAMPLE
You may also check:How to resolve the algorithm Executable library step by step in the Go programming language
You may also check:How to resolve the algorithm SEDOLs step by step in the Wren programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the Erlang programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Clojure programming language