Presentation is loading. Please wait.

Presentation is loading. Please wait.

27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines More Project Design and code segments.

Similar presentations


Presentation on theme: "27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines More Project Design and code segments."— Presentation transcript:

1 27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines More Project Design and code segments

2 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.

3 27 March, 2000 Range of noise level example Text pages 223-228 LargestLevel = 0 SmallestLevel = 999 DO Print * “Noise level?” Read *, NoiseLevel IF (NoiseLevel > LargestLevel) THEN LargestLevel = NoiseLevel Else IF (NoiseLevel < SmallestLevel) THEN SmallestLevel = NoiseLevel END IF IF (NoiseLevel < 0) EXIT END DO PRINT *, LargestLevel-SmallestLevel Input data 94 102 88

4 27 March, 2000 Desk checking Trace Table - recording in a table, step by step, the important variables LoopNoiseLevelLargestLevelSmallestLevel First pass9494999 Second pass102102999 Third pass8810288 Fourth pass-1102-1 Therefore LargestLevel-SmallestLevel = 102- (-1) = 103 Use temporary print statement in code Use special system debugging software

5 27 March, 2000 Modify and retest LargestLevel = 0 SmallestLevel = 999 DO Print * “Noise level?” Read *, NoiseLevel IF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel) THEN LargestLevel = NoiseLevel Else IF (NoiseLevel < SmallestLevel) THEN SmallestLevel = NoiseLevel END IF END DO PRINT *, LargestLevel-SmallestLevel Input data 94 102 88 output is 14 What if? Input data 88 94 102

6 27 March, 2000 Desk Check of revision LoopNoiseLevelLargestLevelSmallestLevel First pass8888999 Second pass9494999 Third pass102102999 Fourth pass-1102999 Therefore LargestLevel-SmallestLevel = 102 - (999) = -897 LoopNoiseLevelLargestLevelSmallestLevel First pass9494999 Second pass102102999 Third pass8810288 Fourth pass-110288 Therefore LargestLevel-SmallestLevel = 102- (88) = 14

7 27 March, 2000 Modify & Retest LargestLevel = 0 SmallestLevel = 999 DO Print * “Noise level?” Read *, NoiseLevel IF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel) LargestLevel = NoiseLevel IF (NoiseLevel < SmallestLevel) SmallestLevel = NoiseLevel END DO PRINT *, LargestLevel-SmallestLevel

8 27 March, 2000 Desk Check of revision 2 LoopNoiseLevelLargestLevelSmallestLevel First pass888888 Second pass949488 Third pass10210288 Fourth pass-1102-1 Therefore LargestLevel-SmallestLevel = 102 - (88) = 14 LoopNoiseLevelLargestLevelSmallestLevel First pass949494 Second pass10210294 Third pass8810288 Fourth pass-1102-1 Therefore LargestLevel-SmallestLevel = 102- (88) = 14

9 27 March, 2000 More Efficient Implementation Print * “Noise level?” Read *, NoiseLevel LargestLevel = NoiseLevel SmallestLevel = NoiseLevel DO IF (NoiseLevel < 0) EXIT IF (NoiseLevel > LargestLevel) THEN LargestLevel = NoiseLevel ELSE IF (NoiseLevel < SmallestLevel) SmallestLevel = NoiseLevel END IF Print * “Noise level?” Read *, NoiseLevel END DO

10 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

11 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

12 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

13 27 March, 2000 Project Introduction Checkout stations Servers check customers out at an average of average-service-time Customers arrival rate

14 27 March, 2000 Project Introduction (cont’d) Grocery Simulation Output StatisticsGet parametersRun Simulation Simulation Clock Generate Customers Checkout Customers Gather Statistics

15 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

16 27 March, 2000 Generate Customers Define a function to generate new customers: NArrivals = CustomerGeneration ( ) Put new customers into queue IF (NArrivals.NE. 0) then DO I = 1, NArrivals ! Find Shortest or appropriate queue ! Add to queue END DO END IF

17 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.

18 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

19 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

20 27 March, 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

21 27 March, 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

22 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. Initialization DO I=1,nstation Idle_time(I) = 0 END DO Track Idle_time DO I=1,nstation IF (Ncustomers(I).EQ. 0) Idle_time(I) = Idle_time(I)+1 END DO


Download ppt "27 March, 2000 CS1001 Lecture 9 Program Debugging Previews of Arrays, Functions, and Subroutines More Project Design and code segments."

Similar presentations


Ads by Google