27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines...
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of 27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines...
27 March, 2000
CS1001 Lecture 9
• Program Debugging• Previews of Arrays, Functions, and Subroutines• More Project Design and code segments
27 March, 2000
Errors in program development• Common errors:
– syntax or compiler errors -- errors discovered during compilation; e.g., punctuation, unbalanced ( )
– Run-time errors -- errors detected during execution of the program; e.g., division by zero, overflow
– Logic errors -- errors in design of algorithms and in coding the algorithms
• System error messages help solve syntax and run-time errors
• Programmers must debug and solve logic error by using their knowledge of the problem, the algorithm and the coding.
27 March, 2000
Range of noise level example
Text pages 223-228LargestLevel = 0SmallestLevel = 999DO
Print * “Noise level?”Read *, NoiseLevelIF (NoiseLevel > LargestLevel) THEN
LargestLevel = NoiseLevelElse IF (NoiseLevel < SmallestLevel) THEN
SmallestLevel = NoiseLevelEND IFIF (NoiseLevel < 0) EXIT
END DOPRINT *, LargestLevel-SmallestLevel
Input data9410288-1
27 March, 2000
Desk checking
• Trace Table - recording in a table, step by step, the important variables
Loop NoiseLevel LargestLevel SmallestLevelFirst pass 94 94 999Second pass 102 102 999Third pass 88 102 88Fourth pass -1 102 -1
Therefore LargestLevel-SmallestLevel = 102- (-1) = 103
• Use temporary print statement in code • Use special system debugging software
27 March, 2000
Modify and retest
LargestLevel = 0SmallestLevel = 999DO
Print * “Noise level?”Read *, NoiseLevelIF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel) THEN
LargestLevel = NoiseLevelElse IF (NoiseLevel < SmallestLevel) THEN
SmallestLevel = NoiseLevelEND IF
END DOPRINT *, LargestLevel-SmallestLevel
Input data9410288-1output is 14
What if?Input data8894102-1
27 March, 2000
Desk Check of revision
Loop NoiseLevel LargestLevel SmallestLevelFirst pass 88 88 999Second pass 94 94 999Third pass 102 102 999Fourth pass -1 102 999Therefore LargestLevel-SmallestLevel = 102 - (999) = -897
Loop NoiseLevel LargestLevel SmallestLevelFirst pass 94 94 999Second pass 102 102 999Third pass 88 102 88Fourth pass -1 102 88Therefore LargestLevel-SmallestLevel = 102- (88) = 14
27 March, 2000
Modify & Retest
LargestLevel = 0SmallestLevel = 999DO Print * “Noise level?” Read *, NoiseLevel IF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel) LargestLevel = NoiseLevel IF (NoiseLevel < SmallestLevel) SmallestLevel = NoiseLevelEND DO
PRINT *, LargestLevel-SmallestLevel
27 March, 2000
Desk Check of revision 2
Loop NoiseLevel LargestLevel SmallestLevelFirst pass 88 88 88Second pass 94 94 88Third pass 102 102 88Fourth pass -1 102 -1Therefore LargestLevel-SmallestLevel = 102 - (88) = 14
Loop NoiseLevel LargestLevel SmallestLevelFirst pass 94 94 94Second pass 102 102 94Third pass 88 102 88Fourth pass -1 102 -1Therefore LargestLevel-SmallestLevel = 102- (88) = 14
27 March, 2000
More Efficient Implementation
Print * “Noise level?”Read *, NoiseLevelLargestLevel = NoiseLevelSmallestLevel = NoiseLevelDO IF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel)
THEN LargestLevel = NoiseLevel ELSE IF (NoiseLevel < SmallestLevel)
SmallestLevel = NoiseLevel END IF Print * “Noise level?” Read *, NoiseLevelEND DO
27 March, 2000
Preview of Arrays
• Primitive data types vs structured data types• An array is a data structure
– elements are of the same type– access to elements by index -- elements are subscript variables– Specification and declaration:
REAL, DIMENSION(l:u) :: array-name
– Example:INTEGER, DIMENSION(1:10) :: Input
INTEGER :: Range, Choice
DO Range=1, 10, 1
READ *, Input(Range)
END DO
27 March, 2000
Array Preview Continued
• Why arrays? Because you can operate on the subscript.• Example, continued:
PRINT *, “Pick a box from 1 to 10”
READ *, Choice
PRINT *, “Box ”, Choice, “ contains number ”, Input(Choice)
• You can perform much more complex operations on the variable used for the subscript
27 March, 2000
Preview of Functions and Subroutines• Fortran provides library functions for many common
operations and functions. (Chap 2)• User defined functions and subroutines
– For large complex problems, divide the problem into a number of simpler problems.
– Each subproblem can be considered separately, designed and coded separately.
– These codes of subproblems are then combined to form the whole solution.
– Each of these separate set of code -- functions and subroutines– Functions and subroutines are controlled by the main program
27 March, 2000
Project Introduction
Checkout stations
Servers check customers out at an average of average-service-time
Customers arrival rate
27 March, 2000
Project Introduction (cont’d)
Grocery Simulation
Output StatisticsGet parameters Run Simulation
SimulationClock
GenerateCustomers
CheckoutCustomers
GatherStatistics
27 March, 2000
Run Simulation
• At time zero, set parameters and initialize • Simulation Clock
Given Sim_Time: The simulation clock may be
DO Time = 1, Sim_Time
Generate Customers
Checkout Customers
Gather Statistics for time step Time
END DO
27 March, 2000
Generate Customers
• Define a function to generate new customers: NArrivals = CustomerGeneration ( )
• Put new customers into queueIF (NArrivals .NE. 0) then
DO I = 1, NArrivals
! Find Shortest or appropriate queue
! Add to queue
END DO
END IF
27 March, 2000
Queue
• Represent each check_out station as a FIFO queue• Should have
1. ways to have customers come in and go out
2. A idle_time counter to keep track of number of minutes when there is no customer in the queue.
3. At a time step (a minute), if no customer in queue, then we can increase idle_time counter by 1 minute.
4. Should we have a counter to see how many customers are in the queue? Probably more efficient.
27 March, 2000
Queue
• Same data type structure, at most 15 of them• Represent some of its properties by arrays• e.g., number of customers in queue
INTEGER, DIMENSION(15) : Ncustomers
Ncustomers(1) is the number of customers in queue 1,
Ncustomers(2) is the number of customers in queue 2,
etc.
• Given nstations as user input for the number of check out stations ==> number of queues needed
27 March, 2000
Queue (Ncustomers)• initialization:
DO I=1,nstations
Ncustomers(I) = 0
END DO
• Finding the line with least number of customers:shortestline=1
DO I=2, nstations
IF (Ncustomers(I) .LT. Ncustomers(shortestline)) & shortestline=I
END DO
27 March, 2000
Add_to_Queue
• Use Subroutine to implement this subproblemSUBROUTINE 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
27 March, 2000
Checkout Customers
• Use Subroutine to implement this subproblemSUBROUTINE 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
27 March, 2000
Gather statistics• Idle_time can be handled the same way as Ncustomers
INTEGER, DIMENSION(15) : Ncustomers , Idle_time
Idle_time(1) is the number of idle minutes in queue 1,
Idle_time(2) is the number of idle minutes in queue 2, etc.
• InitializationDO I=1,nstation
Idle_time(I) = 0
END DO
• Track Idle_timeDO I=1,nstation
IF (Ncustomers(I) .EQ. 0) Idle_time(I) = Idle_time(I)+1
END DO