Question: t this is the example 4 from lecture 12 this is the code from lecture 14 part(b) of question continues this is the sort.f08 file

t
this is the example 4 from lecture 12


this is the code from lecture 14
part(b) of question continues
this is the sort.f08 file
1. Given a list of positive real values, X1, X2, ..., Xn, the central tendency of the list can be measured by the arithmetic mean, the geometric mean, or the harmonic mean. These three means are defined as follows: 1 arithmetic mean Xi = F(x1 + x2 + + Xn) n n n i=1 geometric mean X 1 X2...Xn n (ie. the nth root). harmonic mean 1 21 + 1 22 + + 1 en The median is the value in the list that has half of the numbers larger than it, and half of the numbers smaller than it. Two additional statistical measures of data are the variance and the standard deviation. The variance is the average of the squares of the deviations of the numbers from the mean, n variance (xi 7) n i=1 where is the arithmetic mean. The standard deviation is the square root of the variance. The variance and standard deviation provide a description of the spread of the data. (a) Write a Fortran program to create a data file of random values between 1 and 100. The number of values contained in this file should also be random, between 10 and 100. You can use the built-in random number generation subroutines to generate the values required (see Example 4 in Lecture 12). (b) Using the data file created above as input, write a Fortran program to com- pute and display the arithmetic mean, geometric mean, harmonic mean, median, variance, and standard deviation of the values. Ask the user for the name of the data file, and have your program read to the end of the file. Your program should include a separate external function for each of the 6 measures calculated. Be sure to include an interface within your program. You can use the code from Lecture 14 as a starting point for your solution. Example 4: Subroutine PROGRAM Dice_roll IMPLICIT NONE INTEGER :: spots, count, numrolls, diei, die2, pair, roll REAL :: ri, r2 CHARACTER :: response WRITE(*, *) 'Enter number of times to roll' READ(*, *) numrolls ! Seed the random number generator CALL RANDOM_SEED DO WRITE(*,*) 'Enter the number of spots to count' READ(*, *) spots count = 0 DO roll = 1, numrolls ! Get random numbers between 0 and 1 CALL RANDOM_NUMBER (r1) CALL RANDOM_NUMBER (r2) ! Compute values for each die die1 = 1 + INT(6*r1) TIT( dia - 1 DO WRITE(*,*) 'Enter the number of spots to count' READ(*,*) spots count = 0 DO roll = 1, numrolls ! Get random numbers between 0 and 1 CALL RANDOM_NUMBER (r1) CALL RANDOM_NUMBER(r2) ! Compute values for each die die1 = 1 + INT(6*r1) die2 = 1 + INT (6*r2) pair = diel + die2 IF (pair == spots) count = count + 1 END DO was ', & WRITE(*,*) 'The relative frequency of ', spots,' REAL (count) / REAL (numrolls) WRITE(*,*) 'Roll again? (Y/N)' READ(*, *) response IF (response /= 'y') EXIT END DO ND PROGRAM Dice_roll PROGRAM standev INTEGER :: n=50 REAL, DIMENSION (n) :: values REAL :: variance WRITE(*, *) "Enter",n, "values" READ(*, *) value WRITE(*, *) "The mean value is" calc_mean(values) calc_sd (values, stdv, variance) WRITE(*,*) "The standard deviation is",stdv WRITE(*,*) "The variance is", var CONTAIN FUNCTION calc_mean (list) ! Compute and return the mean of the values in list REAL ,DIMENSION, INTENT(IN) :: values REAL :: mean mean = 0.0 DO i=1,SIZE(list) mean = mean + list(i) END DO mean mean/SIZE(list) END FUNCTION calc_mean SUBROUTINE calc_sd(sd, var) ! Compute the standard deviation and the variance INTEGER , DIMENSION(:),INTENT(IN) :: list REAL, INTENT(IN) :: sd, var REAL :: mean INTEGER :: i calc_mean(list) DO i=1, SIZE(list) var = (list(i)-mean) *(list[i]-mean) END DO var = var/SIZE(list) /) sd = SQRT (var); END SUBROUTINE calc_sd END PROGRAM standev (b) Using the data file created above as input, write a Fortran program to com- pute and display the arithmetic mean, geometric mean, harmonic mean, median, variance, and standard deviation of the values. Ask the user for the name of the data file, and have your program read to the end of the file. Your program should include a separate external function for each of the 6 measures calculated. Be sure to include an interface within your program. You can use the code from Lecture 14 as a starting point for your solution. To calculate the product for the geometric mean you may need to store a value that is larger than can be stored in a 32-bit floating point variable. To use double precision in Fortran, replace REAL in your declaration with REAL (KIND=8). To calculate the median value you will need to sort the data, for which you can make use of the subroutine contained in the sort.f08 file available with this Problem Set. This subroutine can be included in your source code file as an external subroutine. SUBROUTINE Sort(x,n) REAL, INTENT(INOUT) :: x(n) INTEGER, INTENT(IN) ::n INTEGER :: i, j DO i=1, n-1 smallest = x(i) location = i DO j=i+1, n IF (x(j)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
