Document 14539462

advertisement
Physics 2660: Fundamentals of Scientific Computing Lecture 3 News and Announcements
•  I am not Professor Neu!
•  He is away this week at CERN
•  Working on preparing results for an important upcoming series of conferences
–  Studies of the Higgs boson interaction with the top quark
–  Studies of b quarks produced with top-­‐‑quark pairs
–  Search for production of black holes
2
Reminders
•  Weekly readings and homeworks will be posted on the class wiki site in general w/o any official notification to you – please check the links frequently
•  Prepare for labs!
–  Make sure the readings are complete
–  Review the class notes from lecture
•  Do your homework!
–  Electronic submissions by noon every Thursday unless otherwise scheduled
–  HW02 due this Thursday by noon
•  Do not fall behind – this is a crucial time the next few weeks.
3
Reminder
•  Prof. Neu’s office hours this week are canceled
–  He is available via email, chat, twiSer all week
–  Do not hesitate to contact him if you have questions or concerns
•  TA office hours, also in Room 022
–  Mondays 5:00-­‐‑8pm
–  Tuesdays 5:00-­‐‑8pm
4
Announcement: Repeat from Last Time
•  From now on, compile your code using the –Wall (“minus W all”) flag:
•  The –Wall flag tells the compiler to report to you ALL warnings the compiler wants to issue (by default these are suppressed.
•  Types of warnings:
–  use of un-­‐‑initialized variables
–  definition of unused variables
–  syntax problems
•  Warnings are not good!!
•  We will start penalizing in homework grading for compiler warnings
5
Outline
•  From last time:
– 
– 
– 
– 
C program structure
Intro to a basic C program
Defining simple variables and doing arithmetic
FormaSed input/output via printf() and scanf()
•  Today:
– 
– 
– 
– 
– 
– 
How variables’ values are stored on a computer
More on formaSed I/O
Operators Intro to functions
Conditional executions in C (if/else, switch/case)
Loops ( for() , while(), do )
6
Storing Variables
7
Bits and Bytes
•  How is data stored on a computer?
–  Data is stored in bits – think of them as values that can have either the value 0 or 1, or switches
–  One value is called a bit
–  bits are grouped together in groups of 8, called a byte
–  8 bits are convenient for binary arithmetic – using the binary number system (1s and 0s only)
–  Examples: subscripts reflect base system
• 
• 
• 
• 
1d = 1b = 1*2d0
5d = 101b = 1*2d2 + 0*2d1 + 1*2d0
12d = 1100b
21d = 10101b
•  182d = 10110110b à
8
Familiar Storage Sizes
100
103 106
109
1015
1012
9
Storing Variables
•  Recall last time we introduced the most-­‐‑common C variable types:
–  int
–  float and double
–  char
int main(){
double velocity = 0.0;
float x = 0.0;
int number = 0;
char a = ‘a’;
float y = 0.0;
•  When you run your program, an area in your computer’s memory is }
set up to store the value for each of the variables you define.
…
•  Different variable types require a different amount of space.
•  Hence, one must be careful to match variable types with the actual use one envisions …cannot put a decimal number in an int, for instance
10
Storage of an integer: Example
•  For an integer we have up to 4 bytes = 32 bits of storage
•  So the variable “num” defined as this:
int num = 909337759;
is stored this way:
•  Comments:
–  One bit is reserved for the sign +/-­‐‑ (leading bit): •  leading bit = 1 •  leading bit = 0
–  So max value we can store is 2d31 -­‐‑ 1d = 2,147,483,647d
Implication:
If your OS uses only 32-­‐‑bit words
for memory addresses, the max memory address is 4,294,967,296…
meaning up to 4.3 GB of memory can be handled…
hence 32-­‐‑bit systems cannot have much more than 4 GB of memory.
11
Variable Storage: Examples
4 bytes
4 bytes
1 byte
12
Variables: How to know their size
Note the last two lines: compound types. 5/2 is rightfully evaluated as a decimal number but has two int’s as args – hence it is assigned the size of an integer.
So don’t try to divide integers and expect the right answer – unless you define things properly!
And the last one – the size of the result is given by the highest precision argument.
13
Using sizeof()
14
FormaWed Input / Output
15
I/O Format Specifications: Review from Last Time
•  Recall from lab00, you used printf() to print values to screen
•  Similar function scanf() is used to read in values from the user:
•  % is the control key indicating some I/O formaSed data
•  followed by
an I/O specifier:
“i” and “d” are similar but very different. Use “d” for integers until we cover in more detail.
16
Format Mismatches
*
17
Format Mismatches
*
18
Format Mismatches
*
19
Format Mismatches
*
20
Format Mismatches
*
21
Format Mismatches
*
22
Format Mismatches
*
23
Controlling the Appearance of Output: Display
*
24
Controlling the Appearance of Output: Escape Characters
25
Files and Input/Output
•  Essentially all of the lessons about controlling output can be applied to controlling / designing input to your program
•  printf() and scanf() are not very different
•  What about non-­‐‑interactive I/O?
–  Use of files
•  fopen(), fclose(), etc.
•  Some experience in last week’s lab02 and hw02
26
Operators
27
Operator Types
•  What kind of operations do we need to be able to do in a computer program?
•  Here are some:
– 
– 
– 
– 
– 
– 
Arithmetic (eg., +, -­‐‑, *, / )
Assignment (eg., = )
Increment / decrement (eg., ++, -­‐‑-­‐‑) used in counters
Logical / Conditional (eg., && , || )
Bitwise operations…
Pointer operations…
28
Arithmetic Operators
29
Assignment Operators
30
Increment/Decrement Operators
31
Logical / Conditional Operators
32
Operator Precedence: Which Comes First?
or
33
Bad Coding Example: I
34
Bad Coding Example: II
35
Functions
36
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!
37
C Standard Library Functions
38
C Standard Library Functions
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.
39
Functions from the C math library
40
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
41
No functions – repetitive!
User-­‐‑Defined Functions: Example Use Case
42
Three main parts:
Prototype, Definition, and Calls
User-­‐‑Defined Functions: Use Case Example
43
User-­‐‑Defined Functions: Use Case Example
44
User-­‐‑Defined Functions: Reusable?
45
Conditional Executions in C
46
Linear Code Execution in C
•  Simple pieces of code have lines of instructions that are executed linearly, calling external functions like printf:
#include <stdio.h>
int main(void)
{
int num = 5;
printf(Hello, world. num = %d\n”,num);
return 0;
}
•  This is not typical however
•  Most programs need to do more intricate things in their execution
47
Conditional Executions in C: if statements
48
Conditional Checks
49
Conditional Execution: if and else
50
Conditional Execution: if and else
51
We’ll pick up from here next time. Don’t forget to do submit homework by Thursday @ noon. And of course do not forget lab on Thursday. See you then!
52
Download