# Fortran 90: Display values in an array

1. Oct 31, 2015

### Sue Parks

I have a program that calculates the Fibonacci numbers from 1 to 100. The program runs fine. I am not sure how to take my output and display them in an ARRAY. Any ideas?

I tired to pass the array variable through the equation, but my output was weird. I would have 1, 1, 3347...... Basically, I would get the first two values, and then once the program entered my do loop, I would get very high integer values . I tried to use a DO loop at the end of the program, but I was unsuccessful.
EXAMPLE:
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2)

Code (Fortran):

Program large_integers
IMPLICIT NONE
Integer , PARAMETER :: M = 100

Integer, DIMENSION (M)  ::FIBONACCI
integer, parameter :: IK = selected_real_kind(25)
Integer (IK) ::  fn, f2, f1
INTEGER :: n,  I
!allocate (FIBONACCI(fn))
! OPEN(14, FILE = "MY_FIBONACCI.out", ACTION = "WRITE", STATUS = "unknown")

f1 = 1
f2 = 1

IF (m >= 1) THEN
PRINT *, f2
END IF

IF (m >= 2) THEN
PRINT *, f1
END IF

DO n=4,m
fn = f1 + f2
PRINT *, fn
f2 = f1
f1 = fn
END DO

!DO i = 1, m
!write (14,*) FIBONACCI(fn)
!END do

CLOSE(14)

END Program large_integers
[ / code ]

2. Nov 1, 2015

### Staff: Mentor

What do you mean, "display them in an array"? I think you might be confused as to what an array is, as you asked a similar question in another thread.
An array is a computer data structure in which each element is accessed by an index. You can display the numbers in the array, but I don't have any idea what you mean by saying you want to display them in an array.

If you have filled the elements in an array, you can use a loop to print them out, like so:
Code (Fortran):

! Assumes the array Arr has already been initialized, and N is initialized to the size of the array
do i = 1, N
print *, Arr[i]
end do

3. Nov 1, 2015

### Sue Parks

I did ask this question before. This is what I had initially. The output was not correct. How do I initialize the array?

Code (Fortran):

IMPLICIT NONE
Integer , PARAMETER :: M = 100
Real, DIMENSION (M) ::FIBBONACCI
INTEGER :: I, N

OPEN(14, FILE = "MY_FIBONACCI.out", ACTION = "WRITE", STATUS = "unknown")

FIBBONACCI(1) = 1
FIBBONACCI(2) = 1

do i=1, m
FIBBONACCI(i) = FIBBONACCI(i-2) + FIBBONACCI(i-F1)
WRITE (14,*) I, FIBBONACCI(i)
print *, I, Fibbonacci(i)
end do

Last edited: Nov 1, 2015
4. Nov 1, 2015

### Staff: Mentor

Minor point -- you're putting in an extra ending code tag. At the end of your code there should be a [/code] tag. Omit the one that looks like this: [ / code]

Your loop needs to start at 3, not 1, since you have already assigned values to fibbonacci(1) and fibbonacci(2). BTW, there should only be one 'b' in Fibonacci, but of course that doesn't affect how your program works.

In the first line of your do loop, the last expression should be FIBBONACCI(i-1), not FIBBONACCI(i-F1).

5. Nov 1, 2015

6. Nov 1, 2015

### Sue Parks

How can I access those pdf notes. My PP slides are not adequate.

7. Nov 1, 2015

### Sue Parks

What does this error mean? I have looked up the error, but I'm on a mac. I am running the file on a server, using fortran:

[parksse3@compile ~]$gfortran FIBBONACCI_NUMBER.f90 -o MY_FIBONACCI.out [parksse3@compile ~]$ ./MY_FIBONACCI.out
At line 9 of file FIBBONACCI_NUMBER.f90 (unit = 14, file = '')
Fortran runtime error: Text file busy

8. Nov 1, 2015

### Staff: Mentor

Can't you just open the pdf?
On my computer, I have to click OK on a blank Adobe Reader dialog box, and then I have to refresh the browser to open the file.

9. Nov 1, 2015

### Sue Parks

I can access the pdf. I was asking for other material on fortran.

10. Nov 1, 2015

### Staff: Mentor

You had this problem before, as I recall. Your executable is MY_FIBONACCI.out, so you can't open it for writing while your program is running. Give you output file a different name -- that should work.

11. Nov 1, 2015

### Sue Parks

That makes sense.

I was looking for a link to the pdf slides. The link posted above is Lecture 5. Is there a way I can view all lecture slides??

12. Nov 1, 2015

### Staff: Mentor

I don't have a fortran compiler, but when I have a question about some aspect of fortran, I just do a web search, such as "fortran array", which gives me a lot of hits. I'm sure there are lots of fortran textbooks around -- check on Amazon, and lots of online tutorials.

13. Nov 1, 2015

### Sue Parks

Thank you

14. Nov 1, 2015

### FactChecker

FORTRAN has a list directed write feature that will print a formatted text output of a list of variables, including an array