200 likes | 258 Views
FORTRAN PROGRAMMING PRELI MINARIES. FORTRAN is an abbreviation of FORmula TRANslation It was introduced by IBM in 1957 Its successive versions have been called FORTRAN, FORTRAN II, FORTRAN IV In 1977 American National standards Institute (ANSI) published a standard for it called FORTRAN 77
E N D
FORTRAN PROGRAMMING PRELI MINARIES FORTRAN is an abbreviation of FORmula TRANslation It was introduced by IBM in 1957 Its successive versions have been called FORTRAN, FORTRAN II, FORTRAN IV In 1977 American National standards Institute (ANSI) published a standard for it called FORTRAN 77 However FORTRAN 90 and FORTRAN 95 versions are also available. It is very useful language for scientific and engineering computations as it contains many functions for computer mathematical operations and can handle complex numbers very easily. Its syntax is very rigid and hence cannot be used for business applications . .
1. PROGRAM PRELIMINARIES I) PROGRAM :Program is a sequenced set of instructions written in a programming language, to solve a particular problem. II) SOURCE PROGRAM: Program written by user in any programming language e.g. Fortran, Pascal, C. etc. is known as source program. III)OBJECT PROGRAM: The machine code produced, after translating the source program, into corresponding machine-level-language program ,is known as object program. IV) COMPILER: It is system software that translates source program written in High Level Language (HLL) into object program in machine language. It is also called as language processor. It translates the entire program in to an object program and is not involved into its execution.
V) INTERPRETER: It is a program which translates statements of a HLL program into machine language. It translates one statement of the program at a time. It reads one statement of a HLL program, translates it into machine code and executes it. Then it goes to next statement. These terms are explained below in the Fig. 1
VI) ASSEMBLER: It is a system software that translates source program written in assembly language into the object program and is explained in Fig. 2 Fig. 2
VII) PROGRAMMING LANGUAGE: It is method of communication between computer and user. All the programming languages are broadly categorized into two types : i) Low level language (LLL) which is further of two type as shown in Fig. 3 ii) High level language (HLL) Fig. 3
(i) LLL is machine dependent. It is of two types: (a) Machine Language It consists of strings of 0’s and 1’s. Computer can understand machine language only. Different computers have different machine languages. It is very difficult to learn this language and to write program in it. (b) Assembly Language This Language substitutes letters and symbols for numbers (0’s and 1’s) in machine language. It consists of mnemonics, ( numeric characters i.e. = alphabetical), e.g. ADD, SUB, MUL, DIV, MOV ..... etc. These codes are easier to remember and therefore it is easier to learn and to write programs in it. An assembler is needed to translate the program in machine language
(ii) High Level language (HLL) It is machine independent. Instructions in HLL are not in coded form but in statement form and resemble to our ordinary English statements. High level languages are user friendly -easier to learn and faster to write. Since computer understands only machine language, compiler/ interpreter is needed to translate the programmers written in HLL into machine language. Examples of HLL are FORTRAN, COBOL, PASCAL, C,C++...etc. 2. Characters Used in Fortran Following are the set of allowed characters in FORTRAN-77: i) Letters A-Z (All upper case letters) a-z (All lower case letters) ii) Decimal digits 0 to 9 i.e. 0,1,2,3,4,5,6,7,8,9 iii) Special characters or Symbols + - . ( ) = : , * $ ’ / The blank space is also recognized as a character. The digits together with alphabets are called alphanumeric characters.
3 Program Organization A program is constructed with a text-editor, which is simply a word processor. Each program line must follow some specific rules. e.g., the various columns have specific significance as given in Table 1 and Fig. 4 for FORTRAN Language Table 1 Column Purpose 1 C or * in this column indicates a comment. 1-5 Statement labels that are used to identify lines. 6 Continuation character (any character is allowed usually 1 to 9). 7-72 Fortran program statements. 73-80 Used for short comments and everything that follows is ignored. Statement label no. FORTRAN STATEMENT 1 2 3 4 5 6 7-----------------------------72 73--------------------------80 C Continuation Code Ignored * 1 to 9 by the compiler C for Comment or to be used for identification or documentation Fig. 4
Fortran Constants and Variables 1. Constants are those which do not change during the execution of the program. e.g. 1, 234, ‘Fortran’, 3.145, . True. • DEFINITION OF VARIABLE Variables are the data names which represent constants. Their values can be changed during the execution of the program. e.g. PIE = 3.14159, NAME = ‘SHIVA’ X = 5.0, X = 25.5
VARIABLE NAME It is the name or identification given to a memory location. If a memory location is used to store integer constants, then it is called integer variableand if it is used to store real constants, it is called a real variable. Rules for a variable Name : Variable name should start with an alphabet. Other characters in variable name can be digits/alphabets. No special character is allowed. Maximum number of characters is six. e.g. valid variable names are, I A CASEC AT AI AB4 Invalid variable names are /A AB*D 4AC
3. FORTRAN CONSTANTS The FORTRAN constants can be classified broadly in two categories : I) Numeric constants II) Non-numeric constants The sub categories of each are shown in the Fig.
I) NUMERIC CONSTNATS These constants can participate in arithmetic calculations. These are classified as a) Integer b) Real c) Complex a) Integer Constants: An integer constant is also called a fixed point constant. It is any signed or unsigned whole number without a decimal point. Unsigned numbers are taken as positive. The following are acceptable as integer constants, 156 -8 12547 0 -5286 On the other hand the following are not acceptable as integer constants 21.0 (contains a decimal point) 12,357 ( contains a comma) Computer’s RAM has a limited capacity. Also memory location can store a fixed number of bits. For n-bit computer, the range of value that it can represent is from –(2n -1) to + (2n -1)
b) Real Constants: A real constant also called floating point constant, can be written in two different forms. i) Decimal Form ii) Exponential Form. i) Decimal Form: In decimal form, a real constant is written as signed or unsigned finite sequence of digits, together with a decimal point The following are acceptable as real constants, 28.3, -236.0, 24., + 0.1.87, - 234.18 The following are not acceptable as real constants, 4,356.2 (constrains a comma) - 36 (no decimal point) II) Exponential Form : In this form, a real constant consists of two parts. The first part known as mantissa is a real constant in decimal form. The second part exponent starts with the character E followed by a signed or unsigned integer constant with, at most, two digits. e.g. 23. 1 E – 4, is a real constant in E-form which stands for 23.1x10-4 The other acceptable examples are 24.123 E04, 2.6 E+2, -5.36 E-3
Whenever we write or input a real number in E-form, there is no restriction as to where decimal point is place. Hence every real constant in E-form can be written in more than two ways. e.g. 23.1 E-4, 2.31 E-3, 0.0000231E2 all represent the same number and are accepted so by the computer . Exponent may have value between -38 to 38 • Complex Constants: Complex numbers have real and imaginary parts such as 2+3 i. Since computers cannot work with imaginary numbers, programmers have developed a convention where a complex constant is represented by two real components Fortran representation : (REAL1, REAL2) Algebraic representation : (real)1,,+i (real2) The first number (REAL1) represents the real part and the second number (REAL2) represents the imaginary part of the complex number. The following are acceptable as complex constants. (4,67, -2.52) Either component may be negative. (+2.16, 0.42) Positive sign is optional (6.24 E-1, 7.74) Exponential format is permitted. the following is not acceptable as complex constants : 4, 7) Integers are not allowed. (II) Non-numeric Constants: These are of two types : • Character type constants : These constants cannot participate in arithmetic calculations. They are made up of digits, letters and special characters.
e.g. Name of a student : RAMESH Telephone No : 2644320 Regn. No : 32-PHY – 3 • Logical Constants : There are two logical constants represented by, TRUE. and FALSE. • VARIABLE NAME It is the name or identification given to a memory location. If a memory locationis used to store integer constants, then it is called integer variableand if it is used to store real constants, it is called a real variable. Rules for a variable Name : • Variable name should start with an alphabet. • Other characters in variable name can be digits/alphabets • No special character is allowed. • Maximum number of characters is six. e.g. valid variable names are, I A CASEC AT AI AB4 Invalid variable names are /A AB*D 4AC clear. For example, a number having decimal point is treated as real, and if, it has no decimal point it is considered to be an integer and so forth. But, there should be some different way for defining variable types. In FORTRAN there are two options for defining variable type.
i) Implicit typing ii) Explicit typing
Implicit Typing Implicit typing of variable name means that each variable is automatically (implicitly) assigned a data type based on the first letter of the variable name and the following rules : i) If variable names start with letters I, J, K, L, M, N then by default, type of that variable will be integer. ii) Else. The default type of the variable will be real i.e. variable names that start with the letters A-H or O-Z will be real. Example 6.7 Some examples of implicit typing.
Explicit Typing Implicit typing rules or are not applicable to character, complex or logical variables. For these there are two different ways of defining type of variable. a) ‘TYPE’ statement b) ‘IMPLICIT’ statement • ‘TYPE’ statement This statement is used to declare the type of variables. SYNTAX : There can be many declaration statements at the beginning of the program and these statements should come before executable statements. Example 8 Some examples of Explicit typing. TYPE variable 1, variable 2, .....
‘IMPLICIT’ Statement The variable name can also be declared of a particular Type based on the first letter of their name using ‘IMPLICIT’ statement. SYNTAX : IMPLICIT <TYPE> (string) Example 1 Some examples of ‘IMPLICIT’ statement. This example implies that variable name starting from letters (A-E) are integers and so forth as shown below. Example 2
However TYPE statement will over ride the IMPLICIT statement, as shown below: Example 3 So overriding ‘IMPLICIT’ statement, AVERAGE will be considered as a REAL variable instead of INTEGER variable.