13.7: Example
- Page ID
- 54350
\( \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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)In this example we will write a Fortran program to read a series of numbers from a file and compute some statistical information including minimum, maximum, sum, average, and standard deviation1.
The standard deviation is calculated as follows:
\[ \textit{standard deviation} = \sqrt{\frac{\sum_{i = 1}^{n}\left(\textit{average} - \textit{list}(i) \right)^2}{n - 1}} \nonumber \]
As such, the average must be calculated before the standard deviation.
Understand the Problem
The program will display an initial header and get the file name. Specifically, we will need to prompt for the file name, read the file name, and verify that the file is available by attempting to open the file. Then, the program will read the numbers from the file and store them in an array. For this problem, there will be no more than 5000 numbers in the file. After the numbers are in the array, the minimum, maximum, and sum will be found. Next, the average can be computed. Finally, the standard deviation can be calculated (using the provided formula). The numbers should be displayed, ten per line, followed by the results.
Create the Algorithm
After the header is displayed, the program should prompt for the file name, read the file name, and verify that the file is available by attempting to open the file. If the file cannot be opened, the program will display an error message and re-prompt. If the user does not enter correct information after three tries, the program should terminate with an appropriate error message. That is, three errors are acceptable, but if a fourth error is made, the program will terminate.
! declare variables ! integer -> i, ncount, errs, opstat, rdstat ! real -> min, max, sum, stdsum ! real -> array for ! character -> filename(20) ! display initial header ! loop ! prompt for file name ! read file name ! attempt to open file ! if file open successful, exit loop ! display error message ! count error ! if >3 errors, terminate program ! end loop
Then, the program will loop to read the numbers from the file and store them in an array. The program will check for any read errors (status variable > 0) and for the end of file (status variable < 0).
If a valid number is read, it will be counted and placed in the array.
! loop ! read from the file ! if error on read, terminate program ! if end of file, exit loop ! increment number counter ! place number in array ! end loop
Next, another loop will be used to find the minimum, maximum, and sum of the numbers. To find the minimum and maximum values, we will assume that the first element in the array is the minimum and maximum. Then, the program will check each number in the array. If the number from the array is less than the current minimum value, the current minimum value will be updated to the new value. Same for the maximum, if the number from the array is more than the current maximum value, the current maximum value will be updated to the new value.
! initialize min, max, and sum ! loop ! check for new min ! check for new max ! update sum ! end loop
Once the sum is available, the average can be computed. Finally, a loop will be used to calculate the summation for the standard deviation.
! calculate average ! initialize stdsum ! loop ! calculate average – array item ! update stdsum ! end loop
Once the summation is completed, the standard deviation can be computed and the final results displayed. As per the example specifications, the numbers should be displayed 10 per line.
One way to handle this is to display numbers on the same line (with the advance=“no”
clause) and every 10th line display a new line.
! calculate standard deviation ! loop to display numbers, 10 per line ! display results ! end program
For convenience, the steps are written as program comments.
Implement the Program
Based on the algorithm, the program below could be created.
program standardDeviation ! declare variables implicit none integer :: i, ncount=0, errs=0, opstat, rdstat real :: num, min, max, sum, average, stdsum, std real, dimension(5000) :: numbers character(20) :: filename ! display initial header write (*,*) "Standard Deviation Program Example." ! loop do ! prompt for file name write (*,'(a)', advance="no") "Enter File Name:" ! read file name read (*,*) filename ! attempt to open file open(42, file=filename, status="old", & action="read", position="rewind", & iostat=opstat ) ! if file open successful, exit loop if (opstat==0) exit ! display error message write (*,'(a)') "Error, can not open file." write (*,'(a)') "Please re-enter." ! count error errs = errs + 1 ! if >3 errors, terminate program if (errs > 3) then write (*,'(a)') "Sorry you are having problems." write (*,'(a)') "Program terminated." stop end if ! end loop end do ! loop do ! read file read (42, *, iostat=rdstat) num ! if error on read, terminate program if (rdstat>0) stop "Error on read." ! if end of file, exit loop if (rdstat<0) exit ! increment number counter ncount = ncount + 1 ! place number in array numbers(ncount) = num ! end loop end do ! initialize min, max, and sum min = numbers(1) max = numbers(1) sum = 0.0 ! loop do i = 1, ncount ! check for new min and new max if (numbers(i) < min) min = numbers(i) if (numbers(i) > max) max = numbers(i) ! update sum sum = sum + numbers(i) ! end loop end do ! calculate average average = sum / real(ncount) ! initialize stdsum stdsum = 0.0 ! loop do i = 1, ncount ! calculate (average – array item)^2 and update sum stdsum = stdsum + (average - numbers(i))**2 ! end loop end do ! calculate standard deviation std = sqrt ( stdsum / (real(ncount)-1) ) ! display results write (*,'(a)') "-------------------------------" write (*,'(a)') "Results:" do i = 1, ncount write (*,'(f8.2,2x)', advance="no") numbers(i) if (mod(i,10)==0) write (*,*) end do write (*,'(a, f8.2)') "Minimum = ", min write (*,'(a, f8.2)') "Maximum = ", max write (*,'(a, f8.2)') "Sum = ", sum write (*,'(a, f8.2)') "Average = ", average write (*,'(a, f8.2)') "Standard Deviation = ", std end program standardDeviation
The spacing and indentation are not required, but help to make the program more readable.
Test/Debug the Program
For this problem, the testing would involve executing the program using a file with a set of numbers where the correct results are either known ahead of time or can be calculated by hand in order to verify that the results are accurate.
- For more information, refer to: http://en.Wikipedia.org/wiki/Standard_deviation