New Reply

Fortran 90 creating an array of unknown size

 
Share Thread Thread Tools
Feb21-12, 05:26 PM   #1
 

Fortran 90 creating an array of unknown size


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)
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
>> Google eyes emerging markets networks
Feb23-12, 01:45 PM   #2
 
Mentor
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.
Code:
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.
Feb24-12, 04:29 AM   #3
 
Summit like this:

Code:
subroutine(/arguments/)

real,allocatable :: array(:)

/code/

allocate ( array(x) )

/code/

deallocate ( array )

return
Mar15-12, 05:57 AM   #4
 

Fortran 90 creating an array of unknown size


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.
New Reply
Thread Tools


Similar Threads for: Fortran 90 creating an array of unknown size
Thread Forum Replies
Fortran 77 help making an empty array (or blank list if they exist in fortran) Programming & Comp Sci 5
FORTRAN 90 array problem Programming & Comp Sci 4
Fortran: creating array containing characters Programming & Comp Sci 1
Printing 1D array in Fortran as 2D array... Programming & Comp Sci 7
Co-Array Fortran Programming & Comp Sci 2