1 / 12

CS1001 Lecture 21

CS1001 Lecture 21. Application of Arrays Queue More on Project Random Number Generation. Queue. Queue is used to implement First-in-first-out processes. Operations include insert (or add) and delete (or remove)

cera
Download Presentation

CS1001 Lecture 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1001 Lecture 21 • Application of Arrays • Queue • More on Project • Random Number Generation

  2. Queue • Queue is used to implement First-in-first-out processes. • Operations include insert (or add) and delete (or remove) • Simple implementation (enhanced from stack by using two pointers : Front and Rear) For example: queue size is 5 Insert 70, Insert 80, Insert 50 give: 70, 80, 50, ___, ___ Remove, remove give: ___, ___, 50, ___, ___ and Insert 60, Insert 90 give: ___, ___, 50, 60, 90 Before another element can be added, must shift back to beginning • Better Technique -- “circular array” front rear front rear front rear

  3. Circular For example: Queue_size is 5. Queue array starts from 0 to queue_size -1. Front points to just before the first element of queue. Rear points to the last element. Can store Queue_size-1 numbers 4 4 4 rear front 60 0 0 3 0 50 50 50 90 3 3 80 70 rear 2 1 2 2 1 rear 1 front front Insert 70, Insert 80, Insert 50 give: Remove, remove give: and Insert 60, Insert 90 give:

  4. Initial 4 front Queue_size = 5 0 3 rear 1 2

  5. Insert (or Add) 4 front Queue_size = 5 Set Temp = (1+ Rear) (mod Queue_size) IF Temp = = Front then Signal a queue_full condition ELSE Set rear = Temp Set Queue(Rear) equal to the Number being inserted END IF 50 0 Temp 3 70 80 1 2 rear

  6. Remove 4 front Queue_size = 5 IF Rear = = Front then Signal a empty_queue condition ELSE Set Front = (1+Front) (Mod Queue_size) Set Number = Queue(Front) END IF 0 50 3 4 80 70 front 1 2 rear 0 3 rear 1 2

  7. QueueTester - Sample run Addq 22 Newlocation = mod(2,4)=2 Addq 11 Newlocation = mod(1,4)=1 Addq 33 Newlocation = mod(3,4)=3 Capacity = 4 0 1 2 3 Front Rear Front Rear Front Rear Front Rear 11 11 22 11 22 33 Addq 44 Newlocation = mod(0,4)=0 Newlocation = 0 = front so: print *, “Queue is full” Front Rear 0 1 2 3 11 22 33

  8. QueueTester - Sample run remq Front /= Rear Front = mod(2,4)=2 number = Queue(front) = Queue(2) = 22 remq Front /= Rear Front =mod(1,4)=1 number=Queue(front) = Queue(1) =11 Capacity = 4 0 1 2 3 Front Rear 11 22 33 Front Rear Front Rear 11 22 33 11 22 33 remq Front /= Rear Front= mod(3,4)=3 number=Queue(3)=33 Remq Front == Rear so: print *, “Queue is empty” 11 22 33 Front Rear

  9. Queue in Project In each line, there are customers. Each customer can be represented by an integer (the minute that the customer get generated) Line 1: 1, 4 Line 2: 1, 5 Line 3: 1, 7 Line 4: 2, 7 Line 5: 2, 10, 15 Note: This looks like a table - a two dimensional structure Number of rows may be number of checkout lines, each row is a queue

  10. Table -- 2-D Array • Twodimensional arrays are useful when data being processed can be arranged in rows and columns • Our checkout lines may be represented as an 2-D array: e.g., INTEGER, DIMENSION (15, 0:24) :: Checkout where max number of lines is 15 and max number of customers in a line is 24 line 1 is Checkout(1,0:24) line J is Checkout(J,0:24) We can then treat each row as a queue

  11. Random Numbers • Stochastic simulations use random number generators to introduce randomness • Random number generator is a subprogram that produces a number selected “at random” from a “distribution” • Fortran provides two intrinsic subroutines • RANDOM_SEED - initialize the random number generation process • RANDOM_NUMBER(Harvest) - returns values for HARVEST selected randomly between 0 and 1 from a uniform distribution For example, rolling a die: R1 is real, Die is an integer CALL RANDOM_SEED CALL RANDOM_NUMBER(R1) Die = 1 + INT(6*R1) R1 is real >= 0 and < 1 6*R1 is real >= 0 and < 6 INT (6*R1) is 0,1,2,..,or 5 1+INT(6*R1) is 1,2,..., or 6

  12. Random Number Generation • Random numbers having Poisson distribution can be generated from a uniform distribution INTEGER FUNCTION Random_Exp(Mean) REAL :: Mean, R1 DO CALL RANDOM_NUMBER(R1) IF (R1 .GT. 1.0E-10) EXIT END DO Random_Exp = (-Mean* LOG(R1))+0.5 END FUNCTION Random_Exp

More Related