0% found this document useful (0 votes)
9 views4 pages

Fortran City Sorting Examples

The document contains a Fortran program that manages a list of cities, including their populations and countries. It reads city data from a file, sorts the cities by name, population, and country, and outputs the sorted lists. Additionally, it includes a module for generic functions that calculates the sum of squares for both integer and real types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Fortran City Sorting Examples

The document contains a Fortran program that manages a list of cities, including their populations and countries. It reads city data from a file, sorts the cities by name, population, and country, and outputs the sorted lists. Additionally, it includes a module for generic functions that calculates the sum of squares for both integer and real types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXAMPLE PROGRAMS E Revision 07/12/2018

Derived Types

Input file:
London 7000000 UK
Dublin 540000 Ireland
Paris 2200000 France
Berlin 3400000 Germany
Lisbon 670000 Portugal
Madrid 3000000 Spain
Rome 2700000 Italy
Vienna 1600000 Austria
Copenhagen 1360000 Denmark
Stockholm 720000 Sweden
Helsinki 500000 Finland
"The Hague" 520000 Netherlands

Output:
Reading cities ... 12

Initial list:
London 7000000 UK
Dublin 540000 Ireland
Paris 2200000 France
Berlin 3400000 Germany
Lisbon 670000 Portugal
Madrid 3000000 Spain
Rome 2700000 Italy
Vienna 1600000 Austria
Copenhagen 1360000 Denmark
Stockholm 720000 Sweden
Helsinki 500000 Finland
The Hague 520000 Netherlands

Sorted by name:
Berlin 3400000 Germany
Copenhagen 1360000 Denmark
Dublin 540000 Ireland
Helsinki 500000 Finland
Lisbon 670000 Portugal
London 7000000 UK
Madrid 3000000 Spain
Paris 2200000 France
Rome 2700000 Italy
Stockholm 720000 Sweden
The Hague 520000 Netherlands
Vienna 1600000 Austria

Sorted by population:
Helsinki 500000 Finland
The Hague 520000 Netherlands
Dublin 540000 Ireland
Lisbon 670000 Portugal
Stockholm 720000 Sweden
Copenhagen 1360000 Denmark
Vienna 1600000 Austria
Paris 2200000 France
Rome 2700000 Italy
Madrid 3000000 Spain
Berlin 3400000 Germany
London 7000000 UK

Sorted by country:
Vienna 1600000 Austria
Copenhagen 1360000 Denmark
Helsinki 500000 Finland
Paris 2200000 France
Berlin 3400000 Germany
Dublin 540000 Ireland
Rome 2700000 Italy
The Hague 520000 Netherlands
Lisbon 670000 Portugal
Madrid 3000000 Spain
Stockholm 720000 Sweden
London 7000000 UK

Fortran Examples Part E David Apsley


module CityModule
implicit none

type city
character(len=15) name
integer population
character(len=15) country
end type city

contains

logical function lessThanName( a, b )


type(city), intent(in) :: a, b
lessThanName = llt( a%name, b%name )
end function lessThanName

!------------------------------------

logical function lessThanPopulation( a, b )


type(city), intent(in) :: a, b
lessThanPopulation = a%population < b%population
end function lessThanPopulation

!------------------------------------

logical function lessThanCountry( a, b )


type(city), intent(in) :: a, b
lessThanCountry = llt( a%country, b%country )
end function lessThanCountry

!------------------------------------

subroutine readCities( filename, A )


character(len=*), intent(in) :: filename
type(city), allocatable, intent(inout) :: A(:)
type(city) c
integer stat
integer n,i

open( 10, file=filename )

! First pass - just count cities


n = 0
stat = 0
do while ( stat == 0 )
read( 10, *, iostat=stat ) c
if ( stat == 0 ) n = n + 1
end do

! Second pass - allocate space and read cities


rewind( 10 )
allocate( A(n) )
do i = 1, n
read( 10, * ) A(i)
end do

close( 10 )

end subroutine readCities

!------------------------------------

subroutine writeCities( A )
type(city), intent(in) :: A(:)
character(len=*), parameter :: fmt = "( a15, 2x, i8, 2x, a15 )"
integer i

do i = 1, size( A )
write( *, fmt ) A(i)
end do

end subroutine writeCities

end module CityModule

!===========================================================

module SortModule
use CityModule
implicit none

Fortran Examples Part E David Apsley


contains

subroutine swap( a, b )
type(city), intent(inout) :: a, b
type(city) temp

temp = a
a = b
b = temp

end subroutine swap

!------------------------------------

subroutine selectionSort( A, lessThan )


type(city), intent(inout) :: A(:)
logical, external :: lessThan
integer n
integer i, j, jmin

n = size( A )
do i = 1, n - 1
jmin = i
do j = i + 1, n
if ( lessThan( A(j), A(jmin) ) ) jmin = j
end do
if ( jmin /= i ) call swap( A(i), A(jmin) )
end do

end subroutine selectionSort

!------------------------------------

end module SortModule

!===========================================================

program main
use CityModule
use SortModule
implicit none
type(city), allocatable :: A(:)
character(len=*), parameter :: fmt = "( /, a )"

write( *, "( a )", advance="no" ) "Reading cities ... "


call readCities( "[Link]", A )
write( *, * ) size( A )

write( *, fmt ) "Initial list:"


call writeCities( A )

write( *, fmt ) "Sorted by name:"


call selectionSort( A, lessThanName )
call writeCities( A )

write( *, fmt ) "Sorted by population:"


call selectionSort( A, lessThanPopulation )
call writeCities( A )

write( *, fmt ) "Sorted by country:"


call selectionSort( A, lessThanCountry )
call writeCities( A )

end program main

Fortran Examples Part E David Apsley


Generic Functions

module GenericModule
implicit none

interface addSquares ! Interface tying the generic name to specific procedures


module procedure addSquaresInteger, addSquaresReal
end interface addSquares

contains

integer function addSquaresInteger( i, j ) ! Version for integer type


integer, intent(in) :: i, j
addSquaresInteger = i ** 2 + j ** 2
end function addSquaresInteger

real function addSquaresReal( x, y ) ! Version for real type


real, intent(in) :: x, y
addSquaresReal = x ** 2 + y ** 2
end function addSquaresReal

end module GenericModule

program test
use GenericModule
implicit none
integer i1, i2
real x1, x2

print *, "Input two integers:"


read *, i1, i2
print *, "Sum of squares: ", addSquares( i1, i2 )

print *

print *, "Input two reals:"


read *, x1, x2
print *, "Sum of squares: ", addSquares( x1, x2 )

end program test

Fortran Examples Part E David Apsley

You might also like