Document 14539461

advertisement
Physics 2660: Fundamentals of Scientific Computing Lecture 2 Announcements
•  HW01 is due this Saturday, electronically by noon.
•  Start compiling using this command:
•  My office hours:
–  3:30-­‐‑5pm Tuesdays in Room 022-­‐‑C (our computer lab)
•  TA office hours
–  In Room 022-­‐‑C
•  Mondays 5:00-­‐‑8:00 pm
•  Tuesdays 5:00-­‐‑8:00 pm
2
Review and Outline
•  Recall last time:
–  The Linux command line, shells, simple commands
–  Remote connections, both shell and graphics
•  Today:
– 
– 
– 
– 
– 
– 
– 
Intro to programming languages and compilers
C program structure
Defining simple variables and doing arithmetic
FormaQed input/output
Operators Functions
Storing variable and variable types in C
3
Computer Programming in C: The Beginnings
4
Programming Languages
•  As we mentioned earlier, the hardware inside a computer does not speak a human-­‐‑readable language
•  But we as humans want to harness the power of the computer as a tool to solve problems
•  Two options: –  We communicate directly with computer using its language
–  We write programs in something more familiar to us, and figure out how to translate
•  Option 2 is preferred!
5
Programming Langauges
Dennis Ritchie:
-­‐‑ defined the relatively simple syntax of C as part of original Unix R&D
-­‐‑ wrote the first compiler for C programs
Standard simple language meant people could easily write compilers for any CPU architecture AND users could easily write new programs
6
Compilers
7
Our compiler: g++
8
C program structure
9
The C Language: An Example
*
10
Preprocessor Directives
11
Defining and Assigning Variables
12
Arithmetic
13
Using Functions
14
Variable Definition in C
15
Variable Types in C
•  All variables in C must be declared as a particular type •  Note: a “string” of characters in C is an array of single chars
16
Defining Variables in C
*
•  In C, variables should be defined at the beginning of the function in which they are being used:
17
Defining Variables: Defaults?
*
•  What if I do not initialize (give an initial value to) a defined variable?
•  In C, this could be 0.0, could be anything, WILL BE GARBAGE.
–  C does not initialize variables for you!
–  It reuses memory space when defining a new variable though – and this memory space is not “flushed” before the assignment: stale contents
•  It is best to always initialize your defined variables to something appropriate
18
Casting Variables
•  Casting is the conversion from one variable type to another
–  can either increase or decrease the precision of a stored value
Example:
These are known as implicit casts – the compiler knows how to increase/decrease precision for you!
Implicit casts are generally not a good idea – anything done for you by the compiler is generally not ideal.
19
Avoid Implicit Casts
20
Explicit Casts
•  The preferred way to do this is the following:
•  Virtues: –  no warning messages
–  you are more aware of the precision of the stored variables / values
21
Variables vs. Preprocessor Definitions
•  You can define oft-­‐‑used constants (think the speed of light, etc.) using preprocessor definitions
•  Use the #define command
Comparison:
#include <stdio.h>
int main(){
double radiusEarth = 6378.1;
double pi = 3.14159;
printf(
“The circumference of the Earth
= %f\n”, 2.0*pi*radiusEarth);
return(0);
}
With preprocessor definitions, the compiler sees all instances of PI and replaces with 3.14159, etc…
22
Preprocessor Definitions vs Constants
•  The virtue of using preprocessor definitions is speed – can speed up your program at run time, since fewer vars are stored in memory, meaning fewer accesses.
•  But there are dangers:
–  universal replace might go poorly: What if you had a function called SAPInterestCalc() ?
–  other preprocessor definitions are hidden from you – hard to see if yours clobbers another say in a header file
•  Instead, one can use const :
Using const is the best coding practice – compiler won’t let it be altered!
23
FormaTed Input / Output
24
I/O Format Specifications
•  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 formaQed 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.
25
Format Mismatches
*
26
Format Mismatches
*
27
Format Mismatches
*
28
Format Mismatches
*
29
Format Mismatches
*
30
Format Mismatches
*
31
Format Mismatches
*
32
Controlling the Appearance of Output: Display
*
33
Controlling the Appearance of Output: Escape Characters
34
output v. input
•  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 this week’s lab02
35
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.
36
Download