computers in civil engineering cee3100 spring 2002 lecture #3

12
Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3 General Programming Concepts

Upload: kay-salas

Post on 31-Dec-2015

22 views

Category:

Documents


0 download

DESCRIPTION

Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3. General Programming Concepts. Programming Concepts. General discussion Class-specific aspects. Programming Steps. Clearly identify and understand the problem Clearly identify the desired input and output - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Computers in Civil EngineeringCEE3100 Spring 2002

Lecture #3

General Programming Concepts

Page 2: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Programming Concepts General discussion Class-specific aspects

Page 3: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Programming Steps Clearly identify and understand the problem Clearly identify the desired input and output Use ‘pseudo-code’ to design your solution

strategy and algorithm Write source code debug, debug, and debug… the source code Test code on cases with known results; revise

source, compile, debug, .., test, … Document code

Page 4: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Algorithm Definition

- A Sequence of Logical Steps Required to Perform a Task

Characteristics- Finite number of steps- Considers all possibilities- Anticipates errors in input data

Page 5: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Program Composition Program

- A sequence of instructions for computer High-level Languages

- FORTRAN. Original “language of engineers”; still popular for heavy-duty number-crunching; not the best for more general work

- Pascal: a well-structured language; starting to be rare- C, C++, Java, Visual Basic: graphics, PC applications

Other - MATLAB, MATHEMATICA, MATHCAD

Page 6: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Debugging & Testing Types of errors (bugs):

- Syntax (spelling) detected by compiler- Semantic (logic) Difficult to detect!

Page 7: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Example – Logic Error

! Calculate arithmetic mean of a vector Dim n As Integer Dim m As Integer

Dim sum As Single,average As Single

Dim A(n) As Single

! (procedures to load A, scalar n) sum = 0.0 For i = 1 To n Sum = Sum + a(i) Next i

average=sum/m! Will the compiler catch this error?

Page 8: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Some advice to avoid much grief:

NEVER divide without first testing that the denominator is non-zero.

If m = 0 then MsgBox “Oops: denominator m=0”else average=sum/nEndif! The denominator may be a big nasty Expression: TEST IT FIRST!

Page 9: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Documentation of Your Work Internal (use of comments): worth its weight in

gold! External

- README files- Messages printed by program- File naming conventions can be self-documenting

You are the main user! Can you understand your work if you pick it

up after five years????

Page 10: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Storage and Maintenance Logical Directory Organization. E.g.:

ehabib/compce/module01/lab

/hw

Make Backups: Keep multiple copies Revisions

- Try to avoid having multiple versions of a code – it only leads to grief

Page 11: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

Error-Mitigation Approach to Programming

Algorithm development & analysis Modular design of code Individual testing of code elements Analysis of the solution Explain your code to someone else

Page 12: Computers in Civil Engineering CEE3100 Spring 2002 Lecture #3

To Be Continued …

(Please read Chapter 2 of Chapra & Canale)