Fortran Fortran 90: Display values in an array

  • Thread starter Thread starter Sue Parks
  • Start date Start date
  • Tags Tags
    Array Fortran
AI Thread Summary
The discussion centers on a program designed to calculate Fibonacci numbers up to 100, with the user seeking guidance on how to properly display these numbers in an array format. Initial attempts resulted in unexpected high integer values during the output process, indicating a potential issue with the array indexing and loop structure. Clarifications were made regarding the definition of an array and how to initialize it correctly. Suggestions included starting the loop at the correct index and ensuring proper array element references. The user also encountered a runtime error related to file handling, which was resolved by renaming the output file to avoid conflicts with the executable. Additional resources for learning Fortran were discussed, emphasizing the importance of online tutorials and textbooks for further understanding.
Sue Parks
Messages
38
Reaction score
0
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)

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 ]
 
Technology news on Phys.org
Sue Parks said:
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.
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.
Sue Parks said:
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)

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

If you have filled the elements in an array, you can use a loop to print them out, like so:
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
 
  • Like
Likes 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?

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:
Sue Parks said:
I did ask this question before. This is what I had initially. The output was not correct. How do I initialize the array?

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
[ / code ]
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).
 
  • Like
Likes Sue Parks
I think I just got it! I was looking here http://star-www.st-and.ac.uk/~spd3/Teaching/AS3013/lectures/AS3013_lecture5.pdf
 
How can I access those pdf notes. My PP slides are not adequate.
 
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
 
Sue Parks said:
How can I access those pdf notes. My PP slides are not adequate.
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.
 
  • Like
Likes Sue Parks
I can access the pdf. I was asking for other material on fortran.
 
  • #10
Sue Parks said:
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
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.
 
  • Like
Likes Sue Parks
  • #11
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
Sue Parks said:
I can access the pdf. I was asking for other material on fortran.
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.
 
  • Like
Likes Sue Parks
  • #13
Thank you
 
  • #14
FORTRAN has a list directed write feature that will print a formatted text output of a list of variables, including an array
 

Similar threads

Replies
4
Views
2K
Replies
12
Views
3K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
19
Views
6K
Replies
5
Views
8K
Back
Top