1 / 44

Week 2

Week 2. Organizational matters Fortran 90 (subset F): Basics Example programs in detail. Top-down programming. 4 basic steps Specify the problem clearly Analyse the problem and break it down into its fundamental elements Code the program according to the plan developed at step 2

jacob
Download Presentation

Week 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 2 • Organizational matters • Fortran 90 (subset F): Basics • Example programs in detail

  2. Top-down programming • 4 basic steps • Specify the problem clearly • Analyse the problem and break it down into its fundamental elements • Code the program according to the plan developed at step 2 • Test the program exhaustively, and repeat steps 2 and 3 as necessary until the program works in all situations that you can envisage

  3. Program = Data Types + Algorithms • Data types: what you work on • Algorithms: what you do with them

  4. Structure of a program(in Fortran 90) • Heading (program, module, etc.) • specification part • execution part • subprogram part • end program statement

  5. Data Types • Five basic types: • integer • real • complex • character • logical • Data types of ‘container’ classes

  6. Integers • a whole number (positive, negative or zero) • no decimal point • Examples0123-23456+123789

  7. Reals • Numbers with decimal fractions • There has to be decimal point • Examples1.23456-0.0019875678. • Another representation:1.952e30.1952e4123.456e-8

  8. Character • Sequence of symbols from the Fortran character set • Enclosed between double quotes • Examples"This is a string""I do, I don't""1234abc345"

  9. Logical • Can take only two values: • .TRUE. • .FALSE.

  10. Identifiers • Names used to identify programs, constants, variables, etc. • Identifiers must Begin with a letter • This can be followed by up to 30 letters, digits, undescores • Be careful with the case: lower or upper case letters

  11. Identifiers • ExamplesCurrentDecay_Ratepressurean_identifier_with_a_long_namethe_best_program

  12. Constants • 10293845is an integer constant • 12.3456 is a real constant • "Whataniceday!" is a character constant

  13. Variables • Variables are value containers • Compiler associates with a variable a memory location • Value of a variable at any time is the value stored in the associated memory location at that time

  14. Variables Payment 123.56 23456 Hello World 1234567 MEMORY Message

  15. Declarations of Variables • Form:type-specifier :: list • Declares that the identifiers in the list have the specified type • Type statements must appear in the specificationpart of the program • Examplesinteger :: number_years, counts, monthsreal :: Mass, Velocity, Accelerationcharacter (len=12) :: MyName, YourName

  16. implicit none • It should be placed at the beginning of the specification part • You have to declare all variables you will be using in the program!

  17. Variable initialization • All variables are initially undefined • Initialization in the declarationsExamples:real :: W=1.2, z=5.678, mass=4.56integer :: year=1998, count=0

  18. Named constants • Form:type-specifier, parameter :: list • Examplesinteger, parameter :: INITCOUNT = 30real, parameter :: G = 9.81 • It's a good idea to write named constants in upper case

  19. Arithmetic operations • Variables and constants can be processed by using operations and functions appropriated to their types. • Operations

  20. Operations • ExamplesTo calculate B2 - 4ACB**2 - 4*A*C • Types are important:9/4 = 29.0/4.0 = 2.25 • Mixed-mode expressions:3 + 8.0/5 à 3 + 8.0/5.0 à 3 + 1.6 à 3.0 + 1.6 à 4.6

  21. Priority rules • All exponentiations are performed first; consecutive exponentiations are performed from right to left • All multiplications and divisions are performed next; in the order in which they appear from left to right • Additions and subtractions are performed last, in the order in which they appear from left to right

  22. Some examples • 2 ** 3 ** 2 = 512 • 10/5 *2 = 2 * 2 = 4 • To calculate 51/3 5.0**(1.0/3.0) but not 5.0**(1/3) à 5.0**0 à 1.0

  23. Library functions • abs(x) Absolute value of x • cos(x) Cosine of x radians • exp(x) Exponential function • int(x) Integer part of x • sqrt(x) Square root of x

  24. Assignment statement • Form:variable = expression • Assigns the value of expression to variable • Assignment is not a statement of algebraic equality; it is a replacement statement • ExamplesDensity = 2000.0Volume = 3.2Mass = Density*VolumeWeightRatio = log(Mass/90.)

  25. Programs need to communicate with users! • Two kinds of I/O (for the moment!): • Formatted I/O • List-directed I/O • List-directed outputprint *, output-listwrite (unit=*, fmt=*) output-list • List-directed inputread *, input-listread (unit=*, fmt=*) input-list

  26. List-directed I/O • Examplesprint *, "Tell me your birthday"write (unit=*, fmt=*) a, b, c**2read *, day, month, year

More Related