Docsity

Problem Solving and Programming - Fortran Programming - Lecture Notes, Study notes of Computer Programming

Toggle sidebar
bg1
Problem Solving and Programming
Software Engineering Steps:
1. Specify Requirements
• What is the problems, what is required?
2. Analysis
• Identify inputs and outputs.
• Develop a list of problem variables.
• Define the relationships among variables.
3. Design the Algorithm
• Develop list of steps to solve problem.
• Use top-down design method
4. Implementation
• Convert the algorithm in a program
5. Testing and verification
• Design a strategy to test program.
• Test limit cases as well as typical.
Top-down Design
• List the major (abstract) steps
• Then break each step into sub-steps
• Continue until problem is at level which can be
programmed
Case Study Example:
Given the length in meters of a roll of wire, find
the length in feet.
We know one meter is 39.37 inches.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Problem Solving and Programming - Fortran Programming - Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!

Problem Solving and Programming

Software Engineering Steps:

  1. Specify Requirements
    • What is the problems, what is required?
  2. Analysis
    • Identify inputs and outputs.
    • Develop a list of problem variables.
    • Define the relationships among variables.
  3. Design the Algorithm
    • Develop list of steps to solve problem.
    • Use top-down design method
  4. Implementation
    • Convert the algorithm in a program
  5. Testing and verification
    • Design a strategy to test program.
    • Test limit cases as well as typical.

Top-down Design

  • List the major (abstract) steps
  • Then break each step into sub-steps
  • Continue until problem is at level which can be programmed

Case Study Example:

Given the length in meters of a roll of wire, find the length in feet.

We know one meter is 39.37 inches.

Requirements Specification Wire is purchased in rolls measured in meters, but customers need length in feet.

Analysis Program Input: wire length in meters (variable WMETER)

Problem Output : wire length in feet (variable WFEET)

Additional Program Variable : wire length in inches (variable WINCHS)

Relevant Formulas or Relations 1 meter = 39.37 inches 1 foot is 12 inches

Design

  1. Read wire length in meters.
  2. Convert length to feet.
  3. Display the wire length in feet.

Refine step 2 2.1 Convert wire length from meters to inches. 2.2 Convert wire length from inches to feet.

FORTRAN Overview

PROGRAM statement

  • Start of program, names program
  • Syntax: PROGRAM program-name

Comments

  • Any line starting in column 1 with "C" or "*".

Declarations

  • Variable declarations specify type and name.
  • Syntax: REAL variable-list INTEGER variable-list CHARACTER variable-list
  • Parameter statement specifies initial value PARAMETER ( parameter-list )
  • Where parameter-list is a list of: parameter = value

Rules of FORTRAN identifiers

  1. Must begin with a letter.
  2. Must be either uppercase letters or number

Implicit Data Typing of Identifiers:

  • If identifiers not defined, automatically defined
  • Types chosen based on first letter: I - N : INTEGER other : REAL
  • Avoid - explicit typing more readable

Identifiers cannot be keywords PROGRAM, REAL, INTEGER, PRINT, STOP

The PRINT Statement:

  • Syntax PRINT *, output-list

The READ Statement:

  • Permits reading data into variables
  • Syntax READ *, input-list

Assignment Statement:

  • Syntax result = expression

The STOP statement:

  • Terminates execution of program

The END statement:

  • Indicates program end.

FORTRAN Style Issues

  • Each FORTRAN statement must be on a separate line.
  • Each line may not begin before column 7.
  • No line may go beyond column 72.
  • Continuation of lines
    • Long lines can be continued to next line
    • Place non-blank, non-zero character in column 6 of next line, then continue statement.
  • Statement Labels
    • Must be in columns 1 through 5

Arithmetic Expressions

Operator operation Example:

  • Add SUM + TEMP
  • Subtract WEIGHT - 15.
  • Multiply LASBAR * NUM / Divide LENGTH / HEIGHT ** Exponentiation VAL1 ** 3
  • Positive +TEMP
  • Negative -VAL

Consider: A + B * C - D / (-E) (A + B + C) / D + (A - C + (-A * B) )

Expression Evaluation Order: Order Operator Association 1 Parenthesized subexpressions 2 ** Right to Left 3 * and / Left to Right 4 + and - Left to Right

Expression rules:

  1. Two arithmetic operators must never appear together. A * -B - illegal A * (-B) - legal
  2. Integer division: 15/5 → 3 13/5 → 2 1/2 → 0
  3. Mixed Type expression
    • operands converted to most complex type before evaluation
    • result is of type used for evaluation 11 / 4 → 2 (integer result) 11 / 4.0 → 2.75 (real result) 1.0 + 7 / 4 → 2.0 (division done first!)
  4. Explicit type conversion
  • REAL function converts type AVG = REAL (TOTAL) / REAL (NUMBER)
  1. Avoid the confusion of mixed type when possible
  2. Type of result
  • Type is converted (if possible) into the result variable.
  • Real to integer conversion may lose accuracy.

FUNCTION REFERENCE OPERATION

ARGUMENT TYPE

RESULT TYPE INT (arg) Converts arg to an integer by dropping the fractional part

REAL INTEGER

NINT (arg) Rounds arg to nearest integer

REAL INTEGER

REAL (arg) Converts arg to a real value

INTEGER INTEGER

ABS (arg) Finds absolute value of arg

REAL REAL

IABS (arg) Finds absolute value of arg

INTEGER INTEGER

SQRT (arg) Computes square root of arg ( arg >0.0)

REAL REAL

EXP (arg) (^) Computes earg REAL REAL ALOG (arg) Computes natural logarithm, ln( arg ) ( arg >0.0)

REAL REAL

ALOG10 (arg) Computes logarithm, log(arg) (arg>0.0)

REAL REAL

SIN (arg) Computes sine of angle arg in radians

REAL REAL

COS (arg) Computes cosine of angle arg in radians

REAL REAL

TAN (arg) Computes tangent of angle arg in radians

REAL REAL

MOD (a1,a2) Computes remainder of a1/a

INTEGER INTEGER

MAXO (a1,... ,an)

Selects largest value from list a1,... ,an

INTEGER INTEGER

AMAX1 (a1,... ,an)

Selects largest value from list a1,... ,an

REAL REAL

MINO (a1,... ,an)

Selects smallest value from list a1,... ,an

INTEGER INTEGER

AMIN1 (a1,... ,an)

Selects smallest value from list a1,... , an

REAL REAL

Case Study

Finding the value of a coin collection of nickels and pennies.

Requirements Specification Given a count of pennies and nickels, calculate the among of money.

Analysis Program Input: Count of nickels (INTEGER NICKEL) Count of pennies (INTEGER PENNIES)

Problem Output : Number of dollars, number of cents (loose change)
(INTEGER DOLLAR) (INTEGER CHANGE)

Additional Program Variable : Total value in cents (INTEGER TOTVAL)

Relevant Formulas or Relations one dollar = 100 pennies one nickel = 10 5 pennies

Design

  1. Read in the count of nickels and pennies
  2. Compute the total value
  3. Find the value on dollars and cents
  4. Display the value in dollars and cents Refine
  5. Read in the count of nickels and pennies
  6. Compute the total value 2.1 TOTVAL is 5 times NICKEL plus PENNIES
  7. Find the value on dollars and cents

Implementation:

PROGRAM COINS C Determines the value of a coin collection.

C Declarations REAL PENNYS, NICKEL, DOLLAR, CHANGE, TOTVAL

C Read in the count of nickels and pennies. PRINT *, 'Enter number of nickels' READ *, NICKEL PRINT *, 'Enter number of pennies' READ *, PENNYS

C Compute the total value in cents. TOTVAL = 5 * NICKEL + PENNYS

C Find the value in dollars and change. DOLLAR = TOTVAL / 100 CHANGE = MOD(TOTVAL, 100)

C Display the value in dollars and change. PRINT *, 'Your collection is worth ', DOLLAR, ' 1dollars' PRINT *, ' and ', CHANGE, ' cents.' STOP END

Common Programming Errors

Bug: An error in a computer program is referred to as a "bug". Removing errors from a program is called "debugging".

Syntax Errors: These are errors discovered by the compiler as it attempts to translate the program into machine language. These are caused by improper use of the FORTRAN syntax (grammar) rules.

Run-Time Errors: Run-time, or execution, errors are discovered while the program is running and attempts to perform an illegal operation (divide by zero, entering improper data, etc.).

Common Errors:

  • Misspelled FORTRAN keywords
  • Misspelled symbolic-names
  • Statements starting before column 7
  • Statements extending beyond column 72
  • Forgetting one or both apostrophes on a CHARACTER value
  • Using double quotes (") instead of apostrophes (') with CHARACTER constants
  • Omitting a comma from a list of: variables in a type statement, of expressions in a PRINT * statement, etc.
  • Omitting the * (asterisk) from a PRINT * or a READ * statement
  • An improper argument type has been used with a function, SQRT (25).