建立随机数组,搜索并排序FORTRAN [英] build random array, search and sort FORTRAN

查看:456
本文介绍了建立随机数组,搜索并排序FORTRAN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经构建了程序,但是一遍又一遍地说相同的内容时出现错误:

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

推荐答案

您遇到了一些问题.

首先,您必须将虚拟参数传递给子例程.您没有将任何内容传递给子例程,而是通过声明a是INTENT(in)来寻找该子例程.您可以对许多子例程(例如randfunc)执行此操作.

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.

您正在致电randfunc

You are calling randfunc

call randfunc(N,a)

但是定义randfunc的方式不同.

But the way you define randfunc is different.

subroutine randfunc

什么时候应该像

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

binarySearch也是如此

Same goes with binarySearch

Subroutine binarySearch(a)
....
end Subroutine

接下来,必须分配可分配数组的大小.您不能给它们一个n的大小,否则会破坏分配它的目的.像这样:

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

另一个问题是,您应该使用CONTAINS语句(或模块)将子例程放入主程序中,因为如果您具有将延迟形状数组作为虚拟参数的函数,那么您必须具有显式接口.

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

这篇关于建立随机数组,搜索并排序FORTRAN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆