# Document 14539465

```Physics 2660: Fundamentals of Scientiﬁc Computing Lecture 6 News and Announcements
•  Voting Day!
•  Piazza
•  HW05 is due Friday 4 March at noon
•  HW06 will be assigned this week and will be due Thursday 17 March at noon
•  Oﬃce hours reminder:
–  My oﬃce hours are in Room 022-&shy;‐‑C (our computer lab) from 3:30-&shy;‐‑5pm on Tuesdays or by appointment
–  TA oﬃce hours, also in Room 022-&shy;‐‑C
•  Mondays 5-&shy;‐‑8pm
•  Tuesdays 5-&shy;‐‑8pm
2
News and Announcements
•  Mid-&shy;‐‑term exam will be Tuesday 15 March at 7pm in this room
–  Two weeks from now!
–  Will cover everything we have covered so far, including today!
–  Format: Mix of multiple choice, matching, short answer – no in-&shy;‐‑class coding, just need a Number 2 pencil!
–  More later in today’s lecture
3
Review and Today’s Outline
•  Last time:
–
–
–
–
Random numbers
MC integration
Pointers
Recursion
•  Today:
–
–
–
–
–
–
Pointers to functions
Arrays in C
Passing arguments to main(….)
Making re-&shy;‐‑usable code
Intro to statistics
Review of C so far and exam examples
4
Pointers to Functions and Trapezoidal Rule for Integration
5
Pointers to Variables: A Review
•  Pointers allow direct access to memory locations where variables are stored in 1’s and 0’s
–  content via *
•  Passing pointers into functions:
–  In function prototype
#include &lt;stdio.h&gt;
*a_ptr, double *c_ptr);
void main(){
double area = 0.0;
double circumference = 0.0;
•  use pointer-&shy;‐‑to-&shy;‐‑values as argument(s)
&amp;circumference);
printf(“area=%8.3f
circumference=
%8.3f\n”, area, circumference);
–  In call to function
•  call with pointer-&shy;‐‑to-&shy;‐‑address as argument(s)
–  In function deﬁnition, •  use pointer-&shy;‐‑to-&shy;‐‑values as argument(s) as done in prototype
•  use pointer-&shy;‐‑to-&shy;‐‑values in body of function
}
double *a_ptr, double *c_ptr){
*c_ptr = 2.0*3.1416*circumference;
}
6
How are Functions Stored in Memory?
7
Pointers to Functions: Example
8
Pointers to Functions: Pieces
9
Pointers to Functions: Example – Numerical Integration
Monte Carlo integration is one method one can use to integrate some complicated function, especially useful in multiplie dimensions.
10
Pointers to Functions: Example – Numerical Integration
11
Pointers to Functions: Example – Numerical Integration
12
Pointers to Functions: Example – Numerical Integration
13
Arrays in C
14
What is an Array?
•  Very often we need to store and access many instances or sets of related data
•  Example: coordinates in 3-&shy;‐‑space:
–  x=5, y=23, z=1
–  Can be represented by a vector: (5,23,1)
–  The points in space are of the same type – would like a “container” that holds them in a convenient format
•  Arrays do this for us!
•  Not just god for vectors in 3-&shy;‐‑space
–  Good for matrices
–  Good for collections of related items
–  Good generally for similar pieces of data
15
An example: A vector in 3-&shy;‐‑space
*
16
An example: A vector in 3-&shy;‐‑space using an array
*
17
Deﬁning Arrays
18
Using Arrays
19
Storage of Arrays in Memory
20
Storage of Arrays in Memory: Danger
21
Using Arrays: Danger
22
Passing Arrays to Functions
*
23
Multidimensional Arrays
24
Multidimensional Arrays
Do remember though: When you deﬁne an array of any size, the footprint of that array’s size is dedicated in the computer’s memory.
It is very easy to exhaust a computer’s memory by deﬁning arrays that are too large.
25
Arrays of Characters
*
Single characters are nice but …many in succession can be more meaningful to us.
Array of characters are called a string.
26
Arrays and Pointers
27
Passing arguments to main(…)
28
Passing arguments to main(…)
•  We have done this many times already….
29
Special parameters in C: argc and argv
30
Special parameters in C: argc and argv
31
An Example
*
32
The Keyboard Enters Strings Not Numbers!
33
Making Pieces of Code that Are Reusable
34
Reusable Code
•  Many tasks in computer programming for scientiﬁc purposes are confronted over and over again
–  imagine calculating the sine of some angle or the distance between two points in some linear algebra project
–  imagine every time you needed that value, having to write a piece of new code to do the calculation
•  There is point in reinventing the wheel every time
•  Utilities such as these – and many others we can think of – are best coded once and reused over and over again
–  reduction of chance for bugs
–  easier debugging
–  portability of code to other users
35
Reusable Code: Option I
36
Reusable Code: Option I
Drawback of this Option:
You have to compile the extra code every time!
37
Reusable Code: Option II
38
Reusable Code: Option II
39
Mid-&shy;‐‑term Exam
40
Information on Mid Term Exam
•  Tuesday 15 March in this room at 7pm
–  You will have 60 min to ﬁnish
–  Bring a number 2 pencil
–  Regular lecture and labs will of course meet that week
•  Format:
–  multiple choice
–  matching
•  Let’s review some of the things we have learned so far
•  Some examples follow
41
Review of C: What We Have Learned So Far
42
Variables
Variables…
43
Input and Output
44
Files
45
Loops
46
Conditionals
47
Functions
48
Pointers
•  Pointers allow direct access to memory locations where variables are stored in 1’s and 0’s
–  content via *
•  Passing pointers to functions:
–  In function prototype
#include &lt;stdio.h&gt;
*a_ptr, double *c_ptr);
void main(){
double area = 0.0;
double circumference = 0.0;
•  use pointer-&shy;‐‑to-&shy;‐‑values as argument(s)
&amp;circumference);
printf(“area=%8.3f
circumference=
%8.3f\n”, area, circumference);
–  In call to function
•  call with pointer-&shy;‐‑to-&shy;‐‑address as argument(s)
–  In function deﬁnition, •  use pointer-&shy;‐‑to-&shy;‐‑values as argument(s) as done in prototype
•  use pointer-&shy;‐‑to-&shy;‐‑values in body of function
}
double *a_ptr, double *c_ptr){
*c_ptr = 2.0*3.1416*circumference;
}
49
•  What is an operating system?
•  What are the main hardware parts of a computer?
•  What is a compiler?
•  Etc.
50