13.9: Exercises
- Page ID
- 54352
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
Below are some quiz questions and project suggestions based on this chapter.
Quiz Questions
Below are some quiz questions.
- Explain why an array is considered a direct access structure.
- Can arrays hold integer values (yes/no)?
- Can arrays hold real values (yes/no)?
- Write the declarations for the following:
- An integer constant,
SIZE1
, set to 100. - An array,
rvalues
, with 10 real elements. - An array,
inums
, withSIZE1
integer arguments. - An array,
counts
, with 10 real elements whose subscript values range from 0 to 9.
- An integer constant,
- Given the declarations and the following code:
integer, dimension(4) :: arr integer :: k arr(1) = 10 arr(2) = 15 arr(3) = 20 arr(4) = 25 k=3
- What does
arr(1)
equal? - What does
arr(1) + arr(2)
equal? - What does
arr(1+2)
equal? - What does
arr(k)
equal?
- What does
- When can an array be allocated (two options)?
- Given the following statements:
integer, dimension(5) :: nums integer :: i=1 nums = 0 do if ( i == 5 ) exit if ( mod(i,2) == 0) then nums(i) = 99 else nums(i) = i end if i = i + 1 end do
- What values are in the array after execution?
- What is the
(nums(i), i=1,5)
referred to as? - What does
write (*,'(1x,i2)') (nums(i), i=1,5)
display? Note: use an underscore (“_”) to show the spaces.
Suggested Projects
Below are some suggested projects.
- Type in the standard deviation program, compile, and execute the program. Test the program on a series of different input values.
- Write a Fortran program to cube a series of integer numbers from a file and cube each number. The program should also calculate the real average of the original numbers and the real average of the cubed numbers. Test the program on a series of different input values.
- Write a Fortran program to generate a series of real values between 1.0 and 100.0 and store the numbers in an array. Then, the program should compute the norm of a vector (single dimension array). The formula for norm is as follows:\[ \left| \textit{norm} \right| = \sqrt{a_1^2 + a_2^2 + a_3^2 + \ldots + a_n^2} \nonumber \]Refer to Appendix C for more information regarding generating random numbers. Test the program on a series of different input values.
- Write a Fortran program to read a series of numbers from a file, store the numbers in an array, and then sort the numbers using the following selection sort algorithm:
for i = len downto 1 big = arr(1) index = 1 for j = 1 to i if arr(j) > big big = arr(j) index = j end_if end_for arr(index) = arr(i) arr(i) = big end_for
You will need to convert the above pseudo-code algorithm into Fortran code. Test the program on a series of different input values.