
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

One-Dimensional Arrays (Vectors)
Consider the vectors ~a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] and~b = [ 1 , − 1 , 2 , − 2 , 3 , − 3 , 4 , − 4 ].
C Write a Fortran 90 program VECTOR1 that declares ~a as a 1-D REAL array of length 8. Initialize the array using array constants. Print out the components of the array in the normal order.
C Write a Fortran 90 program VECTOR2 that declares~b as a 1-D REAL array of length 8. Initialize the array using a READ loop, reading in the normal order. Print out the components of the array in reverse order.
B Write a Fortran 90 MODULE named VECTOR LIBRARY that contains a single FUNCTION sub- program SUMMATION, which returns the sum of the elements of a 1-D array given the array and its length n. Compile the MODULE.
B Write a Fortran 90 program that calls FUNCTION SUMMATION to sum the elements of vector ~a above. Initialize ~a using an implied DO loop in the main program.
B Add another FUNCTION subprogram AVERAGE to your MODULE that computes the average of n real values. Use it to compute the average value of the vector ~b. Hint: copy, paste, and modify your previous subprogram.
A The dot product or inner product of two n vectors ~a and~b is defined as
~a ·~b =
n
i= 1
aibi
Write a FUNCTION subprogram named INNER PRODUCT that returns the dot product of two n- vectors, given the vectors and their length n. Encapsulate INNER PRODUCT in the same MODULE you wrote above. (Don’t forget to recompile.) Write a simple program to test your inner product sub- program on the vectors ~a and ~b above. Use any method you want to initialize the vectors. (NOTE: Fortran 90 provides an intrinsic function DOT PRODUCT, which accomplishes the same task as IN- NER PRODUCT. You may want to compare results if you have time.)
A+ The length or norm of a vector, denoted |~a|, is defined
|~a| =
~a ·~a
Add one more subprogram VECTOR NORM to your MODULE to compute the norm of a vector. Test it using vector ~b. HINT: Save yourself effort by having VECTOR NORM make use of IN- NER PRODUCT.