330 likes | 474 Views
Class Review. Basic Unix Commands. 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>
E N D
Basic Unix Commands 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 Fortran77 Program • All Fortran Program must have .f postfix • Compile a Fortran Program • f77 <filename> • g77 <filename> • f77 <filename> -o <executable filename> • g77 <filename> -o <executable filename>
Basic Components of a Computer program • Data structures • Identifying the right data structures are half the solution. • Flow of Instructions • The operations on that data.
Compiling • Source program • Compilation is the process of translating the program’s source code into machine code.
Basic Problem Solving 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. • Important Symbols: • Process • Decision
Basic Fortran • Fortran is a free format language • Spaces are ignored • Fortran compiler is not case sensitive • HELLO, HELL O, Hello, and hello are the same • One statement per line in Fortran77
Programming Elements • Data • Constant • A quantity that does not change • For Example: • 3.1415926 • 5 • 0.06 • Variable • A representation for a quantity which is unknown or can vary • Represented by a symbolic variable name • For Example: • A • ALPHA • X1 • Operations
Data Declaration • Variable Declaration • Explicit Typing • With specification statement • Example • INTEGER variable list • REAL variable list • Implicit Typing • Variable names begins with I, J, K, L, M, N • Integer • Others • Real • Explicit Typing rules Implicit Typing • Example • I becomes a real variable instead of an integer • REAL I • Fortran is a weak-typing language • Variables can be used without declaration (but this is not encouraged) • Constant Declaration • PARAMETER(name1=expression, name2=expression,…) • Example • 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 • Variable name=expression • Example • PI = 3.1415926 • VAR1 = VAR2 • I = I + 1
Mixed-Mode Operations • Arithmetic operations • Between two real values -> real values • Between two integer values -> integer • Between a real value and an integer -> real value • Example • ROOT = NUM**(1/2)
Truncation and Rounding • Truncation • Ignore the fractional portion • Store the number portion of the real number • Rounding • The integer closest in value to the real number • Assign a real number to an integer • Truncation happen • Example • Real A • Integer I • A = 2.8 • I = A -> I = 2
Underflow and Overflow • Magnitude of a real number • Exponent in range of –38 through 38 • Exponent > 38 • Overflow • Exponent < -38 • Underflow
Intrinsic Functions SQRT (X) Square Root of X ABS(X) Absolute value for X SIN (X) X in Radians COS (X) X in Radians TAN (X) X in Radians EXP (X) e to the X LOG (X) Natural Log X LOG 10(X) Base 10 to the X INT (X) Truncate X REAL (I) Make I a Real MOD(I,J) Remainder of I/J
Formatted PRINT Statements • Form: • PRINT format identifier, item list • Format Identifier • An asterisk • A format specification • A reference to FORMAT statement
Basic Form of the Format statement n FORMAT(s1, s2 , s3 ... sk) s1, s2, ... 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 – F5.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 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 .TRUE. .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. Not Equal to .LT. Less Than .LE. Less Than or Equal to .GT. Greater Than .GE. 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> <Logical Expression> .AND. <Logical Expression> <Logical Expression> .OR. <Logical Expression> <Logical Expression> .EQV. <Logical Expression> <Logical Expression> .NEQV. <Logical Expression> Note: Not valid to compare two logical variables with .EQ. or .NE.
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 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 There are no while statements in Fortran 77 although many compilers have implemented one. Here is how you do it. nif (logical_exp) then statement – 1 statement – 2 . go ton end if
DO Loop Structure Format: DO k index = initial, limit, increment statement 1 … statement n k CONTINUE
Files 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.