Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 ONE-DIMENSIONAL ARRAYS

Similar presentations


Presentation on theme: "CHAPTER 8 ONE-DIMENSIONAL ARRAYS"— Presentation transcript:

1 CHAPTER 8 ONE-DIMENSIONAL ARRAYS

2 Mean Time to Failure One important statistic used in measuring the reliability of a component in a circuit is the mean time to failure, which can be used to predict the circuit’s life time. This is especially important in situation in which repair is difficult or even impossible, such as a computer circuit in a space satellite. Suppose that NASA (National Aeronautics and Space Administration) has awarded an engineering laboratory a contract to evaluate the reliability of particular component for a future space probe to Mars.

3 Mean Time to Failure As part of this evaluation, an engineer at this laboratory has test of these circuits and recorded the time at which each failed; now she would like a program to process this data and determine the mean time to failure.

4 Space Probe

5 8.1 Example: Processing a List of Failure Times
Consider again the mean-time-to-failure problem from Section 4.4 (p. 215). Recall that in this problem a number of components in a circuit were tested and the time which each component failed was recorded. As a measure of reliability of a component, the mean of this failure times was calculated. This computation required processing the list of failure times only once.

6 Processing a List of Failure Times
Suppose now we wish to analyze these failure times using a program to 1. Find the mean time to failure 2. Print a list of failure times greater than the mean 3. Sort the failure times so that they are in ascending order Clearly, this will require processing the list several times.

7 Solution 1: Use One variable for Each Failure Time

8 Solution 2: Reread the Data

9 Reread the Data INTEGER:: Openstatus, Inputstatus, &
Number_of_Times, I REAL :: FailureTime, Sum, Mean_Time_to_Failure OPEN (UNIT = 10, FILE = “FAILTIME.DAT”, & STATUS = “OLD”, IOSTAT = OpenStatus) IF (OpenStatus > 0) STOP “*** Cannot open file ***”

10 Reread the Data Number_of_Times = 0 DO
READ (10, *, IOSTAT = InputStatus) FailureTime IF (InputStatus > 0) STOP “*** Input error***” IF (InputStatus < 0) EXIT ! End of file Number_of_Times = Number_of_Times + 1 END DO

11 Reread the Data REWIND (UNIT = 10) Sum = 0.0 DO I =1, Number_of_Times
READ (10, *) FailureTime Sum = Sum + FailureTime END DO Mean_Time_to_Failure = & Sum / REAL (Number_of_Times)

12 Reread the Data REWIND (UNIT = 10) DO I =1, Number_of_Times
READ (10, *) FailureTime IF (FailureTime > Mean_Time_to_Failure) & PRINT *, FailureTime END DO

13 Arrays (陣列) .

14 Arrays IMPLICIT NONE INTEGER, Parameter :: NumTimes = 50
REAL, DIMENSION(NumTimes) :: FailureTime INTEGER :: I REAL :: Sum, Mean_Time_to_Failure

15 Arrays Sum = 0.0 DO I = 1, NumTimes Sum = Sum + FailureTime(I) END DO
Mean_Time_to_Failure = Sum / REAL(NumTimes)

16 Arrays DO I = 1, NumTimes IF (FailureTime(I) > Mean_Time_to_Failure) & PRINT '(1X, F9.1)', FailureTime(I) END DO See Figure 8.1 (p. 522) computer program

17 Compile-Time Arrays & Run-Time Arrays
Compile-Time Arrays: The size is fixed before execution begins. Run-Time (or Allocatable 可分配的) Arrays: The memory is allocated (分配) during execution, making it possible to allocate an array of appropriate size.

18 Compile-Time Arrays REAL, DIMENSION(50):: FailureTime
REAL:: FailureTime (50) REAL:: FailureTime (1:50) REAL, DIMENSION(-1:3)::Gamma Gamma(-1), Gamma(0), Gamma(1), Gamma(2), Gamma(3),

19 Allocatable (可分配的) / Run-Time Arrays
IMPLICIT NONE REAL, DIMENSION(:), ALLOCATABLE :: FailureTime INTEGER :: NumTimes, I REAL :: Sum, Mean_Time_to_Failure ‧ ‧ ‧ ‧ ‧ READ *, NumTimes ALLOCATE(FailureTime(NumTimes)) DEALLOCATE(FailureTime)

20 Allocatable Arrays (可分配的)
REAL, DIMENSION(:), ALLOCATABLE :: A, B WRITE (*, ‘(1x, A)’, ADVANCE = “NO”) & “Enter size of arrays A and B:” READ *, N ALLOCATE(A(N), B(0 : N+1), STAT = AllocateStatus IF (AllocateStatus /=0) STOP “*not enough memory*” See Figure 8.2 (p. 531) computer program

21 8.6 Example: Vector Processing (p. 567)

22 Vector Processing n-dimension Vectors:
a = (a1, a2,…, an); b = (b1, b2,…, bn) The norm (秩) of a vector a is The sum and difference of two vectors a + b = (a1+ b1, a2+ b2, …, an+ bn) a - b = (a1 - b1, a2 - b2, …, an - bn)

23 Vector Processing c a = (c a1, c a2,…, c an)
Multiplication of a vector by a scalar c a = (c a1, c a2,…, c an) The dot (or scalar) product a‧ b of two vectors is

24 8.4 Array Processing (p.542) Array Constants
(/ value1, value2, value3, . . ., valuek, /) (/ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 /) (/ (2*I, I = 1, 10) /) (/ 2, 4, (I, I = 6, 18, 2), 20 /)

25 Array Constants INTEGER, DIMENSION (10) :: A
A = (/ (2*I, I = 1, 10) /) A = (/ 2, 4, (I, I = 6, 18, 2), 20 /)

26 Array Expressions (p.544) INTEGER, DIMENSION (4) :: A, B
INTEGER, DIMENSION (0:3) :: C INTEGER, DIMENSION (6:9) :: D LOGICAL, DIMENSION (4) :: P A = (/ 1, 2, 3, 4 /) B = (/ 5, 6, 7, 8 /) A = A + B

27 Array Expressions (p.544) C = (/ -1, 3, -5, 7 /) D = 2 * ABS (C) + 1
P = (C > 0) .AND. (MOD(B,3) == 0) Answer: .FALSE., .TRUE., .FALSE., .FALSE.

28 Array Sections and Subarrays (p.545)
Array Assignment A = 0 INTEGER, DIMENSION (10) :: A A = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /) then A(2:10:2) A = (/ 22, 44, 66, 88, 110 /)

29 Array Sections and Subarrays
INTEGER, DIMENSION (10) :: A A = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /) I = (/6, 5, 3, 9, 1/) B = A (I) or B = A ((/6, 5, 3, 9, 1/)) then B = (/ 66, 55, 33, 99, 11 /)

30 Array Sections and Subarrays
INTEGER, DIMENSION (10) :: A A = (/ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 /) The statement A(1:10:2) = (/ (I**2, I = 1,5) /) changes the elements in the odd positions of A to 1, 4, 9, 16, 25.

31 Array Sections and Subarrays
READ *, FailureTime (1: NumTimes) READ *, (FailureTime (I), I= 1, NumTimes) PRINT *, FailureTime (1: NumTimes) PRINT *, (FailureTime (I), I= 1, NumTimes)

32 The WHERE Construct INTEGER, DIMENSION (5) :: A = (/ 0, 2, 5, 0, 10 /)
REAL, DIMENSION (5) :: B WHERE (A > 0) B = 1.0 / REAL (A) ELSEWHERE B = -1.0 END WHERE

33 Arrays as Arguments (p. 548)
Intrinsic Array-Processing subprograms ALLOCATED (A) DOT_PRODUCT (A, B) MAXVAL(A) MAXLOC (A) MINVAL(A) MINLOC (A) PRODUCT (A) SIZE (A) SUM (A)

34 Examples: Calculating the Mean of a List
Figure 8.4 (p. 549) Figure 8.5 (p. 550) Figure 8.6 (p. 552) Figure 8.7 (p. 554): Reversing a List

35 8.5 Application: Quality Control
A quality control engineer (品質管制工程師) monitors a machine by recording the number of defective parts (有缺點的零件) the machine produce each hour. This information is to be summarized in a frequency distribution that shows the number of one-hour periods in which there were no defective parts, one defective part, two defective parts, …, five or more defective parts. See Figure 8.8 (p. 561) computer program

36 Bar Graph or Histogram Frequency Distributions (出現率分布)


Download ppt "CHAPTER 8 ONE-DIMENSIONAL ARRAYS"

Similar presentations


Ads by Google