Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section Arrays: Subarrays

---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

Section Arrays: Subarrays

This program demonstrates array slicing in Fortran.

program subarrays
  implicit none
  integer :: i
  integer, dimension(10) :: A = [ (i, i = 1, 10) ]
  character(len=10), parameter :: FMT = '(10I3)'

  print FMT, A
  print FMT, A(4:7)
  print FMT, A(:7)
  print FMT, A(4:)
  print FMT, A(4:7:2)
  print FMT, A(4::2)
  print FMT, A(:7:2)
  print FMT, A(7:4:-1)
end program subarrays

The above program is compiled and run using Fortran Package Manager (fpm):

Build the Program using FPM (Fortran Package Manager)

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Subarrays"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")

Run the Program using FPM (Fortran Package Manager)

exec_status = os.system("fpm run 2>/dev/null")
  1  2  3  4  5  6  7  8  9 10
  4  5  6  7
  1  2  3  4  5  6  7
  4  5  6  7  8  9 10
  4  6
  4  6  8 10
  1  3  5  7
  7  6  5  4