所以我已经构建了我的程序但是我一遍又一遍地说同样的错误:
benchmark.f90(17): error #6451: A dummy argument name is required in this context. [N]
INTEGER, intent (in) :: N
------------------------^
benchmark.f90(18): error #6420: This array name is invalid in this context. [A]
REAL, intent (out), DIMENSION (N), allocatable :: a
--------------------------------------------------^
benchmark.f90(18): error #6646: ALLOCATABLE or POINTER attribute dictates a deferred-shape-array [A]
REAL, intent (out), DIMENSION (N), allocatable :: a
--------------------------------------------------^
benchmark.f90(17): error #6219: This variable, used in a specification expression, must be a dummy argument, a COMMON block object, or an object accessible through host or use association [N]
INTEGER, intent (in) :: N
------------------------^
benchmark.f90(26): error #6219: This variable, used in a specification expression, must be a dummy argument, a COMMON block object, or an object accessible through host or use association [N]
REAL, INTENT(in out), DIMENSION(N) :: a
----------------------------------^
benchmark.f90(48): error #6420: This array name is invalid in this context. [A]
real, intent (in out), DIMENSION(N) :: a
--------------------------------------------^
benchmark.f90(48): error #6219: This variable, used in a specification expression, must be a dummy argument, a COMMON block object, or an object accessible through host or use association [N]
real, intent (in out), DIMENSION(N) :: a
所以它显然有我的数组和维度的问题,但由于我对可分配数组缺乏了解,我很难过。我已经包含了程序和子程序,希望有人能够轻松发现我的问题。
PROGRAM BENCHMARK
implicit none
integer :: N
REAL, Dimension(N), Allocatable :: a
write(6,*)'please enter size of array'
read(5,*) N
call randfunc(N,a)
call bubblesort(a,a)
call binarysearch(a,a)
deallocate (a)
END PROGRAM BENCHMARK
SUBROUTINE randfunc
implicit none
INTEGER, intent (in) :: N
REAL, intent (out), DIMENSION (N), allocatable :: a
Allocate(a(1:N))
CALL random_number(a)
a = a*5.0
write(6,*)'this should be the randomly generated array', a
END SUBROUTINE
SUBROUTINE bubblesort(a)
REAL, INTENT(in out), DIMENSION(N) :: a
REAL :: temp
INTEGER :: i, j
LOGICAL :: swapped = .TRUE.
DO j = SIZE(a)-1, 1, -1
swapped = .FALSE.
DO i = 1, j
IF (a(i) > a(i+1)) THEN
temp = a(i)
a(i) = a(i+1)
a(i+1) = temp
swapped = .TRUE.
END IF
END DO
IF (.NOT. swapped) EXIT
END DO
END SUBROUTINE
SUBROUTINE binarysearch
REAL, intent (in out), DIMENSION(N) :: a
INTEGER :: ran, start, finish, mid
INTEGER :: i, N, val
i = 1
do i = 1, N
READ(*,*) a(i)
end do
write(6,*) 'Enter Value'
read(5,*) val
start = 1
finish = N
ran = finish - start
mid = (start + finish) /2
do while( a(mid) /= val .and. ran > 0)
if (val > a(mid)) then
start = mid + 1
else
finish = mid - 1
end if
ran = finish - start
mid = (start + finish)/2
end do
if( a(mid) /= val) then
write(*,*) val, 'NOT FOUND'
else
write(*,*) 'VALUE AT' , mid
end if
END SUBROUTINE
答案 0 :(得分:5)
你有一些问题。
首先,您必须将伪参数传递给子例程。您没有将任何内容传递给子例程,并且通过声明a是INTENT(in)它正在寻找它。你可以为很多子程序执行此操作,例如randfunc。
您正在调用randfunc
call randfunc(N,a)
但是你定义randfunc的方式是不同的。
subroutine randfunc
应该是什么时候
subroutine randfunc(N, a)
integer, intent(in) :: N
real, dimension(:), intent(out), allocatable :: a
与binarySearch
相同Subroutine binarySearch(a)
....
end Subroutine
接下来,可分配数组必须是延迟大小。你不能给它们一个n的大小,否则就会失去分配它的目的。像这样:
real, dimension(:), allocatable :: a
另一个问题是您应该使用CONTAINS语句(或模块)将子例程放入主程序中,因为如果您将具有延迟形状数组的函数作为伪参数,则必须具有显式接口。
program benchmark
stuff
.....
contains
subroutine randfunct(N, a)
end subroutine
! other subroutine declarations
.....
end program