문제

So I've built my program but I'm getting errors saying the same stuff over and over:

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

so it obviously has a problem with my array and dimensions, but due to my lack of understanding regarding allocatable arrays I'm stumped. I've included the program and subroutines in the hope that someone can spot my problem easily.

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
도움이 되었습니까?

해결책

You have a few problems.

First you must pass a dummy argument to the subroutine. You are not passing anything to the subroutine and by declaring that a is INTENT(in) it is looking for that. You do this for a lot of the subroutines such as randfunc.

You are calling randfunc

call randfunc(N,a)

But the way you define randfunc is different.

subroutine randfunc

When it should be like

subroutine randfunc(N, a)
    integer, intent(in) :: N
    real, dimension(:), intent(out), allocatable :: a

Same goes with binarySearch

Subroutine binarySearch(a)
....
end Subroutine

Next, allocatable arrays must be deferred size. You can't give them a size of n otherwise that defeats the purpose of allocating it. Like so:

real, dimension(:), allocatable :: a

Another problem is that you should put the subroutines into the main program with a CONTAINS statement(or module) because if you have a function with a deferred shape array as a dummy argument you must have an explicit interface.

program benchmark
   stuff
    .....
 contains

  subroutine randfunct(N, a)
  end subroutine
  ! other subroutine declarations
  .....

 end program
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top