Professional Documents
Culture Documents
a. Save your programs in your assignment folder (in your Z Drive). The folder should be
named in the following manner.
Your Full Name_Your MS-ID_CF1_Lab6
e.g. Raza Ali_513912_CF1_Lab6
b. Also prepare a document in the word format. Write all the program questions and source
code you have written and also paste the screen shots of their outputs, in the document.
c. Write your full name and MS-ID in the footer of the document.
d. The document title should be “CF Lab Assignment 6”.
e. Place the completed document (with all the program codes, and their output) in
your folder.
f. Paste your folders in the path given as follows.
\\172.30.10.2\Assignments\Engineering Dept\ DNE\Awais Zahur\MS Zero\Lab 6
g. Before leaving the lab, make a backup of your codes and assignment.
h. Access this document at:
\\172.30.10.24\FacultyShare\Awais Zahur\Public\CF 2019 Zero
Important Tip:
Use as many comments in your codes as required in
order to make your programs more comprehensible.
INTEGER AGE(100),NUM(25),DEG
This reserves 100 words of storage for array AGE, 25 for array NUM, and one word for the
variable DEG. All three items are of type INTEGER.
Space can also be reserved for arrays by the DIMENSION statement, which reserves storage using
a similar syntax, but includes no information about type. Thus, if this method is used, the type is
either determined by the initial letter of the array or assigned by a separate type specification.
Therefore, the equivalent to the above using a DIMENSION statement is:
INTEGER AGE,DEG
DIMENSION AGE(100),NUM(25)
DIMENSION statements, like type specifications, are non-executable and must be placed before
the first executable statement.
When this form of declaration is used in a type or DIMENSION statement the upper and lower
bounds for the subscript are 1 and the dimension respectively. Thus, AGE in the above example
may have any subscript from 1 to 100. Arrays can also be declared to have subscripts with a lower
bound other than 1 by using a second form of declaration in which the lower and upper bounds are
given, separated by a colon. For example:
REAL C(0:20)
INTEGER ERROR(-10:10)
Although the declaration stipulates bounds for the subscript, not all compilers check that a
subscript actually lies within the bounds. For example, if NUM is declared as above to have a
subscript from 1 to 25, a reference to NUM(30) may not cause an error. The compiler may simply
use the 30th word of storage starting from the address of NUM(1) even though this is outside the
bounds of the array. This can cause unpredictable results. Care should therefore be taken to make
sure that your subscripts are within their bounds.
Array elements can be used in the same way as variables, their advantage being that different
elements of an array can be referenced by using a variable as a subscript and altering its value, for
example by making it the control variable of a DO loop. This is illustrated in the following sections.
The array name without a subscript refers to the entire array and can be used only in a number of
specific ways.
NUM(1) = 0
NUM(2) = 5
If all the elements are to have equal values, or if their values form a regular sequence, a DO loop
can be used. Thus, if NUM and DIST are arrays of dimension 5:
DO 10, I = 1,5
NUM(I) = 0
10 CONTINUE
initialises all the elements of NUM to 0, while:
DO 10, I = 1,5
DIST(I) = 1.5*I
10 CONTINUE
A common bug in Fortran is that the program tries to access array elements that are out of bounds
or undefined. This is the responsibility of the programmer, and the Fortran compiler will not detect
any such bugs!
size = (hi_index1-low_index1+1)*(hi_index2-low_index2+1)
It is quite common in Fortran to declare arrays that are larger than the matrix we want to store.
(This is because Fortran does not have dynamic storage allocation.) This is perfectly legal.
Example:
The elements in the submatrix A(1:3,4:5) are undefined. Do not assume these elements are
initialized to zero by the compiler (some compilers will do this, but not all).
This program declares two arrays and prints them using DO loop. As the arrays are not defined so
garbage value will be printed. Now change the program and save some numbers in arrays and then
print them.
This program calculates the average of numbers and print its result.
Program – 03: Type the following program and understand its working.
This program finds the maximum value from an array and print it.
On the other hand, when writing data to a new file we want to be sure the file doesn’t already exist,
because if it does, we will overwrite the data already there. So when creating a new file with
STATUS=‘NEW’, we watch for error codes.
ENDFILE Statement
ENDFILE unit-number
Writes an end-of-file (EOF) record (one line of input/output is one record) at the current position
in a sequential file.
After an EOF record is written to the file, only the data records before EOF can be read, whether
access is sequential or direct.
Program – 02: Modify Program – 01 to copy the content of input vector into another vector in
reverse order .i.e. 1st element of vector A shall be stored in last location of vector B. And then
display the 2nd vector.
Program – 03: Write a FORTRAN program to calculate sum of all the numbers stored in a 1-
dimensional array.
Program – 04: Write a FORTRAN program that asks user to enter 10 integers in an array and
then as output gives the largest element in the array and the index at which that element was found.
Program – 05: Write a FORTRAN program that asks the user to enter 15 integers in an array
and computes how many integers are greater than 10.
n n n
xi ( xi x )2 ( x x )( y y )
i i
x i 1
; var( X ) i 1
; cov( X , Y ) i 1
n n n
cov( X , Y )
( X ,Y )
var( X ) var(Y )
n xi2 ( xi ) 2
i 1 i 1
a0 y a1 x
Program – 08: Implement the following procedure to generate prime numbers from 1 to 100
into a program. This procedure is called “Sieve of Eratosthenes”.
Step 1: Fill an array prime[100] with numbers from 1 to 100
Step 2: Starting with the second entry in the array, set all its multiples to zero.
Step 3: Proceed to the next non-zero element and set all its multiples to zero.
Step 4: Repeat step 3 till you have set up the multiples of all the non-zero elements to zero.
Step 5: At the conclusion of step 4, all the non-zero entries left in the array would be prime
numbers, so print out these numbers.
Program – 09: Sort an array of 25 elements in ascending order using Selection Sort Algorithm.
The logic of the algorithm is given in the figure below:
i.e at the end of iteration 1, the smallest value will be at first location and at the end of 2nd
iteration the second smallest value will be at second location and so on.
i.e at the end of iteration 1, the largest value will be at last location and at the end of 2nd iteration
the second largest value will be at second location and so on.
Program – 11: Write a program that reads the file ‘In.txt’ (file provided) which contains the
columns NAME, MID Marks and Final Marks. Then sort (ascending) the names and store the
result in the file ‘sorted.txt’ (as shown in figure below).
Program – 12: Read the file ‘sorted.txt’ and assign the roll numbers (serial number) to the
students and write the output as a sequential file ‘roll.txt’ with columns of Roll No., Name, Mid
and Final Marks (as shown in figure below).
Create a direct access formatted output file ‘Final.txt’ having the data of Roll No., Name, Mid,
Final, Total Marks and GPA (as shown in figure below).