Capstone: stats tool

Contents
The spec
Read the data file
Mean and stddev
Assert the math

The spec

The tool reads a text file with one real number per line, then reports count and mean and standard deviation.

Success means clean compile with warnings on, correct results on a known sample, and no crashes on an empty trailing line.

cat data.txt

1.0 2.0 3.0 4.0 in separate lines

Read the data file

Open with newunit, read in a loop with iostat, and close promptly. Count only successful reads.

The buffer stays fixed size and n records how many slots hold valid data.

subroutine read_data(fname, vals, n) implicit none character(len=*), intent(in) :: fname real, intent(out) :: vals(:) integer, intent(out) :: n integer :: u integer :: ios n = 0 open(newunit=u, file=fname, status="old", action="read") do if (n .ge. size(vals)) exit read(u, *, iostat=ios) vals(n + 1) if (ios .ne. 0) exit n = n + 1 end do close(u) end subroutine read_data

Mean and stddev

Mean is the sum divided by count. Deviation measures spread around that mean.

Use whole-array sum for clarity and guard against zero count before dividing.

function mean_fn(a, n) result(m) implicit none real, intent(in) :: a(:) integer, intent(in) :: n real :: m if (n .le. 0) then m = 0.0 return end if m = sum(a(1:n)) / real(n) end function mean_fn

Assert the math

Test on values with a known answer. Compare with a small tolerance based on epsilon, not exact equality.

Stop with an error message when the check fails so scripts detect the failure.

program test_stats implicit none real :: vals(4) real :: m vals = [1.0, 2.0, 3.0, 4.0] m = sum(vals) / real(size(vals)) if (abs(m - 2.5) .gt. 0.0001) then print *, "mean test failed" error stop 1 end if print *, "mean test passed" end program test_stats

Next: Capstone: 1D heat equation

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: