Fortran Coder

查看: 9114|回复: 2
打印 上一主题 下一主题

[子程序] 智障新人求助

[复制链接]

3

帖子

2

主题

0

精华

新人

F 币
24 元
贡献
12 点
跳转到指定楼层
楼主
发表于 2017-6-24 19:50:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大神门,新手刚刚入门,有一个弱弱的问题问大家。
要写一个程序将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]               


分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
沙发
发表于 2017-6-24 20:38:29 | 只看该作者
本帖最后由 li913 于 2017-6-24 20:40 编辑

[Fortran] 纯文本查看 复制代码
subroutine sort(a, b, m)
integer:: m, i,j, t
real:: a(m), b(m)
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

subroutine sort(a, b)
integer:: m, i,j, t
real:: a(:), b(:)
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


前者比较常用,后者需要接口:http://fcode.cn/guide-103-1.html。另外,t的类型应与a一致。

3

帖子

2

主题

0

精华

新人

F 币
24 元
贡献
12 点
板凳
 楼主| 发表于 2017-6-24 21:57:23 | 只看该作者
li913 发表于 2017-6-24 20:38
[mw_shl_code=fortran,true]subroutine sort(a, b, m)
integer:: m, i,j, t
real:: a(m), b(m)

哇!谢谢您的回复,思路一下清晰了!
万分感谢!!
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-25 19:29

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表