【问题标题】:build random array, search and sort FORTRAN构建随机数组,搜索和排序 FORTRAN
【发布时间】:2014-04-04 22:26:45
【问题描述】:

所以我已经构建了我的程序,但是我一遍又一遍地说同样的话:

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

【问题讨论】:

    标签: arrays sorting random binary fortran


    【解决方案1】:

    你有一些问题。

    首先,您必须将一个虚拟参数传递给子例程。您没有将任何内容传递给子例程,并且通过声明 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
    

    二进制搜索也是如此

    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
    

    【讨论】:

    • 非常感谢,至少现在它正在编译,但我遇到了一个必须查看的分段错误。
    • 抱歉修复了它,在 contains 语句中遇到了一点问题。再次干杯!
    猜你喜欢
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多