MST

星途 面试题库

面试题:Fortran在地球物理学算法实现中的数组操作

在地球物理学的某算法中,需要处理三维数组来存储不同深度、经度和纬度的物理数据。请用Fortran编写代码,实现将数组中所有元素值乘以一个常数(比如2.5),并输出处理前后数组特定位置(例如深度为2,经度为3,纬度为4)的元素值。
46.4万 热度难度
编程语言Fortran

知识考点

AI 面试

面试题答案

一键面试
program multiply_array
    implicit none
    real, dimension(10, 10, 10) :: data_array
    real :: constant = 2.5
    integer :: depth, longitude, latitude
    real :: original_value, new_value

    ! 初始化数组
    data_array = reshape((/ (i, i = 1, 1000) /), shape(data_array))

    ! 特定位置
    depth = 2
    longitude = 3
    latitude = 4

    ! 获取原始值
    original_value = data_array(depth, longitude, latitude)

    ! 所有元素乘以常数
    data_array = data_array * constant

    ! 获取新值
    new_value = data_array(depth, longitude, latitude)

    ! 输出结果
    write(*,*) '原始值 at (depth, longitude, latitude) = (', depth, ',', longitude, ',', latitude, ') 是: ', original_value
    write(*,*) '新值 at (depth, longitude, latitude) = (', depth, ',', longitude, ',', latitude, ') 是: ', new_value
end program multiply_array