Fortran 90: Display values in an array

  • Context: Fortran 
  • Thread starter Thread starter Sue Parks
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 program that calculates Fibonacci numbers and the challenges faced in displaying these numbers in an array format. Participants explore issues related to array initialization, output formatting, and error handling in the context of programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion about how to display Fibonacci numbers in an array after calculating them, noting that their output includes unexpected high integer values.
  • Another participant questions the meaning of "display them in an array," suggesting a misunderstanding of array structures.
  • Concerns are raised about initializing the Fibonacci array correctly, with participants discussing the need to start loops at the correct index based on pre-assigned values.
  • One participant mentions an error related to file access when trying to write output to a file while the program is running, suggesting that the output file name should be changed to avoid conflicts.
  • There is a mention of a link to external resources for Fortran, with participants discussing the adequacy of their current materials and seeking additional references.
  • A participant notes that Fortran has a list-directed write feature that can format output for arrays, indicating a potential solution to the display issue.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding array initialization and output display, with some confusion evident. There is no consensus on the best approach to resolve the issues raised, and multiple viewpoints are presented.

Contextual Notes

Participants reference specific lines of code and potential errors without resolving the underlying issues, indicating a need for further clarification on array handling and file I/O in Fortran.

Who May Find This Useful

Individuals learning Fortran, particularly those interested in array manipulation and output formatting, may find this discussion beneficial.

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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
11
Views
2K