class review. basic unix commands list files in a directory: ls list files in a directory: ls remove...

Post on 16-Jan-2016

244 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Class Review

Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm <filename>remove files: rm <filename> rename files: mv <oldfilename> <newfilename> rename files: mv <oldfilename> <newfilename> copy file: cp <file 1> <file 2> copy file: cp <file 1> <file 2> examine files: cat <filename> examine files: cat <filename> make directory: mkdir <directoryname> make directory: mkdir <directoryname> remove directory: rmdir <directoryname>remove directory: rmdir <directoryname> print a file: lpr –P<printer> <filename>print a file: lpr –P<printer> <filename> query a printer: lpq –P<printer>query a printer: lpq –P<printer> remove a printer job: lprm –P<printer> jobidremove a printer job: lprm –P<printer> jobid

Compile a Fortran77 Program

All Fortran Program must have .f postfixAll Fortran Program must have .f postfix Compile a Fortran ProgramCompile a Fortran Program

f77 <filename>f77 <filename> g77 <filename>g77 <filename> f77 <filename> -o <executable f77 <filename> -o <executable

filename>filename> g77 <filename> -o <executable g77 <filename> -o <executable

filename>filename>

Basic Components of a Computer program Data structuresData structures

Identifying the right data structures are Identifying the right data structures are half the solution.half the solution.

Flow of InstructionsFlow of Instructions The operations on that data.The operations on that data.

Compiling

Source programSource program Compilation is the process of translating the Compilation is the process of translating the

program’s source code into machine code.program’s source code into machine code.

Compilation ExecutionObject

program(binary)

Input Data

Programoutput

SourceProgram

Program Compilation Program Execution

Basic Problem Solving

1.1. State the Problem ClearlyState the Problem Clearly

2.2. Describe the Input and OutputDescribe the Input and Output

3.3. Develop a Method to Solve the Problem by HandDevelop a Method to Solve the Problem by Hand

- Simple Algorithm- Simple Algorithm

4.4. Develop a Solution that is general in natureDevelop a Solution that is general in nature

- Pseudocode, Flow chart- Pseudocode, Flow chart

5.5. Test the Solution with a variety of StepsTest the Solution with a variety of Steps

Flow charts

A diagrammatical approach to problem A diagrammatical approach to problem solving.solving.

Important Symbols:Important Symbols:

ProcessProcess

DecisionDecision

Grade>90

Get a Grade

Grade>80

Grade>70

Print A

Print B

Print C

Grade>60 Print D

Print F

Y

Y

Y

Y

N

N

N

N

Flow chart of students’ grade system

Basic Fortran

Fortran is a free format languageFortran is a free format language Spaces are ignoredSpaces are ignored Fortran compiler is not case sensitiveFortran compiler is not case sensitive

HELLO, HELL O, Hello, and hello are HELLO, HELL O, Hello, and hello are the samethe same

One statement per line in Fortran77One statement per line in Fortran77

FORTRAN Statement FormatColumns 1-5Statementnumber

Column 6ContinuationCharacter

Columns 7-72FORTRAN statement

Programming Elements DataData

ConstantConstant A quantity that does not changeA quantity that does not change For Example:For Example:

• 3.14159263.1415926

• 55

• 0.060.06 VariableVariable

A representation for a quantity which is unknown or can varyA representation for a quantity which is unknown or can vary Represented by a symbolic variable nameRepresented by a symbolic variable name For Example:For Example:

• AA

• ALPHAALPHA

• X1X1 OperationsOperations

Data Type

IntegersIntegers 32, -832, -8

Real ValuesReal Values -15.45, 0.004-15.45, 0.004

Double-precision valuesDouble-precision values 3.1415, 1.0000063.1415, 1.000006

Complex valuesComplex values 1-2i, 5i1-2i, 5i

Character valueCharacter value ‘‘velocity’,’hello’velocity’,’hello’

Logical valuesLogical values .TRUE., .FALSE..TRUE., .FALSE.

Data Declaration Variable DeclarationVariable Declaration

Explicit TypingExplicit Typing With specification statementWith specification statement ExampleExample

• INTEGER INTEGER variable listvariable list• REAL REAL variable listvariable list

Implicit TypingImplicit Typing Variable names begins with I, J, K, L, M, NVariable names begins with I, J, K, L, M, N

• IntegerInteger OthersOthers

• RealReal Explicit Typing rules Implicit TypingExplicit Typing rules Implicit Typing

ExampleExample I becomes a real variable instead of an integerI becomes a real variable instead of an integer

• REAL IREAL I Fortran is a weak-typing languageFortran is a weak-typing language

Variables can be used without declaration (but this is not encouraged)Variables can be used without declaration (but this is not encouraged) Constant DeclarationConstant Declaration

PARAMETER(name1=expression, name2=expression,…)PARAMETER(name1=expression, name2=expression,…) ExampleExample

PARAMETER (PI=3.1415923)PARAMETER (PI=3.1415923)

Simple Input and Outputvery simple to write out information and read in variables. very simple to write out information and read in variables.

PRINT *, PRINT *, expression listexpression listExamples:-Examples:-PRINT*,’ENTER a value’PRINT*,’ENTER a value’PRINT*,’VALUE OF A= ’,A,’.’PRINT*,’VALUE OF A= ’,A,’.’

READ *, READ *, variable listvariable listExamples:-Examples:-

READ*,ValREAD*,ValREAD*,A,B,CREAD*,A,B,C

Assign a value to a variable FormForm

Variable name=expressionVariable name=expression ExampleExample

PI = 3.1415926PI = 3.1415926 VAR1 = VAR2VAR1 = VAR2 I = I + 1I = I + 1

Mixed-Mode Operations Arithmetic operationsArithmetic operations

Between two real values -> real valuesBetween two real values -> real values Between two integer values -> integerBetween two integer values -> integer Between a real value and an integer -> Between a real value and an integer ->

real valuereal value ExampleExample

ROOT = NUM**(1/2)ROOT = NUM**(1/2)

Truncation and Rounding TruncationTruncation

Ignore the fractional portionIgnore the fractional portion Store the number portion of the real numberStore the number portion of the real number

RoundingRounding The integer closest in value to the real numberThe integer closest in value to the real number

Assign a real number to an integerAssign a real number to an integer Truncation happenTruncation happen ExampleExample

Real AReal AInteger IInteger IA = 2.8A = 2.8I = A -> I = 2I = A -> I = 2

Underflow and Overflow

Magnitude of a real numberMagnitude of a real number Exponent in range of –38 through 38Exponent in range of –38 through 38

Exponent > 38Exponent > 38 OverflowOverflow

Exponent < -38Exponent < -38 UnderflowUnderflow

Intrinsic FunctionsSQRT (X)SQRT (X) Square Root of XSquare Root of XABS(X)ABS(X) Absolute value for XAbsolute value for XSIN (X)SIN (X) X in RadiansX in RadiansCOS (X)COS (X) X in RadiansX in RadiansTAN (X)TAN (X) X in RadiansX in RadiansEXP (X)EXP (X) e to the Xe to the XLOG (X) LOG (X) Natural Log XNatural Log XLOG 10(X)LOG 10(X) Base 10 to the XBase 10 to the XINT (X)INT (X) Truncate XTruncate XREAL (I) REAL (I) Make I a RealMake I a RealMOD(I,J) MOD(I,J) Remainder of I/JRemainder of I/J

Formatted PRINT Statements

Form:Form: PRINT format identifier, item listPRINT format identifier, item list

Format IdentifierFormat Identifier An asteriskAn asterisk A format specificationA format specification A reference to FORMAT statementA reference to FORMAT statement

Basic Form of the Format statement

n n FORMAT(FORMAT(s1, s2 , s3 ... sk)s1, s2 , s3 ... sk)

s1, s2, ... s1, s2, ... are format specificationsare format specifications

They can be character or numeric literals, or They can be character or numeric literals, or they can specify the output or input they can specify the output or input precision of character or numeric values. precision of character or numeric values.

Specifications Summary

• X – Spacing, No valuesX – Spacing, No values• Iw – Integer NumbersIw – Integer Numbers• Fw.d – F5.2 FLOATING POINT OR REAL Fw.d – F5.2 FLOATING POINT OR REAL

NUMBERSNUMBERS• Aw – Alphanumeric DataAw – Alphanumeric Data• Ew.d – Real Numbers in E NotationEw.d – Real Numbers in E Notation• T – tab specifies column to tab to. T – tab specifies column to tab to. • / – Continue in new line/ – Continue in new line• ‘ ‘ – ‘ ‘ – Literals between the quotesLiterals between the quotes

Logical Expressions

A logical expression is one that is evaluated as A logical expression is one that is evaluated as either .true. or .false. Notice the periods on either .true. or .false. Notice the periods on either side of .true. and .false.either side of .true. and .false.

Logical constantsLogical constants .TRUE..TRUE. .FALSE..FALSE.

You can declare logical variables that can take on You can declare logical variables that can take on values of .true. or .false. values of .true. or .false.

LOGICAL DONE, EASY, HARDLOGICAL DONE, EASY, HARD

Relational Operators

.EQ..EQ. Equal toEqual to

.NE..NE. Not Equal toNot Equal to

.LT. .LT. Less ThanLess Than

.LE. .LE. Less Than or Equal toLess Than or Equal to

.GT. .GT. Greater ThanGreater Than

.GE. .GE. Greater Than or Equal toGreater Than or Equal to

Note: Expressions are always read and evaluated from Note: Expressions are always read and evaluated from left to right. left to right.

A.LT.B ( A less than B )A.LT.B ( A less than B )

Logical Operators

.NOT. <Logical Expression>.NOT. <Logical Expression>

<Logical Expression> .AND. <Logical Expression><Logical Expression> .AND. <Logical Expression>

<Logical Expression> .OR. <Logical Expression><Logical Expression> .OR. <Logical Expression>

<Logical Expression> .EQV. <Logical Expression><Logical Expression> .EQV. <Logical Expression>

<Logical Expression> .NEQV. <Logical Expression><Logical Expression> .NEQV. <Logical Expression>

Note: Not valid to compare two logical variables with .EQ. or .NE.Note: Not valid to compare two logical variables with .EQ. or .NE.

AA BB .NOT. A.NOT. A A.AND.BA.AND.B A.OR.BA.OR.B A.EQV.BA.EQV.B A.NEQV.BA.NEQV.B

.FALSE..FALSE. .FALSE..FALSE. .TURE..TURE. .FALSE..FALSE. .FALSE..FALSE. .TURE..TURE. .FALSE..FALSE.

.FALSE..FALSE. .TURE..TURE. .TURE..TURE. .FALSE..FALSE. .TURE..TURE. .FALSE..FALSE. .TURE..TURE.

.TURE..TURE. .FALSE..FALSE. .FALSE..FALSE. .FALSE..FALSE. .TURE..TURE. .FALSE..FALSE. .TURE..TURE.

.TURE..TURE. .TURE..TURE. .FALSE..FALSE. .TURE..TURE. .TURE..TURE. .TURE..TURE. .FALSE..FALSE.

Relational and Arithmetic Operator Precedence

PriorityPriority OperationOperation OrderOrder

11 ParenthesesParentheses Innermost firstInnermost first

22 ExponentiationExponentiation Right to LeftRight to Left

33 * /* / Left to rightLeft to right

44 + -+ - Left to rightLeft to right

5 5 Relational operatorsRelational operators Left to rightLeft to right

66 .NOT..NOT. Left to rightLeft to right

77 .AND..AND. Left to rightLeft to right

88 .OR..OR. Left to rightLeft to right

99 .EQV., .NEQV..EQV., .NEQV. Left to rightLeft to right

Logical IF Statement

Format:Format:IF (logical expression) executable statementIF (logical expression) executable statement

If the logical expression is true, execute the statement on If the logical expression is true, execute the statement on the same line.the same line.

If the logical expression is false, jump to the next statement.If the logical expression is false, jump to the next statement.

Example:Example:IF (A.LT.0) SUM = SUM+AIF (A.LT.0) SUM = SUM+A

Block IF Statements

example: Portion of example: Portion of Zero DivideZero Divide

IF (DEN.EQ.0.0) THEN IF (DEN.EQ.0.0) THEN

PRINT *,’ZERO DIVIDE’PRINT *,’ZERO DIVIDE’

STOPSTOP

END IFEND IF

FRACTN = NUM/DENFRACTN = NUM/DEN

PRINT *,’FRACTION =’, FRACTNPRINT *,’FRACTION =’, FRACTN

IF – THEN – ELSE

IF(IF(logical-explogical-exp)THEN)THEN

statement 1 statement 1

..

..

ELSEELSE

statement nstatement n

statement n+1statement n+1

END IFEND IF

LogicalExpression

Statements

Y

N

Statements

ELSE IF StatementIF(logical-exp)THEN statement 1 . .

statement mELSE IF (logical-exp) THEN

statement m+1..statement n

ELSE IF (logical-exp) THENstatement n+1..statement p

ELSEstatement p+1..statement q

END IF

While Loops

There are no while statements in Fortran 77 although There are no while statements in Fortran 77 although many compilers have implemented one. Here is how many compilers have implemented one. Here is how you do it. you do it.

nn if (if ( logical_explogical_exp) then) then statement – 1 statement – 1 statement – 2statement – 2 .. go togo to nnend ifend if

DO Loop Structure

Format:Format:

DO DO kk index = initial, limit, incrementindex = initial, limit, increment

statement 1statement 1

……

statement nstatement n

kk CONTINUECONTINUE

Files

Files are used for permanent storageFiles are used for permanent storage

When we handle files, we open, process, then When we handle files, we open, process, then close the file.close the file.

Processing can be reading from or writing to the Processing can be reading from or writing to the file.file.

top related