Fortran Coder

标题: 一个小问题 [打印本页]

作者: little_kar    时间: 2017-1-13 11:02
标题: 一个小问题
怎么让输出是2*2的,而不是一行?在哪里设置代码左侧显示行数?用的是VS2012环境
[Fortran] 纯文本查看 复制代码
program test1
implicit none
integer,dimension(:,:),allocatable::a
allocate(a(2,2))
a(1,1)=3
a(1,2)=3
a(2,1)=3
a(2,2)=3
write(*,*)a
read(*,*)
end program test1
经常麻烦你们,多谢

作者: fcode    时间: 2017-1-13 11:18
[Fortran] 纯文本查看 复制代码
program test1
implicit none
integer ,allocatable:: a(:,:)
allocate(a(2,2))
a=3
write(*,'(*(2(g0,1x),/))') a
read(*,*)
end program test1

作者: little_kar    时间: 2017-1-14 17:24
大师兄,我又来了
[Fortran] 纯文本查看 复制代码
program test3
implicit none
dimension a(3,3)
integer a
!a=(/(/5,7,6/),(/5,3,7/),(/6,2,0/)/)
a=(/5,7,6,5,3,7,6,2,0/)
write(*,*)a
read(*,*)
end program test3

怎么老是提醒我错误呢,按照彭国伦先生的书来的啊
错误提示:错误        1         error #6366: The shapes of the array expressions do not conform.   [A]        E:\\test\test\test.f90        

作者: fcode    时间: 2017-1-14 21:49
等号的左边 (a)是二维数组,等号右边是一维数组。
这是不允许的。

彭国伦用的 CVF(它的检查不够严格,所以允许)
但实际上,语法是不允许的。所以需要 reshape,把等号右边变成二维数组。
[Fortran] 纯文本查看 复制代码
program test3
implicit none
integer :: a(3,3) = reshape([5,7,6,5,3,7,6,2,0],[3,3])
write(*,*)a
read(*,*)
end program test3

作者: little_kar    时间: 2017-1-15 21:01
fcode 发表于 2017-1-14 21:49
等号的左边 (a)是二维数组,等号右边是一维数组。
这是不允许的。

额,请指正:
[Fortran] 纯文本查看 复制代码
subroutine opq(a,c)
implicit none
real::a(2,1)
c=a(1,1)+a(2,1)
end subroutine opq


program test6
implicit none
real::mat(2,1)
real::ss
mat(1,1)=3
mat(2,1)=4
call subroutine opq(mat,ss)
write(*,*)ss
read(*,*)
end program test6

希望大师兄不要觉得烦哦,多谢多谢
作者: fcode    时间: 2017-1-16 19:17
[Fortran] 纯文本查看 复制代码
subroutine opq(a,c)
implicit none
real::a(2,1),c !//c要定义
c=a(1,1)+a(2,1)
end subroutine opq


program test6
implicit none
real::mat(2,1)
real::ss
mat(1,1)=3
mat(2,1)=4
call opq(mat,ss) !//不需要 subroutine
write(*,*)ss
read(*,*)
end program test6





欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2