Introductory Fortran Programming Part 3 -...
Transcript of Introductory Fortran Programming Part 3 -...
dynamic arrays datatypes lists
Introductory Fortran Programming Part 3
Gunnar Wollan1
Dept. of Geosciences, University of Oslo1
January 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Outline
1 Dynamic arrays
2 Derived datatypes
3 Linked lists
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
List of Topics
1 Dynamic arrays
2 Derived datatypes
3 Linked lists
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What is a dynamic array?
A dynamic array is an array where we can alocate the space atrun-time
We do that by declaring the array as ALLOCATABLE
An allocatable array of a real type can be declared like this:
The code is like this:
REAL, ALLOCATABLE, DIMENSION(:,:) :: arrayINTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The ALLOCATE function reserves space in memory for ourarray
It is important to test if the allocation was successful
We do that by using the STATUS= which returns an integersignaling the result of the operation
If a zero was returned the operation was successful, but anyother value signals an error
If the allocation was ok the dynamic array can be used justlike a static array
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The ALLOCATE function reserves space in memory for ourarray
It is important to test if the allocation was successful
We do that by using the STATUS= which returns an integersignaling the result of the operation
If a zero was returned the operation was successful, but anyother value signals an error
If the allocation was ok the dynamic array can be used justlike a static array
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The ALLOCATE function reserves space in memory for ourarray
It is important to test if the allocation was successful
We do that by using the STATUS= which returns an integersignaling the result of the operation
If a zero was returned the operation was successful, but anyother value signals an error
If the allocation was ok the dynamic array can be used justlike a static array
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The ALLOCATE function reserves space in memory for ourarray
It is important to test if the allocation was successful
We do that by using the STATUS= which returns an integersignaling the result of the operation
If a zero was returned the operation was successful, but anyother value signals an error
If the allocation was ok the dynamic array can be used justlike a static array
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The ALLOCATE function reserves space in memory for ourarray
It is important to test if the allocation was successful
We do that by using the STATUS= which returns an integersignaling the result of the operation
If a zero was returned the operation was successful, but anyother value signals an error
If the allocation was ok the dynamic array can be used justlike a static array
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Another way of using dynamic allocation of memory is to usea POINTER variable
In Fortran 95 we can declare a variable as a pointer. thepointer can be of any variable type
For those who has programmed in other languages likeC/C++, a pointer in Fortran 95 is not the same as a pointerin C/C++
In Fortran 95 a pointer is an alias pointing to a target of thesame datatype and shape as the pointer
A poiter can also be allocated to be used as an ordinaryvariable it beeing a single variable or an array ov one or moredimensions
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Another way of using dynamic allocation of memory is to usea POINTER variable
In Fortran 95 we can declare a variable as a pointer. thepointer can be of any variable type
For those who has programmed in other languages likeC/C++, a pointer in Fortran 95 is not the same as a pointerin C/C++
In Fortran 95 a pointer is an alias pointing to a target of thesame datatype and shape as the pointer
A poiter can also be allocated to be used as an ordinaryvariable it beeing a single variable or an array ov one or moredimensions
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Another way of using dynamic allocation of memory is to usea POINTER variable
In Fortran 95 we can declare a variable as a pointer. thepointer can be of any variable type
For those who has programmed in other languages likeC/C++, a pointer in Fortran 95 is not the same as a pointerin C/C++
In Fortran 95 a pointer is an alias pointing to a target of thesame datatype and shape as the pointer
A poiter can also be allocated to be used as an ordinaryvariable it beeing a single variable or an array ov one or moredimensions
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Another way of using dynamic allocation of memory is to usea POINTER variable
In Fortran 95 we can declare a variable as a pointer. thepointer can be of any variable type
For those who has programmed in other languages likeC/C++, a pointer in Fortran 95 is not the same as a pointerin C/C++
In Fortran 95 a pointer is an alias pointing to a target of thesame datatype and shape as the pointer
A poiter can also be allocated to be used as an ordinaryvariable it beeing a single variable or an array ov one or moredimensions
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Another way of using dynamic allocation of memory is to usea POINTER variable
In Fortran 95 we can declare a variable as a pointer. thepointer can be of any variable type
For those who has programmed in other languages likeC/C++, a pointer in Fortran 95 is not the same as a pointerin C/C++
In Fortran 95 a pointer is an alias pointing to a target of thesame datatype and shape as the pointer
A poiter can also be allocated to be used as an ordinaryvariable it beeing a single variable or an array ov one or moredimensions
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So how can we use both dynamic arrays and pointers
Let us make a small example to illustrate this
We have an allocatable array like in the first code example
In addition we declare a pointer variable of the same datatypeand shape as the array
The code can be like this
REAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))....ptr(:,:) => array(10:20,10:20)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So how can we use both dynamic arrays and pointers
Let us make a small example to illustrate this
We have an allocatable array like in the first code example
In addition we declare a pointer variable of the same datatypeand shape as the array
The code can be like this
REAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))....ptr(:,:) => array(10:20,10:20)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So how can we use both dynamic arrays and pointers
Let us make a small example to illustrate this
We have an allocatable array like in the first code example
In addition we declare a pointer variable of the same datatypeand shape as the array
The code can be like this
REAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))....ptr(:,:) => array(10:20,10:20)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So how can we use both dynamic arrays and pointers
Let us make a small example to illustrate this
We have an allocatable array like in the first code example
In addition we declare a pointer variable of the same datatypeand shape as the array
The code can be like this
REAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))....ptr(:,:) => array(10:20,10:20)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So how can we use both dynamic arrays and pointers
Let us make a small example to illustrate this
We have an allocatable array like in the first code example
In addition we declare a pointer variable of the same datatypeand shape as the array
The code can be like this
REAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstat....ALLOCATE(array(100,100),STATUS=rstat))....ptr(:,:) => array(10:20,10:20)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we done here?
We have declared the array as both ALLOCATABLE and aTARGET
This means that we can hava a pointer pointing to the wholearray or only to a part of the array
In this example the pointer ptr is pointing to the 10x10elements of the 100x100 elements of the array variablestarting at position 10,10
So what have we gained here??
We can now index the ptr variable from 1 to 10 in eachdirection instead of remembering that we have to startindexing from ten 10 to 20This is also a convenient way of passing parts of an array to asubroutine or a function without passing the index values asarguments
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us make a small program based on the array declarationsusing this technique to passparts of an arry to a subroutine
We will read the data from a file where the first two numbersare the size of the array
The code extension can look like this
PROGRAM array_testCHARACTER(LEN=80() :: filenameREAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstatINTEGER :: i, j, m, nINTEGER,PARAMETER :: lun = 10
....
Note the use of a static variable lun. It will be used for filereference
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us make a small program based on the array declarationsusing this technique to passparts of an arry to a subroutine
We will read the data from a file where the first two numbersare the size of the array
The code extension can look like this
PROGRAM array_testCHARACTER(LEN=80() :: filenameREAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstatINTEGER :: i, j, m, nINTEGER,PARAMETER :: lun = 10
....
Note the use of a static variable lun. It will be used for filereference
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us make a small program based on the array declarationsusing this technique to passparts of an arry to a subroutine
We will read the data from a file where the first two numbersare the size of the array
The code extension can look like this
PROGRAM array_testCHARACTER(LEN=80() :: filenameREAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstatINTEGER :: i, j, m, nINTEGER,PARAMETER :: lun = 10
....
Note the use of a static variable lun. It will be used for filereference
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us make a small program based on the array declarationsusing this technique to passparts of an arry to a subroutine
We will read the data from a file where the first two numbersare the size of the array
The code extension can look like this
PROGRAM array_testCHARACTER(LEN=80() :: filenameREAL,ALLOCATABLE,TARGET,DIMENSION(:,:) :: arrayREAL,POINTER :: ptr(:,:)INTEGER :: rstatINTEGER :: i, j, m, nINTEGER,PARAMETER :: lun = 10
....
Note the use of a static variable lun. It will be used for filereference
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us open the file and read in the dimensions of the arrayand allocate the array
....OPEN(UNIT=lun,FILE=filename,FORM=’UNFORMATTED’,&
IOSTAT=rstat)READ(UNIT=lun,IOSTAT=rstat) m, nALLOCATE(array(m,n),STAT=rstat)....
Here we use the two integer numbers containing thedimensions from the binary file to allocate the array
Even if it is not in this code we should always test the returnstatus from all file operations to check if it was successful
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us open the file and read in the dimensions of the arrayand allocate the array
....OPEN(UNIT=lun,FILE=filename,FORM=’UNFORMATTED’,&
IOSTAT=rstat)READ(UNIT=lun,IOSTAT=rstat) m, nALLOCATE(array(m,n),STAT=rstat)....
Here we use the two integer numbers containing thedimensions from the binary file to allocate the array
Even if it is not in this code we should always test the returnstatus from all file operations to check if it was successful
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Let us open the file and read in the dimensions of the arrayand allocate the array
....OPEN(UNIT=lun,FILE=filename,FORM=’UNFORMATTED’,&
IOSTAT=rstat)READ(UNIT=lun,IOSTAT=rstat) m, nALLOCATE(array(m,n),STAT=rstat)....
Here we use the two integer numbers containing thedimensions from the binary file to allocate the array
Even if it is not in this code we should always test the returnstatus from all file operations to check if it was successful
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Then let us read the data from the file into the array, closethe file
and sett the pointer to point to a part of the array (weassume the array size is 100x100)
....DO j = 1, n
DO i = 1, mREAD(UNIT=lun,IOSTAT=rstat) array(i,j)
END DOEND DOCLOSE(lun)ptr => array(1:m-20,1:n-20)....
Now the pointer is associated with the array(1:80,1:80)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Then let us read the data from the file into the array, closethe file
and sett the pointer to point to a part of the array (weassume the array size is 100x100)
....DO j = 1, n
DO i = 1, mREAD(UNIT=lun,IOSTAT=rstat) array(i,j)
END DOEND DOCLOSE(lun)ptr => array(1:m-20,1:n-20)....
Now the pointer is associated with the array(1:80,1:80)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Then let us read the data from the file into the array, closethe file
and sett the pointer to point to a part of the array (weassume the array size is 100x100)
....DO j = 1, n
DO i = 1, mREAD(UNIT=lun,IOSTAT=rstat) array(i,j)
END DOEND DOCLOSE(lun)ptr => array(1:m-20,1:n-20)....
Now the pointer is associated with the array(1:80,1:80)
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
Now we will pass the pointer to the subroutine and makesome calculations on the partial array
....CALL calculate(ptr,m-20,n-20)....END PROGRAM array_test
SUBROUTINE calculate(A,m,n)REAL, POINTER :: A(:,:)INTEGER :: m, nINTEGER :: i, jm = SIZE(A,1)n = SIZE(A,2)DO j = 1, nDO i = 1, m
A(i,j) = A(i,j) * SQRT(3.1415)END DO
END DO....
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
So what have we really done in this program?
First of all we have declared a dynamic array which c an bepointed to by a pointer variable
In addition to other variable declarations we openend a binaryfile, read in two integers containing the array size
Then we allocated the array, read in the data from the file andset the pointe variable to point to a part of the array
The last part was that we passed the pointer to the subroutineas an argument
In the subroutine we used the intrinsic function SIZE() to getthe array size of the argument
finally we performed a calculation on the contents of theargument which was of course the pointer from the callingprogram
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The use of dynamic arrays are a valuable tool since we do nothave to know the array size in advance
With the introduction of the pointer we can simplify the codeby not having to pass array sizes, where to start an index etc.as arguments to a function or subroutine, but just pass thearray and let all indexes start at 1
Click here to see the source code for this example program
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The use of dynamic arrays are a valuable tool since we do nothave to know the array size in advance
With the introduction of the pointer we can simplify the codeby not having to pass array sizes, where to start an index etc.as arguments to a function or subroutine, but just pass thearray and let all indexes start at 1
Click here to see the source code for this example program
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
The use of dynamic arrays are a valuable tool since we do nothave to know the array size in advance
With the introduction of the pointer we can simplify the codeby not having to pass array sizes, where to start an index etc.as arguments to a function or subroutine, but just pass thearray and let all indexes start at 1
Click here to see the source code for this example program
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Dynamic arrays
What have we learned in this lesson?
We have learned about dynamic arrays, target arrays andpointers
In addition we have learned that a pointer can be set up topoint to a part of the target array an dnot the whole array
Also with the use of a pointer in this way we can simplify theuse of indexing in subroutines and functions
To test what we have learned we can again answer a multiplechoice quiz
This requires that you have enabled JavaScript in your browser
Click here for the quiz
The answers without taking the quiz is here
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
List of Topics
1 Dynamic arrays
2 Derived datatypes
3 Linked lists
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
What is a derived datatype?
It is a compund datatype made up from the basic data typesor other derived datatypes
To declare a derived datatype we use the TYPE keyword
An example of such a derived datatype is as follows
TYPE (mytype)CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueINTEGER :: year, month, day
END TYPE mytypeTYPE(mytype) :: station
This derived data type consists of a character string to hold aname, a double precision number to hold a mesured value andthree integers to hold the date of the measurement
We use the TYPE(mytype) to declare a variable of the deriveddata type
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
The example on the previous slide contained three integers tohold the year, month and day
It would of course be nice to have a single variable called datetho hold these values
An implementation could look like this
TYPE dateINTEGER :: year, month, day
END TYPE dateTYPE mytype
CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueTYPE(date) :: today
END TYPE mytypeTYPE(mytype) :: station
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
The example on the previous slide contained three integers tohold the year, month and day
It would of course be nice to have a single variable called datetho hold these values
An implementation could look like this
TYPE dateINTEGER :: year, month, day
END TYPE dateTYPE mytype
CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueTYPE(date) :: today
END TYPE mytypeTYPE(mytype) :: station
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
The example on the previous slide contained three integers tohold the year, month and day
It would of course be nice to have a single variable called datetho hold these values
An implementation could look like this
TYPE dateINTEGER :: year, month, day
END TYPE dateTYPE mytype
CHARACTER(LEN=20) :: nameDOUBLE PRECISION :: measure_valueTYPE(date) :: today
END TYPE mytypeTYPE(mytype) :: station
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
So what have we done here?
First we defined another variable type called date whichcontains three integer variables
Then we defined our variable mytype which in addition to theoriginal variables contains a variable of the new date typeinstead of the three integers in the first definition of mytype
This is all very fine, but how do we get access to the internalvariables in our new types?
Let us take another code example
TYPE(mytype) :: stationstation%name = "Site One"station%today%year = 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
So what have we done here?
First we defined another variable type called date whichcontains three integer variables
Then we defined our variable mytype which in addition to theoriginal variables contains a variable of the new date typeinstead of the three integers in the first definition of mytype
This is all very fine, but how do we get access to the internalvariables in our new types?
Let us take another code example
TYPE(mytype) :: stationstation%name = "Site One"station%today%year = 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
So what have we done here?
First we defined another variable type called date whichcontains three integer variables
Then we defined our variable mytype which in addition to theoriginal variables contains a variable of the new date typeinstead of the three integers in the first definition of mytype
This is all very fine, but how do we get access to the internalvariables in our new types?
Let us take another code example
TYPE(mytype) :: stationstation%name = "Site One"station%today%year = 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
So what have we done here?
First we defined another variable type called date whichcontains three integer variables
Then we defined our variable mytype which in addition to theoriginal variables contains a variable of the new date typeinstead of the three integers in the first definition of mytype
This is all very fine, but how do we get access to the internalvariables in our new types?
Let us take another code example
TYPE(mytype) :: stationstation%name = "Site One"station%today%year = 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
So what have we done here?
First we defined another variable type called date whichcontains three integer variables
Then we defined our variable mytype which in addition to theoriginal variables contains a variable of the new date typeinstead of the three integers in the first definition of mytype
This is all very fine, but how do we get access to the internalvariables in our new types?
Let us take another code example
TYPE(mytype) :: stationstation%name = "Site One"station%today%year = 2007
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
As we can see we access the internal variables by using thevariable name of the new variable type with the percent-signand then the name of the internal variable
If we have nested types (as we have here) we just add anotherpercent-sign and then the name if the nested variable likewhen we set the value of the year
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Derived datatypes
As we can see we access the internal variables by using thevariable name of the new variable type with the percent-signand then the name of the internal variable
If we have nested types (as we have here) we just add anotherpercent-sign and then the name if the nested variable likewhen we set the value of the year
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
List of Topics
1 Dynamic arrays
2 Derived datatypes
3 Linked lists
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
What is a linked list?
In this context we use the term linked list to consist of a listof objects linked together by pointers pointing to the next andprevious element in the list
We use a derived datatype to hold the data and pointers ineach list element
An example of such a list element can be like this
TYPE list_elementTYPE(list_element), POINTER :: prevTYPE(list_element), POINTER :: nextCHARACTER(LEN=80) :: nameDOUBLE PRECISION :: measured_valueTYPE(date) :: today
END TYPE list_element
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
What is a linked list?
In this context we use the term linked list to consist of a listof objects linked together by pointers pointing to the next andprevious element in the list
We use a derived datatype to hold the data and pointers ineach list element
An example of such a list element can be like this
TYPE list_elementTYPE(list_element), POINTER :: prevTYPE(list_element), POINTER :: nextCHARACTER(LEN=80) :: nameDOUBLE PRECISION :: measured_valueTYPE(date) :: today
END TYPE list_element
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
What is a linked list?
In this context we use the term linked list to consist of a listof objects linked together by pointers pointing to the next andprevious element in the list
We use a derived datatype to hold the data and pointers ineach list element
An example of such a list element can be like this
TYPE list_elementTYPE(list_element), POINTER :: prevTYPE(list_element), POINTER :: nextCHARACTER(LEN=80) :: nameDOUBLE PRECISION :: measured_valueTYPE(date) :: today
END TYPE list_element
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
What is a linked list?
In this context we use the term linked list to consist of a listof objects linked together by pointers pointing to the next andprevious element in the list
We use a derived datatype to hold the data and pointers ineach list element
An example of such a list element can be like this
TYPE list_elementTYPE(list_element), POINTER :: prevTYPE(list_element), POINTER :: nextCHARACTER(LEN=80) :: nameDOUBLE PRECISION :: measured_valueTYPE(date) :: today
END TYPE list_element
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
What is a linked list?
In this context we use the term linked list to consist of a listof objects linked together by pointers pointing to the next andprevious element in the list
We use a derived datatype to hold the data and pointers ineach list element
An example of such a list element can be like this
TYPE list_elementTYPE(list_element), POINTER :: prevTYPE(list_element), POINTER :: nextCHARACTER(LEN=80) :: nameDOUBLE PRECISION :: measured_valueTYPE(date) :: today
END TYPE list_element
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Next we defina a new variable to contain the linked list
We call this new variable just list
TYPE linked_listTYPE(list_element), POINTER :: headTYPE(list_element), POINTER :: tailTYPE(list_element), POINTER :: this
END TYPE linked_list....TYPE(linked_list) :: listTYPE(list_element), POINTER :: element
The list type contains three pointer variables which will be setto point to the first element of the list the head, the lastelement of the list tail and the current element of the list this
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Next we defina a new variable to contain the linked list
We call this new variable just list
TYPE linked_listTYPE(list_element), POINTER :: headTYPE(list_element), POINTER :: tailTYPE(list_element), POINTER :: this
END TYPE linked_list....TYPE(linked_list) :: listTYPE(list_element), POINTER :: element
The list type contains three pointer variables which will be setto point to the first element of the list the head, the lastelement of the list tail and the current element of the list this
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Next we defina a new variable to contain the linked list
We call this new variable just list
TYPE linked_listTYPE(list_element), POINTER :: headTYPE(list_element), POINTER :: tailTYPE(list_element), POINTER :: this
END TYPE linked_list....TYPE(linked_list) :: listTYPE(list_element), POINTER :: element
The list type contains three pointer variables which will be setto point to the first element of the list the head, the lastelement of the list tail and the current element of the list this
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Then we will see how we can put things together
ALLOCATE(element)element%name = "nr1"element%measured_value = 3.14list%head => elementlist%tail => elementlist%this => element
Here we have created a new element and added it to our list
Since the element is a pointer we have to allocate space for it
If we do not allocate space the element will have no space forthe variables
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Then we will see how we can put things together
ALLOCATE(element)element%name = "nr1"element%measured_value = 3.14list%head => elementlist%tail => elementlist%this => element
Here we have created a new element and added it to our list
Since the element is a pointer we have to allocate space for it
If we do not allocate space the element will have no space forthe variables
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Then we will see how we can put things together
ALLOCATE(element)element%name = "nr1"element%measured_value = 3.14list%head => elementlist%tail => elementlist%this => element
Here we have created a new element and added it to our list
Since the element is a pointer we have to allocate space for it
If we do not allocate space the element will have no space forthe variables
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Then we will see how we can put things together
ALLOCATE(element)element%name = "nr1"element%measured_value = 3.14list%head => elementlist%tail => elementlist%this => element
Here we have created a new element and added it to our list
Since the element is a pointer we have to allocate space for it
If we do not allocate space the element will have no space forthe variables
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Performing the same procedure we can fill our list with variouselements
The printout in the test program with two elements in the listis this
PRINT *, TRIM(list%head%name)PRINT *, list%head%measured_valuePRINT *, TRIM(list%this%name)PRINT *, list%this%measured_valuePRINT *, TRIM(list%tail%name)PRINT *, list%tail%measured_value
Note the use of the TRIM function whch will suppress theprinting of trailing spaces int the character string
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Performing the same procedure we can fill our list with variouselements
The printout in the test program with two elements in the listis this
PRINT *, TRIM(list%head%name)PRINT *, list%head%measured_valuePRINT *, TRIM(list%this%name)PRINT *, list%this%measured_valuePRINT *, TRIM(list%tail%name)PRINT *, list%tail%measured_value
Note the use of the TRIM function whch will suppress theprinting of trailing spaces int the character string
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
Performing the same procedure we can fill our list with variouselements
The printout in the test program with two elements in the listis this
PRINT *, TRIM(list%head%name)PRINT *, list%head%measured_valuePRINT *, TRIM(list%this%name)PRINT *, list%this%measured_valuePRINT *, TRIM(list%tail%name)PRINT *, list%tail%measured_value
Note the use of the TRIM function whch will suppress theprinting of trailing spaces int the character string
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
The ouput from the test program was this
Nr13.14000010490417
Nr23.14149999618530
Nr23.14149999618530
Click here to see the source code for this example program
Wollan Introductory Fortran Programming Part 3
dynamic arrays datatypes lists
Linked lists
The ouput from the test program was this
Nr13.14000010490417
Nr23.14149999618530
Nr23.14149999618530
Click here to see the source code for this example program
Wollan Introductory Fortran Programming Part 3