程序小白求助,Fortran程序中,如何对一列数据每12个为一组求平均?
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/10 01:13:46
程序小白求助,Fortran程序中,如何对一列数据每12个为一组求平均?
要求直接通过文本读取数据,最后的结果也要放在一个文本里,
要求直接通过文本读取数据,最后的结果也要放在一个文本里,
假定数据一共960个,且是一列排列的
program main
implicit real*8 (a-h,o-z)
dimension a(960),b(80),aa
integer k ,kkk
kkk = 80
aa = 0
k = 1
open(1,file = 'in.dat',status = 'old')
open(2,file = 'out.dat',status = 'unknown')
rean(1,*) (a(i),i=1,960)
do j = 1,kkk
do i = 1,12
aa = a(k)+aa
k = k +1
enddo
b(j) = aa/12
enddo
write(2,*) (b(i),i=1,kkk)
end
应该没有问题
再问: 有不少错误,稍微改了下,最后出来这个错误The shapes of the array expressions do not conform.b(j)=aa/12,该怎么弄啊,能解决的话我在加5分
再答: 程序经过我优化了,运行后会提示要求输入数组的长度,我测试用的是960个数字。这段代码上要求输入数组的长度不超过2400,如果比这个长,需要修改代码: program main implicit real*8 (a-h,o-z) integer :: k,l,m real*8 :: a(2400),b(200) real*8 :: aa aa = 0 k = 1 write(*,*) 'Input the length of the input array: ' read(*,*) l m = l / 12 write(*,*) 'The length of the output array: ',m open(1,file = 'in.dat',status = 'old') open(2,file = 'out.dat',status = 'unknown') read(1,*) (a(i),i=1,l) do j = 1,m do i = 1, 12 aa = a(k)+aa k = k +1 enddo b(j) = aa/12 write(2,*) b(j) aa = 0.0 enddo end
program main
implicit real*8 (a-h,o-z)
dimension a(960),b(80),aa
integer k ,kkk
kkk = 80
aa = 0
k = 1
open(1,file = 'in.dat',status = 'old')
open(2,file = 'out.dat',status = 'unknown')
rean(1,*) (a(i),i=1,960)
do j = 1,kkk
do i = 1,12
aa = a(k)+aa
k = k +1
enddo
b(j) = aa/12
enddo
write(2,*) (b(i),i=1,kkk)
end
应该没有问题
再问: 有不少错误,稍微改了下,最后出来这个错误The shapes of the array expressions do not conform.b(j)=aa/12,该怎么弄啊,能解决的话我在加5分
再答: 程序经过我优化了,运行后会提示要求输入数组的长度,我测试用的是960个数字。这段代码上要求输入数组的长度不超过2400,如果比这个长,需要修改代码: program main implicit real*8 (a-h,o-z) integer :: k,l,m real*8 :: a(2400),b(200) real*8 :: aa aa = 0 k = 1 write(*,*) 'Input the length of the input array: ' read(*,*) l m = l / 12 write(*,*) 'The length of the output array: ',m open(1,file = 'in.dat',status = 'old') open(2,file = 'out.dat',status = 'unknown') read(1,*) (a(i),i=1,l) do j = 1,m do i = 1, 12 aa = a(k)+aa k = k +1 enddo b(j) = aa/12 write(2,*) b(j) aa = 0.0 enddo end