• No results found

Initializing Arrays

In document Fortran 95 (Page 110-115)

An array can be initialized when it is declared. Each element in the array can be initialized to a single value or each element to a different value. The following declaration,

real, dimension(10) :: numbers = 1.0 will initialize each of the 10 elements in the array numbers to 1.0.

To initialize each element to a different value, each value needs to be specified.

Chapter 13 ◄ Single Dimension Arrays For example, the following declaration,

real, dimension(5) :: numbers = (/ 1.0, 2.0, 3.0, 4.0 5.0 /) will initialize each of the 5 elements in the array numbers to 1.0, 2.0, 3.0, 4.0, and 5.0 respectively.

The implied do-loop may also be used. For example, in the following declaration, integer, dimension(5) :: numbers = (/ (i, i=1,5) /) will initialize each of the 5 elements in the array numbers to 1, 2, 3, 4, and 5 respectively.

13.5 Example

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 deviation8. The standard deviation is calculated as follows:

standard deviation =

i =1n (average− list (i))2 n

As such, the average must be calculated before the standard deviation.

13.5.1 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 in steps, the first of which is computing the inner loop. The numbers should be displayed, ten per line, followed by the results.

13.5.2 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 can not 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)

Chapter 13 ► Single Dimension Arrays

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

Next, another loop will be used to find the minimum, maximum, and sum of the numbers. To find the 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

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

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.

Chapter 13 ◄ Single Dimension Arrays

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 a program comments.

13.5.3 Develop the Program

Based on the algorithm, the below program could be developed.

program standardDeviation

! declare variables implicit none

integer :: i, ncount=0, errs=0, opstat, rdstat real :: num, min, max, sum, stdsum

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."

Chapter 13 ► Single Dimension Arrays

Chapter 13 ◄ Single Dimension Arrays

! 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) )

! 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 is not required, but helps to make the program more easily readable.

13.5.4 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.

In document Fortran 95 (Page 110-115)