Fortran 90 creating an array of unknown size


by max2112
Tags: array, creating, fortran, size, unknown
max2112
max2112 is offline
#1
Feb21-12, 05:26 PM
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"
read(*,*) size
call sub(size)



end program arrays

I am stuck, and dont know why this isnt working.
Attached Files
File Type: pdf *** 3.pdf (25.4 KB, 3 views)
Phys.Org News Partner Science news on Phys.org
SensaBubble: It's a bubble, but not as we know it (w/ video)
The hemihelix: Scientists discover a new shape using rubber bands (w/ video)
Microbes provide insights into evolution of human language
Mark44
Mark44 is online now
#2
Feb23-12, 01:45 PM
Mentor
P: 21,073
Quote Quote by max2112 View Post
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.
billiards
billiards is offline
#3
Feb24-12, 04:29 AM
P: 745
Summit like this:

subroutine(/arguments/)

real,allocatable :: array(:)

/code/

allocate ( array(x) )

/code/

deallocate ( array )

return

Louisa
Louisa is offline
#4
Mar15-12, 05:57 AM
P: 24

Fortran 90 creating an array of unknown size


Quote Quote by max2112 View Post
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.


Register to reply

Related Discussions
Fortran 77 help making an empty array (or blank list if they exist in fortran) Programming & Computer Science 5
FORTRAN 90 array problem Programming & Computer Science 4
Fortran: creating array containing characters Programming & Computer Science 1
Printing 1D array in Fortran as 2D array... Programming & Computer Science 7
Co-Array Fortran Programming & Computer Science 2