Presentation is loading. Please wait.

Presentation is loading. Please wait.

13 April, 2000 CS1001 Lecture 17 One-Dimensional Arrays Using 1-D arrays in Project.

Similar presentations


Presentation on theme: "13 April, 2000 CS1001 Lecture 17 One-Dimensional Arrays Using 1-D arrays in Project."— Presentation transcript:

1 13 April, 2000 CS1001 Lecture 17 One-Dimensional Arrays Using 1-D arrays in Project

2 13 April, 2000 Introduction Predefined Fortran data types are simple types -- each variable is a single item Variable Memory names Cels Fahr Degree : MMM DD : 100.00 212.00 45.5 : 30 45 :

3 13 April, 2000 Dealing with lots of data of the same type Declare lots of variables: Cels01, Cels02,… Cels90 Fahr01, Fahr02,… Fahr90 etc. Variable Memory names Cels01 Cels02 Cels03 : Cels90 Fahr01 : 100.00 112.00 145.50 : 130.00 212.00 : Use one-dimensional arrays: Cels(90) Fahr(90) etc. Variable Memory names Cels(1) Cels(2) Cels(3) : Cels(90) Fahr(1) : 100.00 112.00 145.50 : 130.00 212.00 :

4 13 April, 2000 One-Dimensional Array Declare an array variable Direct access data structure Data stored or retrieved by specifying the location in the structure Access each element of the array by means of the subscripted array variable using a subscript or index Variable Memory names Cels(1) : Fahr(1) : 100.00 112.00 145.50 : 130.00 212.00 : Index 1 2 3 : 90 1 :

5 13 April, 2000 Array Declaration To declare an array of 90 real values: REAL, DIMENSION(90) :: Cels or, REAL, DIMENSION(1:90) :: Cels or, REAL :: Cels (90) If you need to start at 0, you must use (0:u) Special case for CHARACTER: –CHARACTER(20) :: String ! String is 20 characters long –then String(n:m) isolates the nth to mth characters in the string –e.g., String = “abcdefghijklmnopqrst”, then String(5:5)= “e”, String (3:6) = “cdef”

6 13 April, 2000 Temperature Conversion PROGRAM TEMP_Conv REAL, DIMENSION(90) :: Cels,Fahr : DO Index = 1, 90 Fahr(Index) = CTOF(Cels(Index)) PRINT *, Range, Fahr END DO : INCLUDES REAL FUNCTION CTOF (Ctemp) :

7 13 April, 2000 Input Using DO Loop INTEGER :: NumVel, Loop INTEGER, PARAMETER :: Limit = 90 REAL, DIMENSION(Limit) :: Cels, Fahr PRINT *, “Enter number of Celsius Temperature up to 50” READ *, NumVel DO Loop = 1, NumVel PRINT *, “Enter Celsius ”, Loop READ *, Cels (Loop) END DO etc.

8 13 April, 2000 Output Using DO Loop ! Continues from previous code PRINT *, “Celsius” DO Loop = 1, NumVel PRINT *, Cels(Loop) END DO ! etc.

9 13 April, 2000 Input/Output Tricks READ *, Cels is the same as READ *, Cels(1),Cels(2),Cels(3),Cels(4),... PRINT *, Cels is the same as PRINT*, Cels(1),Cels(2),Cels(3),Cels(4),...

10 13 April, 2000 Implied DO Loop for I/O (i/o_list, control_var = init_value, limit, step) –i/o_list is a subscripted variable –control_var, init_value, limit, and step (if used) are the same as for a DO loop –must be in parentheses READ *, (Cels(I), I = 1, NumVel) PRINT *, (Cels(I), I = 1, NumVel)

11 13 April, 2000 Assigning Values to Arrays Can be initialized using assignment statements: –REAL, DIMENSION(1:10) :: Alpha –Alpha(1) = 0.0 –Alpha(2) = 0.0! etc. –or Alpha = (/ 0.0, 0.0,... 0.0 /) Can be initialized/filled using DO loops: DO Loop = 1, 10 Alpha (Loop) = 0.0 END DO

12 13 April, 2000 Project - checkout station Same data type structure, at most 15 of them Instead of checkout1, checkout2, … checkout15, Represent them and their properties by arrays e.g., checkout(1), checkout(2),…checkout(15) e.g., number of customers in each checkout station (i.e., each queue) and Idle_time for each station INTEGER, DIMENSION(15) : Ncustomers, Idle_time Ncustomers(n) is the number of customers in queue n Idle_time(n) is the Idle_time accumulated for queue n

13 13 April, 2000 Queue Data: Ncustomers Given nstations as user input for the number of check out stations ==> number of queues needed initialization: DO I=1,nstations Ncustomers(I) = 0 Idle_time(I) = 0 END DO

14 13 April, 2000 Finding the line with least number of customers: shortestline=1 DO I=2, nstations IF (Ncustomers(I).LT. Ncustomers(shortestline)) & shortestline=I END DO 56453875645645387564 Ncustomers(1) Ncustomers(2) : Initially: Shortest line is first line shortest I = 2 Ncustomers(2).LT. Ncustomers(1) 1 I = 2 Ncustomers(3).LT. Ncustomers(1)  3 I = 2 Ncustomers(4).LT. Ncustomers(3) I = 2 Ncustomers(5).LT. Ncustomers(3)  5 I = 2 Ncustomers(6).LT. Ncustomers(5) I = 2 Ncustomers(7).LT. Ncustomers(5) I = 2 Ncustomers(8).LT. Ncustomers(5) I = 2 Ncustomers(9).LT. Ncustomers(5) I = 2 Ncustomers(10).LT. Ncustomers(5)

15 13 April, 2000 Add_to_Queue Use Subroutine to implement this subproblem SUBROUTINE Add_to_queue ! *** coding details later ! You will have to tell this subroutine which ! queue to add the customer (i.e., pass in a parameter ! shortestline) ! *** you will be provided with a subroutine to add ! customer to a queue Call AddTo (shortestline) ! Other book keeping activities Ncustomers(shortestline) = Ncustomers(shortestline)+1 END SUBROUTINE Add_to_queue

16 13 April, 2000 Checkout Customers Use Subroutine to implement this subproblem SUBROUTINE Check_out ! *** coding details later ! *** you will be provided with a subroutine to remove ! customer from a queue DO I = 1, nstations IF (Ncustomers(I).NE. 0.AND. Finished) THEN Call POP_queue (I) Ncustomers(I) = Ncustomers(I)-1 END IF END SUBROUTINE Check_out

17 13 April, 2000 Gather statistics Track Idle_time DO I=1,nstation IF (Ncustomers(I).EQ. 0) Idle_time(I) = Idle_time(I)+1 END DO


Download ppt "13 April, 2000 CS1001 Lecture 17 One-Dimensional Arrays Using 1-D arrays in Project."

Similar presentations


Ads by Google