




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These are class notes for the course phys-4007/5007: computational physics i taught by dr. Donald luttermoser at east tennessee state university, including code examples and explanations in c programming language.
Typology: Study notes
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































Dr. Donald G. Luttermoser East Tennessee State University
Version 4.
Abstract
These class notes are designed for use of the instructor and students of the course PHYS-4007/5007: Computational Physics I taught by Dr. Donald Luttermoser at East Tennessee State University.
Appendix B–2 PHYS-4007/5007: Computational Physics
e) /* ... */ is a comment and is ignored by the compiler; the C standard says that comments cannot be nested. Also, comments must be terminated explicitly, i.e., new- line does not terminate them — this is a common source of compiler errors, and, for the unwary, can be very dif- ficult to trace. For your own sanity the header comment should include:
i) Name of the program — to correspond to the file- name.
ii) Authors name — even if copied!
iii) Date.
iv) Brief indication of function and purpose of the program, e.g., assignment number, or project, and what it does.
f) Program layout is very important; I suggest two spaces indentation for each block; I suggest that you avoid tabs
g) The #include <stdio.h> is a directive to the C preproces- sor to include the contents of a file called stdio.h; stdio.h contrains declarations of STanDard Input-Output func- tions; include means that the full contents of the file stdio.h are inserted where the #include directive appears; only then is the program passed to the C-compiler proper. #include is C’s way of importing.
h) In the definition of a function, () encloses the argument list; in this case main expects no arguments; (void) explic- itly denotes has no arguments.
Donald G. Luttermoser, ETSU Appendix B–
i) { ... } enclose the statements in a function.
j) printf is a library function for printing output on the users screen — in this case the string of characters between quotes; \n is C notation for newline; printf will not insert a newline by itself.
k) \n represents a single character — neccessary, since typing the
l) return 0; in C, programs can return values to the operating system; in UNIX, ’0’ signifies that the program terminated normally; other values indicate error conditions. This is useful if you intend putting your program in a UNIX shell script; likewise DOS ’.bat’ files.
B. Variables and Arithmetic.
TC =
Then, print a list as follows: 0 - 20 - 40 4 60 15 300 148 /------------------------------------------------ ftoc.c - Fahr. to Celsius table j.g.c. 3/10/ Copied from K&R p. --------------------------------------------------/ #include <stdio.h> int main(void)
Donald G. Luttermoser, ETSU Appendix B–
i) Condition is tested.
ii) If condition is true — body is executed.
iii) Go back to 1.
iv) However, if condition is false — execution con- tinues at the statement following the loop.
a) Has multiple arguments; the first is always a string con- stant — then, this may contain codes (e.g., ’%d’) to say how subsequent arguments are to be printed.
b) %d: print the second argument as a decimal integer.
c) printf is not part of the C language — but it’s in the standard library, which is available with all C compilers.
d) Field widths can be specified, e.g., %6d.
e) Uses right justification, unless specified otherwise.
Appendix B–6 PHYS-4007/5007: Computational Physics
lower = 0; /* lower limit of table - fahr./ upper = 300; /upper limit/ step = 20; /step size*/ fahr = lower; while(fahr <= upper){ celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; }
b) f conversion specification in printf: i) %3.0f: 3 characters wide, no decimal point, no fraction digits.
Appendix B–8 PHYS-4007/5007: Computational Physics
printf("%d %6.1f\n",fahr,(5.0/9.0)*(fahr-32)); } return 0; }
b) for statement and loop: There are three statements con- tained within the ’(.)’ in a for statement: i) fahr = 0: initialization.
ii) fahr <= 300: loop continuation control; evaluate the condition — if true execute the body — other- wise jump out.
iii) fahr = fahr + 20: increment; do this AFTER the first and subsequent loops — BEFORE evaluation of control condition.
iv) The for(...) body may be a single statement or a block { ... }.
D. Symbolic Constants and the Preprocessor.
Donald G. Luttermoser, ETSU Appendix B–
#define LOWER 0 /lower limit of table/ #define UPPER 300 #define STEP 20 int main(void) { int fahr; float celsius; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP){ celsius = (5.0/9.0) * (fahr-32); printf("%3d %6.1f\n", fahr, celsius); } return 0; }
b) Adopt a convention to use upper-case for symbolic con- stants; be very careful what else you use upper case for.
Donald G. Luttermoser, ETSU Appendix B–
echoes it to the screen / terminal; immediately means BE- FORE it is presented to the reading program (see buffered below). (Incidentally, this means that the terminal, it- self, does not display the typed character — just what is echoed from the host computer.)
c) Buffered input. While the user is typing, the com- puter stores all the typed characters in a buffer (array) and presents the array (line) of characters to the reading program ONLY AFTER A CARRIAGE RETURN (En- ter) has been typed.
b) Incidentally, putc(c, stdprn) is a handy way of writing to a PC printer, as is putc(c, stdaux) — to the auxiliary (com- munications) port, where stdprn is the standard printer device, and stdaux is the standard auxiliary device. BUT THESE ARE NOT STANDARD C / UNIX.
c) So, when we talk about file I-O below, we include ter- minal I/O. All the programs below can be tested using the terminal. (Note:
Appendix B–12 PHYS-4007/5007: Computational Physics
d) If you want to test the programs using files, you can use input-output redirection. i) First, you must get the compiler to compile to an .exe file, say cio.exe.
ii) Second, create a text file, say test.dat. Then, en- ter at the Unix prompt cio < test.dat which tells cio to read from test.dat =⇒ the < redi- rects the program to read from the file instead of the keyboard.
iii) If you want to send the output to a file, say testout.dat, use cio < test.dat > testout.dat.
Appendix B–14 PHYS-4007/5007: Computational Physics
/------------------------------------------------ ctch1.c - counts input chars; version 1 j.g.c. 3/10/89. copied from K&R p.18. ---------------------------------------------------/ #include <stdio.h> int main(void) { long nc; nc=0; while(getchar() != EOF) ++nc; printf("%ld\n", nc); return 0; }
Dissection: a) ++ operator: increment by one; – –: decrement by one. Note: As single statements, ++nc and nc++ are equiva- lent, but give different values in expressions, e.g., int i, n; n = 6; i = ++n; /* POST-increment gives i == 7, and n == 7; the increment is done BEFORE the assignment / wheras, n = 6; i = n++; / POST-increment gives i == 6, and n == 7 */
b) long (integer) is at least 32 bits long.
c) %ld tells printf to expect a long.
Donald G. Luttermoser, ETSU Appendix B–
b) == means is-equal to. CAUTION: = in place of == can be syntactically correct and so not trapped by compiler — a nasty hard-to-find error results!
c) Character constants, e.g., ’\n’ represents an integer value equal to the value of the character in the machine’s charac- ter set; In ASCII: ’\n’ == 10 decimal; ’A’ == 65 decimal.
Donald G. Luttermoser, ETSU Appendix B–
b) The for( ... ) loops must go 0, 1, ... 9, since, in C, array subscripts must start at 0.
c) The test if( c>=’0’ && ... : && denotes logical AND.
d) c–’0’ assumes that ’0’,’1’...’9’ have consecutive, increasing values.
e) c–’0’ is an integer expression =⇒ it’s OK for a subscript.
Appendix B–18 PHYS-4007/5007: Computational Physics
f) if( condition1 ) statement else if( condition2 ) statement ........ else statement This is the model for a multiway decision; you can have any number of else if ( cond ) stmt groups between first if and final else; the final else catches anything that didn’t satisfy any of the previous condi- tions.
g) Indenting style: Again, please note that we don’t want to run off the right-hand edge of the paper.
F. Functions.