1 / 26

Programming

Programming. Administrivia. From santaclaus@northpole.org Thu Nov 8 12:05:31 2001 Date: Thu, 8 Nov 2001 12:04:36 -0500 (EST) From: santaclaus@northpole.org no goodies for you just a lump of coal & T From santaclaus@northpole.org Thu Nov 8 12:05:31 2001

Download Presentation

Programming

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

  2. Administrivia From santaclaus@northpole.org Thu Nov 8 12:05:31 2001 Date: Thu, 8 Nov 2001 12:04:36 -0500 (EST) From: santaclaus@northpole.org no goodies for you just a lump of coal & T From santaclaus@northpole.org Thu Nov 8 12:05:31 2001 Return-Path: <santaclaus@northpole.org> Received: from Princeton.EDU (postoffice.Princeton.EDU [128.112.129.120]) by upright.CS.Princeton.EDU (8.11.6/8.11.6) with ESMTP id fA8H5QQ29528 for <dpd@cs.Princeton.EDU>; Thu, 8 Nov 2001 12:05:31 -0500 (EST) Received: from yuma.Princeton.EDU (yuma.Princeton.EDU [128.112.128.89]) by Princeton.EDU (8.9.3/8.9.3) with SMTP id MAA29199 for dpd; Thu, 8 Nov 2001 12:04:36 -0500 (EST) Date: Thu, 8 Nov 2001 12:04:36 -0500 (EST) From: santaclaus@northpole.org Message-Id: <200111081704.MAA29199@Princeton.EDU> Status: RO no goodies for you just a lump of coal

  3. Where we are • Built a machine • Built an operating system to control the machine • Now, want to run programs under the operating system

  4. What is programming • We did machine language • Single instructions that tell the hardware what to do • Primitive • Arithmetic, simple branching, communication with memory • We built state machines • States using memory • Transitions modeling tasks

  5. Problem solving • We’ve seen • Truth tables • Logic gates • States and transitions in a state machine • Machine language • Now, higher level programming language

  6. To build a computer program • Figure out what you want to do • Understand the rules that guide the process you are automating • Make sure that your rules are complete • Translate the rules into the computer language • Build structures to hold your data • Build tools to manipulate them • Make sure that the program does what the rules say

  7. Figuring out the rules • For traffic lights, • We stored data that told us the current color of lights • We read input from sensors • We had rules that told us whether to change state • We had rules that told us how to change state

  8. Traffic Light Behavior Otherwise IF A=1 AND B=0 Light A Always Always IF A=0 AND B=1 Otherwise Light B

  9. Turn Memory Into Variables • Store data to tell current color of lights • Dim LightA, LightB as Integer • ‘ 0 for red, 1 for yellow, 2 for green • Read input from sensors • Dim SensorA, SensorB as Integer • ‘ tell if cars are waiting

  10. Turn Rules Into Statements • Decide whether to change state • If LightA = 2 And SensorA = 0 And SensorB = 1 Then • ‘ here we want to specify that the colors change • If LightB = 2 And SensorA = 1 And SensorB = 0 Then • ‘ again, we want to specify that the colors change

  11. Build shell of program Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) If LightB = 2 And SensorA = 1 And SensorB = 0 Then ChangeGreenToYellow(LightB) ChangeYellowToRed(LightB) ChangeRedToGreen(LightA)

  12. Some Rules • Statements have to be in blocks • How does the computer know that it is If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) • And Not If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB)

  13. Some Rules • Statements have to be in blocks If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) End If

  14. More Rules • We have to tell the program to loop Do While condition action Loop

  15. More Rules • We have to tell the program to loop Do While StillWantToControlTraffic RunMyTrafficControlProgram Loop

  16. Procedures • Must fill in functions to change lights Private Sub ChangeGreenToYellow(Light As Integer) Light = 1 End Sub Private Sub ChangeYellowToRed(Light As Integer) Light = 2 End Sub Private Sub ChangeRedToGreen(Light As Integer) Light = 0 End Sub

  17. Could build Procedure of Procedures ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) Could become the commandChangeLights(LightA,LightB) Private Sub ChangeLights(Light1 As Integer, Light2 As Integer) ChangeGreenToYellow(Light1) ChangeYellowToRed(Light1) ChangeRedToGreen(Light2) End Sub

  18. Using the procedure ChangeLights(LightB,LightA) then does ChangeGreenToYellow(LightB) ChangeYellowToRed(LightB) ChangeRedToGreen(LightA)

  19. The program Private Sub ChangeGreenToYellow(Light As Integer) Light = 1 End Sub Private Sub ChangeYellowToRed(Light As Integer) Light = 2 End Sub Private Sub ChangeRedToGreen(Light As Integer) Light = 0 End Sub Private Sub ChangeLights(Light1 As Integer, Light2 As Integer) ChangeGreenToYellow(Light1) ChangeYellowToRed(Light1) ChangeRedToGreen(Light2) End Sub

  20. The program (cont.) Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeLights(LightA,LightB) End If If LightB = 2 And SensorA = 1 And SensorB = 0 Then ChangeLights(LightB,LightA)

  21. Make it happen forever Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer Dim StillWantToControlTraffic as Integer StillWantToControlTraffic = 1 Do While StillWantToControlTraffic If LightA = 2 And SensorA = 0 And SensorB = 1 Then ChangeLights(LightA,LightB) End If If LightB = 2 And SensorA = 1 And SensorB = 0 Then ChangeLights(LightB,LightA) End If Loop

  22. What could go wrong? • Program could get confused • Check for consistency • Replace Private Sub ChangeGreenToYellow(Light As Integer) Light = 1 End Sub • With Private Sub ChangeGreenToYellow(Light As Integer) If (Light = 0) Then Light = 1 Else ReportInconsistency() End If End Sub

  23. Building a bigger program • Could write this as a subroutine • Private sub ControlTrafficLight(light1,light2,sensor1,sensor2) • Could reuse the subroutine to do a whole string of lights. • But how would we keep track of hundreds of lights?

  24. Show the array structure • Build array • Keep track of things in array • Control all the traffic lights in Manhattan • But items in array have to track more info • Time for lights each way • Location • Maybe dependencies on other lights • So need better ways of storing data • Modern programming • Data are objects • There are methods for operating on data

  25. Having built up to datatypes • What are the relative merits of different languages • Visual vs. line oriented • Debugging • What the language gives you • Some history of programming languages • And tools • What is it about perl, awk, … • What does the compiler do?

  26. Let’s look at a bigger system • Maybe the registrar’s or PeopleSoft • Then joys and pitfalls of modern programming • How do we know if we’ve gotten it right?

More Related