各位大神门,新手刚刚入门,有一个弱弱的问题问大家。
要写一个程序将a中的元素从小到大排列后存入b中(a中各元素保持不变),b也是一维数组,与a具有相同的长度,我写了一个subroutine,结果参数老是出问题,希望好心人指教,不胜感激。
这是我的代码和错误情况。(subroutine的形参只能是两个)
[Fortran] 纯文本查看 复制代码
program test
Implicit none
integer::n
real, allocatable:: a(:),b(:)
Print*, '输入一维数组元素个数n:'
Read*, n
allocate(a(n), b(n))
Print*, '输入数组a的各元素值:'
Read*, a
call sort(a,b)
Print'(a, <n>f7.3)', '排序前a=', a
Print'(a, <n>f7.3)', '排序后b=', b
End program
subroutine sort(a, b)
integer:: m, i,j, t
real:: a(m), b(m)
m=size(a)
b = a !先把a整体赋给b
do i=1, m-1
do j= i+1,m
if(b(i)>b(j))then
t = b(i); b(i) = b(j); b(j) = t!交换
endif
enddo
enddo
end subroutine
错误:
错误 1 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 [M]
错误 2 error #6631: A non-optional actual argument must be present when invoking a procedure with an explicit interface. [M]
|