Document 14539464

advertisement
Physics 2660: Fundamentals of Scientific Computing Lecture 5 Announcements
•  HW04 is due Friday 26 Feb at noon
•  Do not forget to pledge your HWs!
•  Grading of HW03 will be done by the weekend
•  Lab solutions are coming!
•  My office hours:
–  Tuesdays 3:15-­‐‑5pm in Room 022-­‐‑C
•  TA office hours, also in Room 022-­‐‑C
–  Mondays 5:00-­‐‑8pm
–  Tuesdays 5:00-­‐‑8pm
2
Announcements
•  Midterm Exam:
–  Scheduled for next Tuesday 1 March
–  Requests from some students to move the exam to the following week…which is Spring Break
3
Announcements
•  Midterm Exam:
–  Scheduled for next Tuesday 1 March
–  Requests from some students to move the exam to the following week…which is Spring Break
–  We will reschedule the exam for the evening of Tuesday 15 March, the week you return from Spring Break
•  7pm in room 204 (this room)
4
Unsolicited Tips
•  Don’t neglect to use command history in linux. Try this:
– 
– 
– 
– 
log in to galileo
press up arrow
the last command you executed on galileo will appear!
more instances of up arrow will give you more of your command history
•  Launch emacs in a new window this way from your galileo session:
–  emacs myfile.C &
–  The ‘&’ in the Linux OS allows you to have access to your command line
session on galileo while editing your document – nice for debugging!
•  Linux tip: use ls –lrt to show when files were last modified and their
sizes, ownership
•  Use of multiple simultaneous terminal connections to galileo is
encouraged…allows you to compile and run in a different session from
your code editing
5
Review from Last Time and Today’s Outline
•  Last time:
–  More on loops •  conditioned controlled loops: while(), do –  Random numbers
•  Today:
– 
– 
– 
– 
– 
– 
– 
– 
– 
– 
– 
More on random numbers The scope of variables
More on functions: limitations and extensions
Pointers in C
Static variables
Recursive functions
Arrays in C
Passing arguments to main(..)
Creating re-­‐‑usable code
Review of pointers and pointers to functions
Intro to statistics
6
Back to Random Number Generation
7
Generating a Random Number
•  One can generate a random number in some range in which all the possibilities have the same probability:
–  The probability distribution function is sometimes called the “prior” in the vocabulary of statistics
•  Each value same probability = “flat prior”
•  Example: –  Tossing a coin: Assign heads = 0, tails = 1. Each has probability 0.5.
–  Tossing a single fair dice: Each integer value [1,6] has same probability = ~0.166667
•  A random number need not have a flat prior distribution!
–  Can have some other distribution…like a Gaussian, or some user-­‐‑defined thing.
–  We will explore this in lab this week.
8
An Example: Integration Using Random Numbers
9
An Example: Integration Using Random Numbers
10
Monte Carlo Techniques: Integration
11
Monte Carlo Integration
12
Multiple Measurements
13
Monte Carlo Techniques: Integration
14
The Scope of Variables
15
Scope
•  When one defines a variable in C, it is accessible only in a well-­‐‑defined extent within the program
•  Scope refers to where variables can be accessed
–  Variables defined inside a function are available only in that function. These variables are of local scope
–  Variables can be defined outside all functions and accessed anywhere. These variables are of global scope.
16
Global vs. Local Scope
17
Global vs. Local Scope
Not allowed:
int main(){
int a;
float a; // not allowed!
return(0);
}
18
Guidelines for Scope and Variable Definitions
•  In general, it is best to keep your variables limited to the smallest scope possible
•  Having a globally-­‐‑definied and accessible variable means it can be manipulated/changed ANYWHERE in your code – and this could lead to
–  confusion
–  bad results
–  heartache
19
Internal Comments and Documenting Code
20
Internally Documenting / Comments
21
Static Variables and Functions
22
Lost Information in Functions
•  As described before, typically variables created in functions are only temporary – their contents are lost when a function completes
23
Static Variables in Functions
•  We can get around this by declaring the variable in the function as static 24
Static Variables: Uses
•  Static variables can be used to…
–  flag a function as having already been called
–  keep track of how many times a function has been called
–  keep track of values a variable amained in previous calls
–  keep track of previous paramters sent to a function
25
Functions: Limitations and Further Uses
26
Functions As We Know Them So Far
•  The kinds of functions we have used so far
–  have some type
–  are given some arguments – which do not change in a global sense
–  return some value – which we then use
27
Functions As We Know Them So Far
28
Functions: More Functionality
•  So far we have used functions in this manner:
–  Supply some arguments to a function, some values we need for a calculation
–  The function uses those arguments/values to perform some task
–  The result is:
•  stored in some variable of global scope OR
•  returned as the result of the function OR
•  the function returns success / failure and the code proceeds accordingly
•  This behavior is somewhat limited!
•  single output is a limitation
•  cannot manipulate the arguments…this is often a less-­‐‑than-­‐‑desirable feature
•  We need a more general function interface
29
Functions: Two Types
•  Functions in C can be classified into two types:
–  pass-­‐‑by-­‐‑value: (what we have done so far)
•  the initial values of the arguments are not allowed to change globally
•  they can change interior to the function, but those changes are never carried out on the global variable
•  Copies of the input vars are made but not used beyond the function
–  pass-­‐‑by-­‐‑reference:
•  arguments can be values or memory locations
•  output can be “returned” through the parameter list
•  allows multiple results to be accessible to rest of program 30
Functions: Two Types
•  Functions in C can be classified into two types:
–  pass-­‐‑by-­‐‑value: (what we have done so far)
•  the initial values of the arguments are not allowed to change globally
•  they can change interior to the function, but those changes are never carried out on the global variable
•  Copies of the input vars are made but not used beyond the function
Pass-­‐‑by-­‐‑reference functions requires a discussion of pointers to variables in –  pass-­‐‑by-­‐‑reference:
memory….
•  arguments can be values or memory locations
•  output can be “returned” through the parameter list
•  allows multiple results to be accessible to rest of program 31
Pointers
32
Pointers: What are they?
•  Pointers are like variables that provide direct access to the memory location for a specific variable
•  One can then manipulate the contents of the memory location – and hence manipulate the contents of the variable from any location in the code
–  allows global access and manipulation
•  Pointers give them user more power and more freedom!
•  However, paraphrased from many (Voltaire, Churchill, FDR, Uncle Ben in Spiderman)
“With great power and freedom comes great responsibility.”
33
Recall: Variable Storage
34
Memory Addresses: The ‘&’ Operator
35
Memory Addresses: Storage in Functions
36
Memory Addresses: Storage in Functions
37
Pointers
38
Storing Pointers in Memory
39
Pointers: The Indirection Operator ‘*’
40
Pointers: Changing Data in a Memory Location
41
An Example: scanf
42
Passing Pointers As an Argument To A Function
43
Advantages: Passing Pointers As Args To Functions
•  Through the use of pointers passed to functions, we can manipulate an arbitrary number of variables, rather than just return one result
44
Pointers: Be Careful!
45
Pointers: Be Careful!
46
Recursive Functions
47
Recursion: An Example
•  So we really don’t need to calculate the product of N numbers, just the product of 2 numbers (N-­‐‑1) times, each time one of the numbers being new, one being retained from the previous iteration – recursion
48
Recursive Functions
49
We’ll pick up from here next time. See you then.
50
Download