Physics 2660: Fundamentals of Scientific 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 • Office hours reminder: – My office hours are in Room 022-­‐‑C (our computer lab) from 3:30-­‐‑5pm on Tuesdays or by appointment – TA office hours, also in Room 022-­‐‑C • Mondays 5-­‐‑8pm • Tuesdays 5-­‐‑8pm 2 News and Announcements • Mid-­‐‑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-­‐‑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-­‐‑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 – address via & – content via * • Passing pointers into functions: – In function prototype #include <stdio.h> void circle_stuff(double radius, double *a_ptr, double *c_ptr); void main(){ double radius = 3.0; double area = 0.0; double circumference = 0.0; • use pointer-­‐‑to-­‐‑values as argument(s) circle_stuff(radius, &area, &circumference); printf(“area=%8.3f circumference= %8.3f\n”, area, circumference); – In call to function • call with pointer-­‐‑to-­‐‑address as argument(s) – In function definition, • use pointer-­‐‑to-­‐‑values as argument(s) as done in prototype • use pointer-­‐‑to-­‐‑values in body of function } void circle_stuff(double radius, double *a_ptr, double *c_ptr){ *a_ptr = 3.1416*radius*radius; *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-­‐‑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-­‐‑space – Good for matrices – Good for collections of related items – Good generally for similar pieces of data 15 An example: A vector in 3-­‐‑space * 16 An example: A vector in 3-­‐‑space using an array * 17 Defining 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 define 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 defining 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 scientific 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-­‐‑term Exam 40 Information on Mid Term Exam • Your mid-­‐‑term is coming! • Tuesday 15 March in this room at 7pm – You will have 60 min to finish – Bring a number 2 pencil – Regular lecture and labs will of course meet that week • Format: – multiple choice – matching – short answer • 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 – address via & – content via * • Passing pointers to functions: – In function prototype #include <stdio.h> void circle_stuff(double radius, double *a_ptr, double *c_ptr); void main(){ double radius = 3.0; double area = 0.0; double circumference = 0.0; • use pointer-­‐‑to-­‐‑values as argument(s) circle_stuff(radius, &area, &circumference); printf(“area=%8.3f circumference= %8.3f\n”, area, circumference); – In call to function • call with pointer-­‐‑to-­‐‑address as argument(s) – In function definition, • use pointer-­‐‑to-­‐‑values as argument(s) as done in prototype • use pointer-­‐‑to-­‐‑values in body of function } void circle_stuff(double radius, double *a_ptr, double *c_ptr){ *a_ptr = 3.1416*radius*radius; *c_ptr = 2.0*3.1416*circumference; } 49 Questions About the Basics • What is an operating system? • What are the main hardware parts of a computer? • What is a compiler? • Etc. 50 Questions About the Linux Shell 51 Questions About the C Language 52 Questions About the C Language 53 Questions About the C Language 54 Questions About the C Language 55 We’ll pick up from here next time. Thursday labs -­‐‑-­‐‑ see you then. 56