Fortran 90 creating an array of unknown size

by max2112
Tags: array, creating, fortran, size, unknown
P: 1
I was given an assignment to create a program that solves the Josephus Problem ([URL="http://en.wikipedia.org/wiki/Josephus_problem"]. the program needs to work for n amount of people, and it must use an array.

Problem:
I need to create a subroutine that will allow the user to read(*,*) n
and create an array that is n big.

Attempt:

subroutine sub(n)
implicit none
integer :: n, i
integer :: array(n)
do i = 1, n
array(i) = 0
end do
end subroutine sub

program arrays
implicit none

integer :: size, i
write(*,*) "how big is your array"
call sub(size)

end program arrays

I am stuck, and dont know why this isnt working.
Attached Files
 *** 3.pdf (25.4 KB, 3 views)
Mentor
P: 19,753
 Quote by max2112 I was given an assignment to create a program that solves the Josephus Problem ([URL="http://en.wikipedia.org/wiki/Josephus_problem"]. the program needs to work for n amount of people, and it must use an array. Problem: I need to create a subroutine that will allow the user to read(*,*) n and create an array that is n big. Attempt: Mod note: I added a [code] tag at the top of your code, and a [/code] tag at the bottom. You should do that when you post code as it makes your code easier to read, by preserving your indentation. subroutine sub(n) implicit none integer :: n, i integer :: array(n) do i = 1, n array(i) = 0 end do end subroutine sub program arrays implicit none integer :: size, i write(*,*) "how big is your array" read(*,*) size call sub(size) end program arrays I am stuck, and dont know why this isnt working.
Here's a link to an article about arrays in fortran 90 - http://orion.math.iastate.edu/burkar...an_arrays.html. Take a look at the whole article, but especially the section on dynamic arrays.
 P: 741 Summit like this: subroutine(/arguments/) real,allocatable :: array(:) /code/ allocate ( array(x) ) /code/ deallocate ( array ) return
P: 24

Fortran 90 creating an array of unknown size

 Quote by max2112 I was given an assignment to create a program that solves the Josephus Problem ([URL="http://en.wikipedia.org/wiki/Josephus_problem"]. the program needs to work for n amount of people, and it must use an array. Problem: I need to create a subroutine that will allow the user to read(*,*) n and create an array that is n big. Attempt: subroutine sub(n) implicit none integer :: n, i integer :: array(n) do i = 1, n array(i) = 0 end do end subroutine sub program arrays implicit none integer :: size, i write(*,*) "how big is your array" read(*,*) size call sub(size) end program arrays I am stuck, and dont know why this isnt working.
Why do you think that is not working?
You didn't print out anything after the value of "n" is typed.

 Related Discussions Programming & Computer Science 5 Programming & Computer Science 4 Programming & Computer Science 1 Programming & Computer Science 7 Programming & Computer Science 2