forked from zziolko/fortran-class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derived-type-2.f90
76 lines (46 loc) · 1.23 KB
/
derived-type-2.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module algebra
implicit none
type Matrix
integer :: num_cols = 0
integer :: num_rows = 0
real, dimension(:,:), allocatable :: elements
contains
procedure :: create => create_matrix
procedure :: delete => delete_matrix
procedure :: print => print_matrix
end type Matrix
contains
subroutine create_matrix(A,nrow,ncol)
implicit none
class(Matrix), intent(inout) :: A
integer, intent(in) :: nrow, ncol
A%num_cols = ncol
A%num_rows = nrow
allocate( A%elements(nrow,ncol) )
A%elements = 0.0
end subroutine create_matrix
subroutine delete_matrix(A)
implicit none
class(Matrix), intent(inout) :: A
A%num_cols = 0
A%num_rows = 0
deallocate( A%elements )
end subroutine delete_matrix
subroutine print_matrix(A)
class(Matrix), intent(in) :: A
integer :: i, j
do i=1, A%num_rows
print *,( A%elements(i,j), j=1, A%num_cols )
end do
end subroutine print_matrix
end module algebra
program derived_type
use algebra
integer :: n
integer :: i, j
type(Matrix) :: A, B
n = 4
call A%create(n,n)
call A%print
call A%delete
end program derived_type