Fortran 90: Display values in an array

In summary, The conversation is about a program that calculates Fibonacci numbers from 1 to 100. The program runs fine but the person is unsure how to display the output in an array. They tried using a do loop but were unsuccessful in getting the desired result. They also mentioned getting high integer values when passing the array variable through an equation.
  • #1
Sue Parks
38
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
  • #2
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
  • #3
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:
  • #4
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
  • #5
I think I just got it! I was looking here http://star-www.st-and.ac.uk/~spd3/Teaching/AS3013/lectures/AS3013_lecture5.pdf
 
  • #6
How can I access those pdf notes. My PP slides are not adequate.
 
  • #7
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
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
  • #9
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
 

1. What is Fortran 90 and how is it different from previous versions?

Fortran 90 is a programming language commonly used in scientific and engineering applications. It is an update to the original Fortran language, with new features such as modules, recursion, and dynamic memory allocation. These features make it more modern and easier to use compared to previous versions.

2. How do I display values in an array using Fortran 90?

To display values in an array using Fortran 90, you can use the print statement followed by the array name and the desired array elements. For example, print*, array(1), array(2), array(3) will display the first three elements of the array.

3. Can I display the entire array at once?

Yes, you can display the entire array at once by using the print statement followed by the array name without specifying any specific array elements. For example, print*, array will display all elements of the array.

4. How can I format the output of the array values?

To format the output of the array values, you can use the format statement before the print statement. The format statement allows you to specify the desired format for the output, such as the number of decimal places or the width of the output. For example, format(F10.3) will display the array values with 10 spaces and 3 decimal places.

5. Can I display the array values in a specific order?

Yes, you can display the array values in a specific order by using the do loop. The do loop allows you to iterate through the array in a specified order and display the values using the print statement. You can also use the sort function to sort the array before displaying the values.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
621
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top