34e2010d97187006f85ada0156d2a413.ppt
- Количество слайдов: 33
Class Review
Basic Unix Commands n n n n n list files in a directory: ls remove files: rm <filename> rename files: mv <oldfilename> <newfilename> copy file: cp <file 1> <file 2> examine files: cat <filename> make directory: mkdir <directoryname> remove directory: rmdir <directoryname> print a file: lpr –P<printer> <filename> query a printer: lpq –P<printer> remove a printer job: lprm –P<printer> jobid
Compile a Fortran 77 Program All Fortran Program must have. f postfix n Compile a Fortran Program u f 77 <filename> u g 77 <filename> u f 77 <filename> -o <executable filename> u g 77 <filename> -o <executable filename> n
Basic Components of a Computer program Data structures u Identifying the right data structures are half the solution. n Flow of Instructions u The operations on that data. n
Compiling Source program n Compilation is the process of translating the program’s source code into machine code. n
Basic Problem Solving 1. 2. 3. 4. 5. State the Problem Clearly Describe the Input and Output Develop a Method to Solve the Problem by Hand - Simple Algorithm Develop a Solution that is general in nature - Pseudocode, Flow chart Test the Solution with a variety of Steps
Flow charts A diagrammatical approach to problem solving. n Important Symbols: n n Process n Decision
Flow chart of students’ grade system
Basic Fortran n Fortran is a free format language Spaces are ignored u Fortran compiler is not case sensitive t HELLO, HELL O, Hello, and hello are the same u n One statement per line in Fortran 77
FORTRAN Statement Format
Programming Elements n n Data u Constant t A quantity that does not change t For Example: • 3. 1415926 • 5 • 0. 06 u Variable t A representation for a quantity which is unknown or can vary t Represented by a symbolic variable name t For Example: • ALPHA • X 1 Operations
Data Type Integers 32, -8 Real Values -15. 45, 0. 004 Double-precision values 3. 1415, 1. 000006 Complex values 1 -2 i, 5 i Character value ‘velocity’, ’hello’ Logical values . TRUE. , . FALSE.
Data Declaration n Variable Declaration u Explicit Typing t With specification statement t Example • INTEGER variable list • REAL variable list u Implicit Typing t Variable names begins with I, J, K, L, M, N • Integer t Others • Real u Explicit Typing rules Implicit Typing t Example t I becomes a real variable instead of an integer • REAL I Fortran is a weak-typing language u Variables can be used without declaration (but this is not encouraged) Constant Declaration u PARAMETER(name 1=expression, name 2=expression, …) u Example t PARAMETER (PI=3. 1415923)
Simple Input and Output very simple to write out information and read in variables. PRINT *, expression list Examples: PRINT*, ’ENTER a value’ PRINT*, ’VALUE OF A= ’, A, ’. ’ READ *, variable list Examples: READ*, Val READ*, A, B, C
Assign a value to a variable Form u Variable name=expression n Example u PI = 3. 1415926 u VAR 1 = VAR 2 u. I = I + 1 n
Mixed-Mode Operations n Arithmetic operations u Between two real values -> real values u Between two integer values -> integer u Between a real value and an integer -> real value u Example t ROOT = NUM**(1/2)
Truncation and Rounding n n n Truncation u Ignore the fractional portion u Store the number portion of the real number Rounding u The integer closest in value to the real number Assign a real number to an integer u Truncation happen u Example t Real A t Integer I t A = 2. 8 t I = A -> I = 2
Underflow and Overflow Magnitude of a real number u Exponent in range of – 38 through 38 n Exponent > 38 u Overflow n Exponent < -38 u Underflow n
Intrinsic Functions SQRT (X) ABS(X) SIN (X) COS (X) TAN (X) EXP (X) LOG 10(X) INT (X) REAL (I) MOD(I, J) Square Root of X Absolute value for X X in Radians e to the X Natural Log X Base 10 to the X Truncate X Make I a Real Remainder of I/J
Formatted PRINT Statements Form: u PRINT format identifier, item list n Format Identifier u An asterisk u A format specification u A reference to FORMAT statement n
Basic Form of the Format statement n FORMAT(s 1, s 2 , s 3. . . sk) s 1, s 2, . . . are format specifications They can be character or numeric literals, or they can specify the output or input precision of character or numeric values.
Specifications Summary • X – Spacing, No values • Iw – Integer Numbers • Fw. d – F 5. 2 FLOATING POINT OR REAL • • • NUMBERS Aw – Alphanumeric Data Ew. d – Real Numbers in E Notation T – tab specifies column to tab to. / – Continue in new line ‘ ‘ – Literals between the quotes
Logical Expressions n n n A logical expression is one that is evaluated as either. true. or. false. Notice the periods on either side of. true. and. false. Logical constants u. TRUE. u. FALSE. You can declare logical variables that can take on values of. true. or. false. LOGICAL DONE, EASY, HARD
Relational Operators. EQ. Equal to . NE. . LT. . LE. . GT. . GE. Not Equal to Less Than or Equal to Greater Than or Equal to Note: Expressions are always read and evaluated from left to right. A. LT. B ( A less than B )
Logical Operators. NOT. <Logical Expression>. AND. <Logical Expression>. OR. <Logical Expression>. EQV. <Logical Expression>. NEQV. <Logical Expression> Note: Not valid to compare two logical variables with. EQ. or. NE. A B . NOT. A A. AND. B A. OR. B A. EQV. B A. NEQV. B . FALSE. . TURE. . FALSE.
Relational and Arithmetic Operator Precedence Priority Operation Order 1 2 3 4 5 6 7 8 9 Parentheses Exponentiation */ +Relational operators. NOT. . AND. . OR. . EQV. , . NEQV. Innermost first Right to Left to right Left to right
Logical IF Statement Format: IF (logical expression) executable statement If the logical expression is true, execute the statement on the same line. If the logical expression is false, jump to the next statement. Example: IF (A. LT. 0) SUM = SUM+A
Block IF Statements example: Portion of Zero Divide IF (DEN. EQ. 0. 0) THEN PRINT *, ’ZERO DIVIDE’ STOP END IF FRACTN = NUM/DEN PRINT *, ’FRACTION =’, FRACTN
IF – THEN – ELSE IF(logical-exp)THEN statement 1. . ELSE statement n+1 END IF
ELSE IF Statement IF(logical-exp)THEN statement 1. . statement m ELSE IF (logical-exp) THEN statement m+1. . statement n ELSE IF (logical-exp) THEN statement n+1. . statement p ELSE statement p+1. . statement q END IF
While Loops n There are no while statements in Fortran 77 although many compilers have implemented one. Here is how you do it. n if ( logical_exp) then statement – 1 statement – 2. go to n end if
DO Loop Structure Format: DO k index = initial, limit, increment statement 1 … statement n k CONTINUE
Files are used for permanent storage When we handle files, we open, process, then close the file. Processing can be reading from or writing to the file.
34e2010d97187006f85ada0156d2a413.ppt