现在有1960到2008每一天的温度数据,要求用Fortran写出每个月的平均温度
时间: 2024-03-18 16:42:05 浏览: 60
好的,我可以帮您解决这个问题。以下是Fortran的代码:
```Fortran
program avg_temp
implicit none
integer :: i, year, month, day, prev_month
real :: temp, sum
integer, parameter :: num_years = 49 ! 49 years of data
integer, parameter :: num_months = 12 ! 12 months in a year
real :: monthly_avg(num_years, num_months)
! Initialize the monthly average array to zero
do i = 1, num_years*num_months
monthly_avg(i) = 0.0
end do
! Open the temperature data file
open(unit=10, file='temp_data.txt')
! Loop over the temperature data
do i = 1, 13779 ! 1960-01-01 to 2008-12-31
read(10,*) year, month, day, temp
! Calculate the index in the monthly_avg array for this temperature data point
prev_month = month - 1
if (prev_month == 0) prev_month = 12
i = (year-1960)*num_months + prev_month
! Add the temperature to the sum for this month
monthly_avg(i) = monthly_avg(i) + temp
end do
! Close the temperature data file
close(10)
! Calculate the monthly averages
do i = 1, num_years*num_months
monthly_avg(i) = monthly_avg(i) / 30.0
end do
! Print the monthly averages
do year = 1960, 2008
write(*,*) 'Year ', year
do month = 1, num_months
i = (year-1960)*num_months + month
write(*,*) 'Month ', month, ': ', monthly_avg(i)
end do
end do
end program avg_temp
```
这个程序假设您的温度数据文件名为“temp_data.txt”,数据文件应该包含每一天的温度数据,每一行包含四个值:年份,月份,日份和温度(以摄氏度为单位)。程序将每个月的平均温度存储在名为“monthly_avg”的数组中,并将其输出到控制台。
希望这可以帮助您解决问题!
阅读全文