Fortran 90 creating an array of unknown size

In summary: You only wrote out "In summary, the program needs to work for n amount of people, and it must use an array.
  • #1
Technology news on Phys.org
  • #2
  • #3
Summit like this:

Code:
subroutine(/arguments/)

real,allocatable :: array(:)

/code/

allocate ( array(x) )

/code/

deallocate ( array )

return
 
  • #4
  • #5

Creating an array of unknown size in Fortran 90 can be achieved using the ALLOCATABLE keyword. This allows for dynamic allocation of memory based on user input. In the case of the Josephus Problem, the size of the array would depend on the number of people (n) given by the user. The ALLOCATE statement can be used within a subroutine to allocate memory for the array based on the value of n.

Here is an example of how the subroutine could be modified to use the ALLOCATABLE keyword:

subroutine sub(n)
implicit none
integer :: n, i
integer, allocatable :: array(:) ! Declare array as allocatable

! Allocate memory for array based on value of n
allocate(array(n))

do i = 1, n
array(i) = 0
end do
end subroutine sub

It is important to note that the ALLOCATE statement should also be used in the main program to allocate memory for the array before calling the subroutine. This can be done by declaring the array as allocatable and using the ALLOCATE statement with the desired size before calling the subroutine.

Additionally, the DO loop in the subroutine can be replaced with the FORALL statement, which is more efficient for initializing array elements. The modified subroutine would look like this:

subroutine sub(n)
implicit none
integer :: n
integer, allocatable :: array(:) ! Declare array as allocatable

! Allocate memory for array based on value of n
allocate(array(n))

! Initialize array elements to 0
forall (i = 1:n)
array(i) = 0
end forall
end subroutine sub

I hope this helps with your assignment. Remember to always read the Fortran language specification and documentation for more information on how to use specific features and statements.
 

1. How do I create an array of unknown size in Fortran 90?

In Fortran 90, you can create an allocatable array using the ALLOCATABLE keyword. This allows you to define an array without specifying its size at compile time.

2. Can I change the size of an allocatable array in Fortran 90?

Yes, you can use the ALLOCATE statement to dynamically change the size of an allocatable array during runtime. You can also use the DEALLOCATE statement to free up memory once the array is no longer needed.

3. How do I access elements in an allocatable array in Fortran 90?

You can use the same syntax as a regular array to access elements in an allocatable array. For example, array(i) would access the ith element in the array. However, keep in mind that index values must be within the bounds of the current size of the array.

4. Can I use logical expressions to specify the size of an allocatable array in Fortran 90?

Yes, you can use logical expressions to specify the size of an allocatable array in the DIM argument of the ALLOCATE statement. For example, DIM = (2*N) would create an array with a size of twice the value of N.

5. How do I deallocate an allocatable array in Fortran 90?

To deallocate an allocatable array, you can use the DEALLOCATE statement. This will free up the memory that was allocated for the array and make it available for other use. It is important to deallocate arrays when they are no longer needed to prevent memory leaks.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
25
Views
1K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top