FORTRAN programming language Question with Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
1. Describe briefly about FORTRAN programming language. Write its uses and application.
FORTRAN is an acronym for FORmula TRANslation. It was designed to allow easy translation of math formulas into code. It was the first high-level language, using the first compiler ever developed. Some of the features of this language are as listed below:
•Simple to learn
•Machine independent
•Efficient execution
•Natural ways to express mathematical functions
FORTRAN is useful for applications that are computational-bound rather than I/O bound. Some of its applications are:
•Used in processing mathematical equations, since its execution time is fast.
•Other applications include scientific, statistical and engineering problems.
2.Illustrate the difference between FORTRAN and C programming language.
The differences between FORTRAN and C programming language are as listed below:
|
SN
|
FORTRAN
|
C Programming
|
|
1.
|
Designed for numerical computation
|
Designed for many other purposes
|
|
2.
|
Easier to learn
|
It is comparatively difficult to learn
|
|
3.
|
It allows maximal speed of execution
|
It is comparatively slower in execution than
|
|
FORTRAN
|
||
|
4.
|
FORTRAN compilers emit much better
|
Compilers are universally available but produces
|
|
diagnostic messages
|
less better diagnostic messages
|
|
|
5.
|
It is very specific in its scope.
|
It is very broad in scope and is more powerful
|
|
due to facility of pointers and manipulation of
|
||
|
strings.
|
||
|
6.
|
It is case insensitive.
|
It is case sensitive.
|
|
7.
|
It follows one statement per line
|
One line can contain more than one statement.
|
|
principle.
|
||
|
8.
|
Statements are not terminated by
|
Statements are terminated by semicolon.
|
|
semicolon.
|
||
|
9.
|
Variable declaration is optional due to
|
Variable must be declared before it is used.
|
|
implicit declaration feature.
|
||
|
62
|
|
10.
|
Implied do loop facility increases
|
Such feature is not available.
|
|
flexibility.
|
||
|
11.
|
Supports arithmetic of complex number.
|
Arithmetic of complex number is not supported.
|
|
12.
|
Array index ranges from 1 to N
|
Array index ranges from 0 to N-1
|
3. Define FORTRAN constants, variables and data type. List the rules for naming a variable.
FORTRAN constants, variables and data type are not much different to those of C language.Constants are the fixed values that remain same throughout the program. Variables are simply the name of the memory location which is used for storing data. Data types are those entities which give the information about what type of value a variable can hold and also represents how much memory space is required by the variable in the memory. The generally used data types in FORTRAN are integer, real, and character.
The rules for naming a variable are:
•First character must be an alphabet.
•FORTRAN verbs should not be used as name of variables
•Special characters are not allowed.
4. List different types of constants with examples.
The different types of constants in FORTRAN are:
•Numeric Constants: constants related to numbers are numeric constants.
▪Integer constants: There are whole numbers, written without decimal point. The numbers 1, 13, 45, -35 etc are examples of integer constants.
▪Real constants: These are numbers with fractional part. It can further be represented in two forms
o Fractional form: 12.4, 44.12, -82.615
o Exponential form: it contains mantissa, which is real constant, and exponent – which is an integer constant. It is represented as:
0.375E14: Here, 0.375 is mantissa and 14 is exponent. 0.375E14 is equivalent to 0.375 x 1014
•Character constants: can contain single character or list of characters. These constants are
enclosed by pair of singe quotation mark. For example: ‘a’, ‘1’, ‘sample’ etc.
63
5. Explain briefly about the implicit declaration of variables in FORTRAN language.
Implicit declaration of variables is special feature of FORTRAN language. According to this feature, when variables are not declared, then all variables starting with alphabets i, j, k, l, m, or n are, by default, considered as integer variables while those starting with others letters are real type variables. Also, implicit type declaration can be done using implicit verb of FORTRAN. Its general form is:
implicit type(..), type(..)
For example, implicit integer(a) symbolizes that variables starting with a will be an integer. Similarly, implicit real(i-k), integer(a-f, x-z): this declaration will cause all variables starting from i, j, and k to be
real while variables starting with a, b, c, d, e, f, x, y, z are taken as integer.
6. Is it necessary to declare a variable in FORTRAN? Give reasons to support your answer.
It is not necessary to declare a variable if we follow the convention of implicit declaration. But not
following the convention can cause problem in the program. Since there can be mix of different types inadvertently. For example, an integer variable may be assigned a real value after some data manipulation, which causes the values to be truncated and gives undesired results. So, it is always better to declare a variable.
7. What are FORTRAN verbs? Explain how it differs from a variable.
FORTRAN verbs similar to keywords in C language. These are set of words having a predefined meaning and are used for specific purpose. Some examples of verbs include integer, read, write, if, else, then, continue, stop.
Variables and verbs are completely distinct elements of FORTRAN language. Variables represent the storage space while verbs are simply words with predefined meaning. Verbs cannot be used to
name a variable.
8.Explain arithmetic operators with examples. How does the operator ** work in FORTRAN language.
The different arithmetic operators are +, -, *, /, and **. For addition of two operands, + symbol is used, while -, * and / are used for subtraction, multiplication and division purpose respectively. For example these operators can be used as: a + b, x – y, x*2*c, y/x etc. The ** operator is used for the exponentiation. For example we need to determine the value of 47 then we can use ** operator as: 4**7 which represents 4 multiplied 7 times.
64
9. What are library functions? Give few examples. Explain how function max() works in FORTRAN
language.
The predefined functions which are used for specific purpose are called library functions. These functions can be directly used in our calculations. For example, abs(), exp(), sqrt(), max() etc are few library functions. Function max() returns the maximum value from the list of numbers provided to it.
For example: m = max(a, b, c, d) returns the maximum of four variables and assigns to variable m.
10. Describe about the FORTRAN coding sheet. Explain what can be done for the continuation of the
statement in next line.
The coding sheet of FORTRAN can be explained using the following diagram.
|
1
|
5
|
6
|
7
|
72
|
73
|
80
|
|
Line
|
Comment
|
|||||
|
number
|
FORTRAN codes are written in this section
|
are written
|
||||
|
in this
|
||||||
|
section
|
||||||
•Coding sheet has 80 columns.
•Columns 1 to 5 are used to write line numbers which must be a positive integer. oWriting C or * in Column 1 indicates that line in coding section as comment.
•Column 6 is used for continuation. If a statement is long enough and cannot adjust in columns 7 – 72 then next line can be used for continuing the statement by placing the last character of last statement in the next line of 6th column. The symbol $ can also be used for continuation.
•Columns 7 – 72 are used for writing codes
•Columns 73 – 80 are used for writing comments
65
11. What do you mean by format statement? Explain different types of format used in FORTRAN with
examples.
Format statement is a non executable statement, which is used to specify the different format for formatted input and output. There are different types of format available in FORTRAN but few commonly used are:
•I format: It is for integer numbers. Its general form is Iw, where w is the width for the
integer data. For example: FORMAT (I5, I7) represents two integer numbers with width 5 and 7 respectively.
•F format: It is used for fractional numbers. Its general form is Fw.d, where w is the total width for the number and d represents the number of decimal number after decimal point. For example: FORMAT (F7.3, F6.2) represents two real numbers; one with total width of 7 and 3 numbers after decimal point, another number with total width of 6 and 2 numbers after decimal point.
•E format: It is used for real numbers with exponential form. The general form for this format is Ew.d, where w is the total width of the number and d represents the decimal width of the mantissa. For example: FORMAT (E7.2) represents a real number having a total width of 7 and mantissa has two numbers after decimal point.
•A format: It is used for characters. The general form is Aw, where w is the width. For
example: FORMAT (A5) represents the only five characters to be read or displayed. But FORMAT (A) represents the width as defined in declaration.
•X format: It is used to skip columns for displaying data on the screen. Its general form is nX,
where n is the number of columns to be skipped. For example: FORMAT (I5, 3x, I3) is used to display two integers with width 5 and 3 respectively and three spaces in between them.
12. Write the general syntax for the following and also explain the meaning of symbols used in it.
•Format statement:
NFORMAT(f1, f2, f3… fn)
Where N is the statement number, which will be of same value to that in READ and WRITE for corresponding format. And f1, f2, f3… fn are the format specifications which varies depending upon the number and types of variables in READ and WRITE statement.
•Formatted input and formatted output:
|
READ(h, n) list of variables
|
READ(*, 20) a, b
|
|
WRITE(h, n) list of variables
|
WRITE(*, 30) ‘a = ‘, a, ‘b = ‘, b
|
|
66
|
Here, h represents the hardware number. We place asterisk (*) in place of hardware number (h) to represent keyboard and screen as the default input and output devices. And n represents the line number where format statement is written. The format statement defines the formatting of inputs and outputs. In case formatting is not required then we can replace line number (n) with asterisk (*).
•Unformatted input and unformatted output
|
READ *, list of variables
|
|
READ *, a, b, c
|
|
PRINT *, list of variables/items
|
|
PRINT *, ‘a = ’, a
|
In READ, generally list of variables are present but in PRINT the list may contain variables as well as constants
•Unconditional GOTO GOTO n
Where n is any line number, if the line number is below the GOTO n statement then it skips the statements but if the line number is above the GOTO n then it creates a loop.
|
Skipping Statements
|
Creating a Loop
|
|
……
|
10 ……….
|
|
GOTO 10
|
……….
|
|
…..
|
……….
|
|
…..
|
GOTO 10
|
|
10 …..
|
……….
|
•Computed GOTO
GOTO(K1, K2, K3…Kn), Expression
If value of expression is 1, then the control goes to line number K1. Similarly, the value of expression 2, 3 … n will cause the control to go to line number K2, K3 … Kn respectively. If expression is less than one and greater than n then control goes to next line.
For example:
GOTO(10, 20, 30), I
If value of I is 1 then the control goes to line number 10. For I = 2, control will be at line 20 and for I = 3, control moves to 30. If value of I is other than 1, 2, or 3 then the control goes to next line.
67
•Arithmetic IF, Logical IF, IF THEN ELSE, ELSE IF THEN
•ARITHMETIC IF
IF(arithmetic Expression) K1, K2, K3
Here K1, K2, K3 represents line number. If the expression gives negative value then, the
statements at line number K1 will execute. Similarly, statements at line number K2 and K3
will execute for zero and positive value of expression respectively. Its example is shown as:
|
IF(a-b*c) 10, 20, 30
|
|
|
10
|
Statement for negative
|
|
GOTO 40
|
20statements for zero GOTO 40
30statements for positive
40STOP END
•LOGICAL IF
Unconditional GOTO comes in conjunction with ARITHMETIC IF, since part of the code must be skipped. When statement for negative expression is executed, then other statements for zero and positive cases must be skipped.
IF(Logical Expression) Statements for true
Statements for false
When logical expression is true, then its corresponding statement for true condition will
execute but skips the next statement which is for false condition. But if the condition is false
then the statements for false condition will be executed ignoring the statements for true
conditions.
For example:
IF(MOD(n,2).eq.0) WRITE(*,*) n, ‘ is even’
WRITE(*,*) n, ‘ is odd’
•IF THEN ELSEIF(condition)THEN
Statements_T ! These statements will execute if the condition is true
ELSE
Statements_F ! This section will execute if the condition is false
END IF
68
In IF THEN ELSE structure, IF is always followed by condition and THEN. END IF should not be missed from the structure. The ELSE part, however, can be omitted from the structure. The structure without ELSE would be like:
IF(condition)THEN
Statements ! These statements will execute if the condition is true
END IF
These forms are similar to that of simple if and if else in C programming language. The statements within the IF and ELSE block can contain IF THEN ELSE statement making a nested IF THEN ELSE.
•ELSE IF THEN
IF(condition 1) THEN
Statement 1
ELSE IF(condition 2) THEN
Statement 2
ELSE IF(condition n) THEN
Statement n
ELSE
Default Statement
END IF
This structure is similar to that of else if ladder in C programming. First, condition 1 is checked, if it is true then statement 1 is executed. If condition 1 is false then condition 2 is checked. Statement 2 will execute if condition 2 is true. But statement n will execute if condition n is true. If none of the conditions is true then Default statement will execute. The statements may contain IF ELSE THEN making the structure NESTED IF.
•DO LOOPSyntax:
DO N I = start, stop, update …….
…….
N CONTINUE
Here, N represents the valid statement number representing the last statement of the loop. Integer variable (I), as running variable, is used to control the loop. Start represents the starting
69
point of the loop, while stop indicates the terminating point of the loop. The update is a value which represents increment or decrement from start point to end point. If update is not given then its default value will be taken as one.
13.Write the difference between
•Formatted and unformatted input/output statements
|
SN
|
Formatted input/output statements
|
Unformatted input/output statements
|
|
1.
|
The width and precision can be defined
|
Here, width and precision cannot be
|
|
for inputs and outputs
|
defined for inputs and outputs
|
|
|
2.
|
Format statement is used in
|
Format statement is not required
|
|
conjunction
|
||
|
3.
|
Its general form is:
|
Its general form is:
|
|
Read(h, N) list of variables
|
Read *, list of variables
|
|
|
Write(h, N) list of variables
|
Print *, list of variables
|
|
|
Here, N represents the line number
|
Here, line number is not specified. So,
|
|
|
where format statement is written. The
|
formatting is not possible in this case.
|
|
|
format statement defines the
|
||
|
formatting of inputs and outputs.
|
||
|
The symbol asterisk(*) is used in place
|
||
|
of N if formatting is not required.
|
||
•Unconditional GOTO and Computed GOTO
|
SN
|
Unconditional GOTO
|
Computer GOTO
|
|
1.
|
Changes control of statement without
|
Changes control of statement based on
|
|
any condition
|
value of an integer expression
|
|
|
2.
|
It is used to skip certain portion of code
|
It is used to executed particular set of
|
|
and can be used to create a loop in
|
statements from list of instructions based
|
|
|
conjunction with IF statement.
|
on the result of an integer expression
|
|
|
3.
|
Its general form is:
|
Its general form is
|
|
GOTO n
|
GOTO(K1, K2, K3…Kn), Expression
|
|
|
Where n is any line number, if the line
|
If value of expression is 1, then the control
|
|
|
number is below the GOTO n
|
goes to line number K1. Similarly, the
|
|
|
statement then it skips the statements
|
value of expression 2,3 … n will cause the
|
|
|
70
|
but if the line number is above the GOTO n then it creates a loop.
control to go to line number K2, K3 … Kn respectively. If expression is less than one and greater than n then control goes to next line.
•Logical IF and Arithmetic IF
|
SN
|
Logical IF
|
Arithmetic IF
|
|
1.
|
It uses relation and logical expression
|
It uses arithmetic expression
|
|
2.
|
Expression generates true or false
|
Expression generates positive, negative or
|
|
based on operator and operands used
|
zero value based on operation
|
|
|
3.
|
Its general form is:
|
Its general form is:
|
|
IF(condition) True statement
|
IF(expression) K1, K2, K3
|
|
|
False statement
|
Here K1, K2, K3 represents line number. If
|
|
|
If condition is true the true statement
|
the expression gives negative value then,
|
|
|
will execute, otherwise the false
|
the statements at line number K1 will
|
|
|
statement will execute
|
execute. Similarly, statements at line
|
|
|
number K2 and K3 will execute for zero
|
||
|
and positive value of expression
|
||
|
respectively
|
||
•Do Loops and Implied do loops
|
SN
|
Do loops
|
Implied do loops
|
|
1.
|
It is used to repeat a block of code that
|
It is used to repeat only read and write
|
|
can be any statements
|
statement in arrays
|
|
|
2.
|
Verbs DO and CONTINUE are used
|
Verbs DO and CONTINUE are not used
|
|
3.
|
Using do loop to read and write
|
Using implied do loop to read and write
|
|
elements of arrays makes program
|
elements of arrays makes program
|
|
|
longer
|
comparatively shorter
|
|
•Stop and End Statement
|
SN
|
STOP Statement
|
END Statement
|
|
1.
|
Used to stop program execution
|
Indicates the end of the program
|
|
2.
|
Can be placed anywhere within the program
|
It is placed at the end of the program
|
|
71
|
|
3.
|
There can be more than one stop
|
There will always be one end
|
|
statements in a program
|
statement in the program
|
|
14. How iteration are done without using do loops. Explain briefly about CONTINUE statement.
Iterations can be done using LOGICAL IF and unconditional GOTO. Its general forms are:
|
10
|
IF(logical condition) GOTO 20
|
10
|
STATEMENTS
|
|
STATEMENTS
|
…..
|
||
|
….
|
OR
|
….
|
|
|
GOTO 10
|
IF(logical condition) GOTO 10
|
20
In first looping structure, if the logical condition is true, then the loop exits. But in second looping structure, true in logical condition will cause the loop to continue. This type of loop structure can be used to solve the problem which requires while loop in C programming. Let us take an example: to find the sum of all digits of a given number. Only loop structure is shown.
In C languageIn FORTRAN language
while(n!=0)
{
r = n%10; s = s + r; n = n/10;
}
|
10 IF(n.eq.0) GOTO 20
|
||
|
r = MOD(n,10)
|
10
|
r = MOD(n,10)
|
|
s = s + r
|
s = s + r
|
|
|
n = n/10
|
n = n/10
|
|
|
GOTO 10
|
IF(n.ne.0) GOTO 10
|
20
CONTINUE statement is used in conjunction with DO loops. It represents end of do loop structure.
15. What are arrays? How is it declared in FORTRAN?
Arrays are list of variables occupying consecutive memory location. It shares the same name among all the variables but the variables are differentiated with the help of index. In FORTRAN the indexing of array starts from 1. The declaration of array can be done as:
dimension array_name(size)
dimension array_name(row size, column size)
Use of dimension follows the implicit declaration of FORTRAN. Instead of dimension, other data type such as integer, real, character etc can also be used.
For example: dimension a(5) represents an array declaration of real type. It can hold maximum of five values as its declaration creates five variables a(1), a(2), a(3), a(4), and a(5).
72
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
