How to resolve the algorithm Multiple distinct objects step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Multiple distinct objects step by step in the Fortran programming language

Table of Contents

Problem Statement

Create a sequence (array, list, whatever) consisting of n distinct, initialized items of the same type. n should be determined at runtime. By distinct we mean that if they are mutable, changes to one do not affect all others; if there is an appropriate equality operator they are considered unequal; etc. The code need not specify a particular kind of distinction, but do not use e.g. a numeric-range generator which does not generalize. By initialized we mean that each item must be in a well-defined state appropriate for its type, rather than e.g. arbitrary previous memory contents in an array allocation. Do not show only an initialization technique which initializes only to "zero" values (e.g. calloc() or int a[n] = {}; in C), unless user-defined types can provide definitions of "zero" for that type. This task was inspired by the common error of intending to do this, but instead creating a sequence of n references to the same mutable object; it might be informative to show the way to do that as well, both as a negative example and as how to do it when that's all that's actually necessary. This task is most relevant to languages operating in the pass-references-by-value style (most object-oriented, garbage-collected, and/or 'dynamic' languages). See also: Closures/Value capture

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Multiple distinct objects step by step in the Fortran programming language

Source code in the fortran programming language

program multiple
  ! Define a simple type
  type T
     integer :: a = 3
  end type T

  ! Define a type containing a pointer
  type S
     integer, pointer :: a
  end type S

  type(T), allocatable :: T_array(:)
  type(S), allocatable :: S_same(:)
  integer              :: i
  integer, target      :: v
  integer, parameter   :: N = 10

  ! Create 10 
  allocate(T_array(N))

  ! Set the fifth one to b something different
  T_array(5)%a = 1

  ! Print them out to show they are distinct
  write(*,'(10i2)') (T_array(i),i=1,N)

  ! Create 10 references to the same object
  allocate(S_same(N))
  v = 5
  do i=1, N
     allocate(S_same(i)%a)
     S_same(i)%a => v
  end do

  ! Print them out - should all be 5
  write(*,'(10i2)') (S_same(i)%a,i=1,N)

  ! Change the referenced object and reprint - should all be 3
  v = 3
  write(*,'(10i2)') (S_same(i)%a,i=1,N)  

end program multiple


  

You may also check:How to resolve the algorithm Search in paragraph's text step by step in the J programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Swift programming language
You may also check:How to resolve the algorithm Range expansion step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Goldbach's comet step by step in the C++ programming language
You may also check:How to resolve the algorithm Mayan numerals step by step in the Ruby programming language