CS1001%20Lecture%2021 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001%20Lecture%2021

Description:

Number of rows may be number of checkout lines, each row is a queue. 18 April, 2000 ... Our checkout lines may be represented as an 2-D array: e.g. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 13
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: CS1001%20Lecture%2021


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
1
rear
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
50
0
Temp
3
80
70
1
2
rear
  • 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

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

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 22
11
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 numberQueue
(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 numberQueue
(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
  • Two dimensional 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, 024) Checkout
  • where max number of lines is 15 and max number of
    customers in a line is 24
  • line 1 is Checkout(1,024)
  • line J is Checkout(J,024)
  • 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(6R1)

R1 is real gt 0 and lt 1 6R1 is real gt 0 and lt
6 INT (6R1) is 0,1,2,..,or 5 1INT(6R1) 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
Write a Comment
User Comments (0)
About PowerShow.com