Fortran Forum

  • Fortran
  • Thread starter FUNKER
  • Start date
121
0

Main Question or Discussion Point

Mentor's note (May 2012):

This thread is closed to new posts. It is over five years old and has been resuscitated many many times by people finding it in Google searches or something. If you want to post a question about Fortran programming, please go to the parent forum

https://www.physicsforums.com/forumdisplay.php?f=165

and start a new thread (topic) by clicking the "New Topic" button. Give your new thread a suitably descriptive title.

------------------------------------------------------


hey guys,
I was reading the sticky threads and did not find much help for fortran so I thought I might list a a website that has a bunch of helpful people on it. It has an awesome forum for Fortran 90-95 aswell as other languages:

www.tek-tips.com

Its a no bull**** site for those who need it.

Plus another 5c :

I have been using FORTRAN 90/95 explained by Metcalf and Reid, not very helpful :grumpy:

Regards
 
Last edited by a moderator:

Answers and Replies

20
0
Hi guys
As I m anew user for Fortran 77, so I am still struggling how to do the declaration, i mean what is the common block?I have written a code but gaveme error message about the declaration............
any one could help
 
Astronuc
Staff Emeritus
Science Advisor
18,543
1,685
Hi guys
As I m anew user for Fortran 77, so I am still struggling how to do the declaration, i mean what is the common block?I have written a code but gaveme error message about the declaration............
any one could help
Can you post the error message.

A COMMON block is a way of storing data in memory. One can store arrays, vectors and scalar data.

One can also label COMMON blocks.

They are placed in the main routine and subroutines as necessary. If one does this, then it's not necessary to put the variables in the argument of the subroutine or function subroutine.

http://labmon.io.usp.br/cursos/iof0227/fortran/common.htm [Broken]
Syntax - common / name / list-of-variables

You should know that
  • The common statement should appear together with the variable declarations, before the executable statements.
  • Different common blocks must have different names (just like variables). A variable can belong to more than one common block.
  • The variables in a common block do not need to have the same names each place they occur (although it is a good idea to do so), but they must be listed in the same order and have the same type.
 
Last edited by a moderator:
20
0
implicit none
c * LOCAL SCALARS
integer i,N
real*8 X,Y,sum1,sum2,arc
c * COMMON BLOCKS
integer dim(X),dim(Y)
real*8 area,arc
common/matpara/ area,arc,dim(X),dim(Y)
1 read(*,*) dim(X),dim(Y)
if (dim(X).ne.dim(Y)) then
print *, 'The dim of X must be equal to dim of Y'
go to 1
else
if (dim(X).le.3)
print *, 'This is not a case'
end if
end if
N=5
Many thanks for ur help this is the code, but I do not know if the declaration is enough, also cos I need to call subroutine to do a job, so do I need to redefine varible again, is the way I have entered the array X Y correct,
do 10 i=0, N-1
read(*,*) X(i), Y(i)
10 continue
when I compiled I have got this error message

Area.f: In program `MAIN__':
Area.f:6:
real*8 X,Y,sum1,sum2,arc
1
Area.f:8: (continued):
integer dim(X),dim(Y)
2
Invalid declaration of or reference to symbol `x' at (2)
Area.f:6:
real*8 X,Y,sum1,sum2,arc
1
Area.f:8: (continued):
integer dim(X),dim(Y)
2
Invalid declaration of or reference to symbol `y' at (2)
Area.f:8:
integer dim(X),dim(Y)
1 2
Invalid declaration of or reference to symbol `dim' at (2)]
Area.f:6:
real*8 X,Y,sum1,sum2,arc
1
Area.f:9: (continued):
real*8 area,arc
2
Invalid declaration of or reference to symbol `arc' at (2)]
Area.f:6:
real*8 X,Y,sum1,sum2,arc
1
Area.f:10: (continued):
common/matpara/ area,arc,dim(X),dim(Y)
2
Invalid declaration of or reference to symbol `arc' at (2)]
Area.f:8:
integer dim(X),dim(Y)
1
Area.f:10: (continued):
common/matpara/ area,arc,dim(X),dim(Y)
2
Invalid declaration of or reference to symbol `dim' at (2)]
Area.f:11:
1 read(*,*) dim(X),dim(Y)
^
Non-numeric character at (^) in label field [info -f g77
Area.f:12:
if (dim(X).ne.dim(Y)) then
^
Invalid first character at (^) [info -f g77 M LEX]
Area.f:13:
print *, 'The dim of X must be equal to dim of Y'
^
Non-numeric character at (^) in label field [info -f g77
Area.f:14:
go to 1
^
Non-numeric character at (^) in label field [info -f g77
Area.f:15:
else
^
Invalid first character at (^) [info -f g77 M LEX]
Area.f:16:
if (dim(X).le.3)
^
Non-numeric character at (^) in label field [info -f g77
Area.f:17:
print *, 'This is not a Polygon'
^
Non-numeric character at (^) in label field [info -f g77
Area.f:18:
end if
^
Non-numeric character at (^) in label field [info -f g77
Area.f:19:
end if
^
Invalid first character at (^) [info -f g77 M LEX]
Area.f:20:
N=5
^
Invalid first character at (^) [info -f g77 M LEX]
Area.f:22:
read(*,*) X(i), Y(i)
^
Invalid first character at (^) [info -f g77 M LEX]
Area.f:23:
10 continue
 
Borek
Mentor
28,132
2,641
if (dim(X).ne.dim(Y)) then
print *, 'The dim of X must be equal to dim of Y'
If they are identical, why do you read both of them, instead of reading one and use it twice?
 
Astronuc
Staff Emeritus
Science Advisor
18,543
1,685
The statement:
integer dim(X),dim(Y)

requires the X and Y be integers. In F77, the subscripted variables are integers. Later versions for Fortran may be more flexible, but I'm not familiar with these.

By convention, variable names starting with I, J, K, L, M, N are integers in FORTRAN (F77), and A-H,O-Z are real unless otherwise declared.

Also, I'm not by the listing if the FORTRAN statements start in the 7th column, or 1st, but the 1st column is reserved for C (comment statements) and cols 2-6 for STATEMENT numbers.

Here is another tutorial - http://web.utk.edu/~prdaves/Computerhelp/Unix_Fortran_Overview.htm

We do have another Fortran thread around here since F77 a legacy language, which is still used by us dinosaurs from the pre-internet age.
 
20
0
Hi guyes
I would be highly grateful if you could help me in these questions as I need to build code in Fotran 77 for the first time
In fortran 77 what is the difference between write(*, 'ghghfy') and write(9, 'ghghf)
Why we do this declaration
Parameter (zero=0.0d0, one=1.0d0, two=2.0d0, thr=3.0d0)
what is the meaing of
pi=two*dacos(zero)
I mean what is the job of dcos function
 
jtbell
Mentor
15,404
3,197
It looks like you want to use two arrays named X and Y, but you don't know (before you run the program) how big you want those arrays to be. Instead, you want the program to read the array size, before reading the data for the arrays. Is this correct?

I don't want to proceed further until I know for sure what you are trying to do.
 
20
0
It looks like you want to use two arrays named X and Y, but you don't know (before you run the program) how big you want those arrays to be. Instead, you want the program to read the array size, before reading the data for the arrays. Is this correct?

I don't want to proceed further until I know for sure what you are trying to do.
thanks, actually I need dim of the arrays to be 5, in the case which I am dealing with now, but I want to write a general code.
 
jtbell
Mentor
15,404
3,197
Then you can declare the arrays either using

Code:
      real*8 x(5), y(5)
or as

Code:
      parameter (n=5)
      real*8 x(n), y(n)
The "parameter" statement declares a named constant with the name "n". "n" is not a variable and does not even have any memory allocated for it. The compiler substitutes "5" whenever it sees "n" in the code. So you cannot change its value while the program is running (i.e. after the program is compiled). Its advantage is that you can easily change 5 to, say, 100, in the "parameter" statement, and re-compile your code, without having to search for all the places where you use 5 as the array size.
 
20
0
Hey there, please anyone could give me a hand
your help is greatly appreciated, actually I have written that code, but still giving me an errors, so still struggling as a begginer in Fortran 77, here the code and the errors
c * This program reads the five vertices of an irregular Polygon then c* calculate its area and periemeter
c * The area of an irrigular Polygon with n vertices is(x0*y1+x1*y2+....+x_n-1*y0)-(y0*x1+y1*x2+....+y_n-1*x0)
c *
c *
c *
implicit none
c * LOCAL SCALARS
integer i,n
c * PARAMETER
parameter (n=5.0d0)
real*8 X(1,n),Y(1,n),sum1,sum2
c * COMMON BLOCKS
real*8 area,arc
common/matpara/ area,arc
c * 1 read(*,*) dim(X),dim(Y)
c *if (dim(X).ne.dim(Y)) then
c * print *, 'The dim of X must be equal to dim of Y'
c * go to 1
c *else
c * if (dim(X).le.3)
c * print *, 'This is not a Polygon'
c * end if
c *end if
do 10 i=1, n
read(*,*) X(1,i), Y(1,i)
10 continue
call calarea(X,Y,sum1)
call calarea(Y,X,sum2)
c * area=(calarea(X,Y)-calarea(Y,X))/2.0
area=(sum1-sum2)/2.0
c * THis subroutine to calculate the area
subroutine calarea(A,B,sum)
implicit none
c * LOCAL VARIABLES
integer i, n
c * PARAMETER
parameter (n=5.0d0)
c * ARGUMENTS
real*8 A(1,n),B(1,n),sum
sum=0.0
do 10 i=1,n-1
sum=sum+(A(i)*B(i+1))
end do
sum=sum+(A(n)*B(1))
end
c * The following step to calculate the arc lenght of the Polygon by summing over the distance between its vertices
arc=0.0
do 10 i=1, n-1
arc=arc+dsqrt((X(i)-X(i+1))**2 + (Y(i)-Y(i+1))**2)
end do
arc=arc+dsqrt((X(n-1)-X(1))**2 + (Y(n)-Y(1))**2)
print*,area,arc
**********
Area-PoLygon.f: In program `MAIN__':
Area-PoLygon.f:25:
read(*,*) X(1,i), Y(1,i)
^
Invalid first character at (^) [info -f g77 M LEX]
Area-PoLygon.f:26:
10 continue
^
Non-numeric character at (^) in label field [info -f g77 M LEX]
Area-PoLygon.f:30:
area=(sum1-sum2)/2.0
^
Invalid first character at (^) [info -f g77 M LEX]
Area-PoLygon.f:32:
subroutine calarea(A,B,sum)
^
Invalid first character at (^) [info -f g77 M LEX]
Area-PoLygon.f:33:
implicit none
^
Invalid first character at (^) [info -f g77 M LEX]
Area-PoLygon.f:8:
integer i,n
1
Area-PoLygon.f:35: (continued):
integer i, n
2
Invalid declaration of or reference to symbol `i' at (2) [initially seen at (1)]
Area-PoLygon.f:8:
integer i,n
1
Area-PoLygon.f:35: (continued):
integer i, n
 
156
0
Have you tried putting 7 or 8 leading spaces before your code lines yet?

Columns 1 to 6 are reserved for labels and the continuation character.

Obviously something like
Code:
123      continue
Wouldn't need leading spaces in front of the 123, but does need the spaces between the 3 of 123 and the c of continue.
 
20
0
Have you tried putting 7 or 8 leading spaces before your code lines yet?

Columns 1 to 6 are reserved for labels and the continuation character.

Obviously something like
Code:
123      continue
Wouldn't need leading spaces in front of the 123, but does need the spaces between the 3 of 123 and the c of continue.
yes you right, but Is the way I define the array X &Y which contains real numbers correct or no?
 
jtbell
Mentor
15,404
3,197
yes you right, but Is the way I define the array X &Y which contains real numbers correct or no?
No, it is not correct. Array indexes (inside the parentheses) must be integers.
 
20
0
No, it is not correct. Array indexes (inside the parentheses) must be integers.
Hi there yes they are integers cos I have already changed n to 5 not 5.0d, I mean X(1,n)is correct declaration for an array of n elements?????????
 
156
0
Code:
       parameter (n=5.0d0)
       real*8 X(1,n),Y(1,n),sum1,sum2
This doesn't look right to me...

You appear to be defining n as a real number (floating point), then using it to define an array.

Also, why are you defining X and as two dimensional arrays?

Surely X(n) and Y(n) would be simpler?

So I reckon that
Code:
       Parameter( n=5)
       real*8 X(n), Y(n)
would be a step forward.
 
20
0
Code:
       parameter (n=5.0d0)
       real*8 X(1,n),Y(1,n),sum1,sum2
This doesn't look right to me...

You appear to be defining n as a real number (floating point), then using it to define an array.

Also, why are you defining X and as two dimensional arrays?

Surely X(n) and Y(n) would be simpler?

So I reckon that
Code:
       Parameter( n=5)
       real*8 X(n), Y(n)
would be a step forward.
thanks, yeah I have n=5 not 5.0d0, if I define the array as real*8 X(n) , then how can I refer to the nth term in the array which is X(n) isnit??
 
20
0
Code:
       parameter (n=5.0d0)
       real*8 X(1,n),Y(1,n),sum1,sum2
This doesn't look right to me...

You appear to be defining n as a real number (floating point), then using it to define an array.

Also, why are you defining X and as two dimensional arrays?

Surely X(n) and Y(n) would be simpler?

So I reckon that
Code:
       Parameter( n=5)
       real*8 X(n), Y(n)
would be a step forward.
if I define the array like this then how can I refer to the nth term of it which is X(n)?????????
 
156
0
If you want to access the 5th element of X (FORTRAN arrays start at 1, by the way, or at least they used to when I was programming), then it's simply X(5).

for instance:

Code:
       do 100 i = 1,5
       print ( *,1000 ) X(i)
100  continue

      stop
1000 format ( 1x, f6.2 )
        end
should print out all five elements of X.
 
89
0
I have written this bit of code in Fortran 77 but it gives me an errors message about n
integer n
real u(n), v(n)
c
c
print*, "Enter the number n="
read *, n

It gives me invaild declaration of n,u,v??????????/should be something easy, hope anyone could point it out:confused:
 
365
0
Sally,
I have read your code and understand what you are trying to do. In fact, I have done exactly the same exercise two days ago for a different reason, except the perimeter did not interest me.

First, I suggest you do your programming (in any language) step by step.
You have done very well the first step in figuring out and documenting the formula required to do the work.
The second step is to write a pseudo-code which you will follow when coding in the language of your choice, which in your case is Fortran. By the way, is Fortran your first computer language (it was in my case), or you already programme in other languages?
The pseudocode is a sequence of steps that describes the algorithm you follow to solve your problem. It can be in any language (English, Fortran...) that you can understand. It can even be an imaginary language.
The third step is to code the programme in the language of your choice. You need to have a rudimentary knowledge of the language, which it seems to me is lacking in your case. All is not lost, because if you don't, you need a compiler to help you do tests, together with your text book and your teacher, you will acquire the knowledge.
In your particular case, I think it is important to code a few lines at a time, compile, get rid of compilation errors. If you cannot, read up the textbook or the compiler's user guide to find the errors before you move on. This way, you will have a smaller problem at hand.

Having said all that, I will give you a few hints as to where to start. I suggest you start from scratch, using all the materials you have done so far as reference.

Step 1. Documentation.
To find the area S of a polygon where the n vertices are (x1,y1), (x2, y2), ...(xn,yn), the forllowing formula may be used:
S=[Sum of (x(i)*y(i+1)-x(i)-y(i+1))]/2
for i=1 to n, and assuming (x(n+1)=x(1), and (y(n+1)=y1)
The Perimeter P of the same polygon is the same as in your code:
P=[sum of sqrt((x(i+1)-x(i))**2+(y(i+1)-y(i))**2))]
for i=1 to n, and assuming (x(n+1)=x(1), and (y(n+1)=y1)

Your idea of calling the area twice and subtacting before dividing by 2 is therefore not required. It can be done in one and the same step.


Now the pseudocode:
1. Input the number of points, n
2. Input the coordonates of each point Pi= (x(i), y(i)) for points 1 to n
3. print the input data (for purposes of checking).
4. copy point P1 to Pn+1 to facilitate the summation.
5. calculate s=area(x,y,n) where area is a function that returns the value
6. calculate p=perimeter(x,y,n) where periimeter is a function that returns the value
7. output the values of and p.
8. end of program.

Program structuring
The program should have three units, main, functions area and perimeter.
They would look like this. All you have to do is to use the following template and fill in your code where there are...
Coompile as often as possible so you will know exactly where the problem lies. Post if you have errors you do not understand. I am sure Zeitghost and I will be glad to help.
Code:
C      MAIN PROGRAM
C      Step 1: input given data
        REAL*8 AREA          !Function 
        REAL*8 PERIMETER  !Function
        REAL*8 X,Y            ! Input parameter
        INTEGER N              ! Inpu parrameter
C        ....
        S=AREA(X,Y,N)
        P=PERIMETER(X,Y,N)
        PRINT *, "AREA=",S
        PRINT *,"PERIMETER=",P
        STOP
        END
C
        REAL*8 FUNCTION AREA(X,Y,N)
C        ....
        RETURN
        END
C
        REAL*8 FUNCTION PERIMETER(X,Y,N)
        ....
        RETURN
        END
 
Last edited:
365
0
Angela,
Unfortunately, in fortran, we cannot declare the dimension (i.e assign memory) using a variable that is yet unknown to the compiler, contrary to what we can do in C and C++ using malloc().
The only exception is when this happens in a sub-program where the memory has already been assigned in the calling program. If the array and the dimensions appear in the formal parameters, the compiler will accept it. For example, it is OK to code the following, because the compiler will be sure that the calling program assigns memory properly before passng onto the subprogram.
Code:
      SUBROUTINE SUB1(U, V, n)
      INTEGER n
      REAL U(n), V(n)
      RETURN
      END
      REAL A(5), B(5)
      INTEGER I
      I=4
      CALL SUB1(A,B,I)
      STOP
      END
To do what you want to do, most of the time people would assign a maximum dimension to the array and use only part of it, as much as the input requires, up to the maximum. A check should be made to ensure that the maximum is not exceeded.
 
89
0
Angela,
Unfortunately, in fortran, we cannot declare the dimension (i.e assign memory) using a variable that is yet unknown to the compiler, contrary to what we can do in C and C++ using malloc().
The only exception is when this happens in a sub-program where the memory has already been assigned in the calling program. If the array and the dimensions appear in the formal parameters, the compiler will accept it. For example, it is OK to code the following, because the compiler will be sure that the calling program assigns memory properly before passng onto the subprogram.
Code:
      SUBROUTINE SUB1(U, V, n)
      INTEGER n
      REAL U(n), V(n)
      RETURN
      END
      REAL A(5), B(5)
      INTEGER I
      I=4
      CALL SUB1(A,B,I)
      STOP
      END
To do what you want to do, most of the time people would assign a maximum dimension to the array and use only part of it, as much as the input requires, up to the maximum. A check should be made to ensure that the maximum is not exceeded.
The matter is that I need to write a general code for any n and then I can enter n as input after compile the code
Code:
        print*, "Enter  n="
        read(*,*)  n
and I need to do some calculation on the arrays U and V in the main program, so as far as I understand from you this is impossible, cos the idea of calling subroutine not enough as I want use just n and not giving any value to it at any stage of the program
 
alphysicist
Homework Helper
2,238
1
Anglea,

The matter is that I need to write a general code for any n and then I can enter n as input after compile the code
Code:
        print*, "Enter  n="
        read(*,*)  n
and I need to do some calculation on the arrays U and V in the main program, so as far as I understand from you this is impossible, cos the idea of calling subroutine not enough as I want use just n and not giving any value to it at any stage of the program

I don't believe this is possible in fortran77 in your main program, as mathmate pointed out. You can do it in fortran95, if that is an option for you.
 
89
0
I have included a common block in my code, but it gives me an error message saying my common block is too large, I s there anything in particular I need to be careful with using common Blocks, any help would be much appreciated...........:zzz:
 

Related Threads for: Fortran Forum

Replies
8
Views
2K
  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
3K
  • Last Post
2
Replies
32
Views
9K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
7
Views
5K
Top