physics 2660: fundamentals of scientific...

55
Physics 2660: Fundamentals of Scientific Computing Lecture 5 Instructor: Prof. Chris Neu ([email protected])

Upload: vuongdien

Post on 05-Jun-2018

251 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Physics 2660: Fundamentals of Scientific Computing

Lecture 5 Instructor: Prof. Chris Neu ([email protected])

Page 2: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Reminder

•  I am back!

•  HW04 due Thursday 22 Feb electronically by noon

•  HW grades are coming. Please be patient.

•  Office hours: all held in our computer lab, room 022-C of this bldg–  Me: After lecture 3:30-4:30 every Tuesday –  TAs:

•  Mondays: 3-5pm and 6-8pm•  Wednesdays: 5-9pm

2

Page 3: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Review and Outline•  Last time:

–  C program structure–  Intro to a basic C program–  Defining simple variables and doing arithmetic–  FormaWed input/output via printf() and scanf()–  How variables’ values are stored on a computer–  More on formaWed I/O

•  Today:–  Comments–  Functions–  Conditional structures:

•  if, if/else, if/elseif/else•  switch/case

–  Loops•  Count-controlled loops: for()More on loops •  Conditioned controlled loops: while(), do

–  Random numbers

3

Page 4: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Comments Inside Your Code

4

Page 5: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Internally Documenting / Comments

5

Page 6: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Functions

6

Page 7: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Introduction to Functions

•  C is a simple language

•  Its utility is extended through the use of re-usable functions

•  Some functions are found in standard libraries like stdio.h and math.h

•  Users can write functions too – for many good reasons!

7

Page 8: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

C Standard Library Functions

8

Page 9: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

C Standard Library Functions

9

Common C Standard Libraries we will use:•  stdio.h•  math.h

Best to get in the habit of including these in every program you write!

You don’t even know WHERE these files are on galileo in order to use them.

Page 10: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Functions from the C math library

10

Page 11: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

User-Defined Functions

•  Why would one want to write their own functions?

–  Avoid duplicating the same code many times within a program: streamlined simplicity

–  Re-usable functions are easier to maintain and modify

–  Functions are portable – can be used in other programs

–  Simplicity: code something intricate once and call it via a simple single line rather than multi-line complications

11

Page 12: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

User-Defined Functions: Example Use Case

12

No

func

tions

– re

petit

ive!

Page 13: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

User-Defined Functions: Use Case Example

13

Thre

e m

ain

part

s:

Prot

otyp

e, D

efini

tion,

and

Cal

ls

Page 14: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

User-Defined Functions: Use Case Example

14

Page 15: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

User-Defined Functions: Reusable?

15

Page 16: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Executions in C

16

Page 17: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Linear Code Execution in C

•  Simple pieces of code have lines of instructions that are executed linearly, calling external functions like printf:

•  This is not typical however•  Most programs need to do more intricate things in their

execution

17

#include <stdio.h> int main(void){int num = 5;printf(Hello, world. num = %d\n”,num);

return 0;}

Page 18: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Executions in C: if statements

18

Page 19: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Checks

19

Page 20: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Execution: if and else

20

Page 21: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Execution: if and else

21

Page 22: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Execution: switch and case

22

Page 23: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Conditional Execution: switch and case

23

Page 24: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

if/else versus switch/case

24

Page 25: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

The ? Operator

25

Page 26: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: “=“ versus “==“

26

Page 27: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: “=“ versus “==“

27

Take-away message:

Be careful to use “==“

in conditionals and not “=“.

Page 28: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Return Values and Tests

•  Values are returned for all conditional checks:–  if false, the value is zero ….. the value of the expression (5 < 3) is 0–  if true, the value is not zero ….. the value of the expression (5 > 3) is 1

•  We can use these characteristics in our code:

28

Page 29: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Return Values and Tests: From Functions

•  One can call a function that performs some test

29

“Did it fail?”

Page 30: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

30

Page 31: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time:

31

Page 32: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0

32

Page 33: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0–  What is A?

33

Page 34: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0–  What is A?–  Well, we all know the calculation should yield 10000000000.0

34

Page 35: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0–  What is A?–  Well, we all know the calculation should yield 10000000000.0

–  The computer would evaluate things in this manner:

35

float A = (10000000000.0/3.0)*3.0;if (A==10000000000.0 ){ printf(“A=%lf\n”,A);} else printf(“A is not 10000000000.0 \n”);

Page 36: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0–  What is A?–  Well, we all know the calculation should yield 10000000000.0

–  The computer would evaluate things in this manner:

–  What will be printed?

36

float A = (10000000000.0/3.0)*3.0;if (A==10000000000.0 ){ printf(“A=%lf\n”,A);} else printf(“A is not 10000000000.0 \n”);

Page 37: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

DANGER: Take Care with Floating-Point Numbers

•  Quiz time: –  Lets say A = (10000000000.0 / 3.0) * 3.0–  What is A?–  Well, we all know the calculation should yield 10000000000.0

–  The computer would evaluate things in this manner:

–  What will be printed?

37

float A = (10000000000.0/3.0)*3.0;if (A==10000000000.0 ){ printf(“A=%lf\n”,A);} else printf(“A is not 10000000000.0 \n”);

A is not 10000000000.0 What?

*

Page 38: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Floats and Comparisons

38

Page 39: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Floats and Comparisons

39

Page 40: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Loops!

40

Page 41: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Loops

•  Loops allow computers to do what computers do best:–  Execute repetive boring tasks efficiently and accruately over and over

again

•  Two types:

41

Page 42: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Count-Controlled Loops!

42

Page 43: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Count-controlled loops: for()

43

Page 44: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

How a for() loop works

44

Page 45: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

for() loop: Important Parts

•  Every for() loop has four important parts:–  counter initialization–  test condition–  counter

update–  body of

execution

45

Page 46: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Example: Good for() loop usage

46

Page 47: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Example: BAD for() loop usage

47

Page 48: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

for() loops and break statements

48

Page 49: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

for() loops and continue statements

49

Page 50: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Nested Loops

50

Page 51: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Condition-Controlled Loops!

51

Page 52: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Count-controlled Loops vs. Condition-controlled Loops

52

Page 53: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Pre-test Conditional Loops: while()

53

Page 54: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

Post-test Conditional Loops: do/while()

54

Page 55: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/pdf/L5... · Physics 2660: Fundamentals of Scientific Computing ... Internally

We’ll pick up from here next time.

See you Thursday in labs!

55