1 / 320

Book: Fortran 95-2003 for Scientists and Engineers, by S. J Chapman

Book: Fortran 95-2003 for Scientists and Engineers, by S. J Chapman. Transparencies prepared by Anthony T. Chronopoulos. CS1073. CHAPTER 1. The Computer In summary, the major components of the computer are: CPU Main Memory Secondary Memory

meredith
Télécharger la présentation

Book: Fortran 95-2003 for Scientists and Engineers, by S. J Chapman

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. Book: Fortran 95-2003 for Scientists and Engineers, by S. J Chapman Transparencies prepared by Anthony T. Chronopoulos

  2. CS1073 CHAPTER 1

  3. The Computer In summary, the major components of the computer are: CPU Main Memory Secondary Memory Input Devices (e.g. keyboard, tapes etc) Output Devices (e.g. display, monitor, tapes etc). Chapter 1 Intro. to Computers and the Fortran Language

  4. The CPU The Central Processing Unit is "heart“ (or better “brain”) of a computer. The CPU consists of three main parts: The Control Unit - coordinates activities of the computer The Arithmetic Logic Unit (ALU) - performs the calculations Registers - store a small amount of data and instructions Chapter 1 Intro. to Computers and the Fortran Language

  5. Main Memory (RAM) It is larger than the Registers and smaller than the hard drive. It temporarily stores the program currently being run by the computer and also the data required by the programs. RAM is volatile, i.e. when power is interrupted, then what was stored in RAM is lost. Chapter 1 Intro. to Computers and the Fortran Language

  6. Secondary Memory It is a permanent (non-volatile) storage. The hard drive is the best example, but also USB, CDs, tapes, etc. The size of hard drives is larger than that of RAM.  However, accessing data stored on a hard drive (or other secondary memory) takes much longer (5-10 times) than from RAM. Chapter 1 Intro. to Computers and the Fortran Language

  7. Computer Languages Each computer has its own machinelanguage (Assembly) used to execute very basic operations.  Operations are: load and store (data, to and from memory), and add, subtract, multiply, or divide.  The problem with a machine language is that it is very difficult to program and use, and also it can be unique for a computer. Chapter 1 Intro. to Computers and the Fortran Language

  8. Thus, computer scientists design high-level language that are easy to program and use. The programs must be converted to a machine-language (by compilers and linkers) for the computer to run them. Examples are Fortran, C, C++, Java etc. The benefit of a high-level language (e.g. Fortran) is that a program and can be compiled on any machine that has a Fortran compiler.  Chapter 1 Intro. to Computers and the Fortran Language

  9. Data Representation in a Computer Data is represented by millions of switches, each of which can be either ON (1) or OFF (0). 0 and 1 are the two binary digits (bits). We use a combination of 0's and 1's to represent the other numbers (and characters as well).The smallest common combination of bits (0's and 1's) is called a byte.  A byte is a group of 8 bits. A word is a group of 2, 4, or 8 bytes. Our PCs have 4-byte words. Chapter 1 Intro. to Computers and the Fortran Language

  10. The Binary Number System The number system used by humans is the decimal system.  The decimal system is a base=10 system. There are 10 digits (0-9). Each digit in a number represents a power of 10. The number 221 is: 2 * 10^2+ 2 * 10^1 + 1 * 10^0 (where 10^i is 10 raised to exponent i =0,1,2,..). Chapter 1 Intro. to Computers and the Fortran Language

  11. Similarly, converting a number from binary (base 2) to decimal (base 10). The number 101: 1 * 2^2+ 0 * 2^1 + 1 * 2^0 =  4 +0+ 1 = 5 If n bits are available, then those bits can represent 2^n possible values. e.g. One byte (n=8 bits) can represent 256 possible values.  Two bytes (n=16 bits) can represent 65,536 possible values. Chapter 1 Intro. to Computers and the Fortran Language

  12. Which of the following ranges would best describe the possible values of a byte? (1) 0 ,.., 255 (2) 1 ,.., 256 (3) -127 ,.., 128 (4) -128 ,.., 127 . The answer is (4).  The reason is that the computer must store both positive and negative integers. The following example illustrates how this can be achieved. Chapter 1 Intro. to Computers and the Fortran Language

  13. Chapter 1 Intro. to Computers and the Fortran Language Example from the car odometer/milometer. (Note: content not in Book will not be used in Exams) Milometer readings during travel.

  14. Chapter 1 Intro. to Computers and the Fortran Language e.g. Storage of 8-digit integers, in base=10 system:

  15. Chapter 1 Intro. to Computers and the Fortran Language When using the binary system, the effect is that if the first binary digit (or bit) is a one then the number is negative, while if it is zero the number is positive.

  16. Two other number systems that are also useful are octal (base 8) and hexadecimal (base 16). Table 1-1 shows the conversion between these systems for the decimals: 0,1, …,15 Chapter 1 Intro. to Computers and the Fortran Language

  17. Types of Data Three common types of data are stored in a computer's memory: character, integer, real Each type has unique characteristics and takes up a different amount of memory. Chapter 1 Intro. to Computers and the Fortran Language

  18. Character Data A typical system for representing character data may include the following: Letters: A through Z and a through z Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) Miscellaneous symbols, e.g. (", ', ?, ., <, >, =, %, &) Chapter 1 Intro. to Computers and the Fortran Language

  19. In the past, it has been common to use just one byte of memory to represent a maximum of 256 characters (ASCII). To represent characters found in many languages, one can use use 2 bytes of memory which allows 65,536 possible characters (Unicode). Chapter 1 Intro. to Computers and the Fortran Language

  20. Integer Data Integer data ( e.g. -1, -355, 0, 1993) are represented exactly on computers. However, only a finite number can be stored. Most machines will use 4 bytes of  memory to represent integers.  Smallest n-bit integer: -2^(n-1) Largest n-bit integer: 2^(n-1) - 1 e.g. n=32 for 4-byte numbers. Chapter 1 Intro. to Computers and the Fortran Language

  21. With 4 bytes of memory we can represent numbers in the approx. range [-2.15*billion, 2.15*billion]. Outside this range we get arithmetic overflow. Integer data limitations: (1) No fractional parts. (2) It is not possible to represent very large positive integers or very small negative integers. Chapter 1 Intro. to Computers and the Fortran Language

  22. Real Data The real data type stores numbers in a format similar to scientific notation. For example, the speed of light in a vacuum is about 299,800,000 meters per second.  The number in scientific notation would be 2.998 * 10^8  (m/s) meters per second. Chapter 1 Intro. to Computers and the Fortran Language

  23. A format similar to scientific notation will be used to represent real numbers in FORTRAN. Real numbers occupy 4 bytes of memory.  These 4 bytes will be divided as follows: 3 byte for the fraction (or mantissa) 1 byte exponent The mantissa will be a number between -1 and 1. The exponent will contain the power of 2 required to scale the number to its actual value. Chapter 1 Intro. to Computers and the Fortran Language

  24. exponent mantissa Summary: Numbers represented on the computers Integer numbers are stored in the computer as they are (e.g. 1321 ). The real numbers are converted. Consider any non-zero real number as a fraction lying between 0.1 and 1.0, called the mantissa, which is multiplied or divided by 10 a certain number of times, where this number is called the exponent. e.g. 17877.0 (in fraction, base=10 form): 0.17877x10^5 The same technique as was used for integers to distinguish positive and negative numbers will be used for both the mantissa and the exponent.

  25. Precision and Range Precision refers to the number of significant digits that can be preserved in a number (on a computer). Based on the number of bits (or bytes) in the mantissa, 3 byte mantissa gives about 7 significant digits (between 1.0 and -1.0) e.g. 12345678.4 is stored as 12345678.0. The difference (i.e. 0.4) is called the round-off error. Chapter 1 Intro. to Computers and the Fortran Language

  26. Range is the difference between the largest and smallest numbers that can be represented. The number of bits in the exponent e.g. 1-byte exponent (with the 3-byte mantissa) allows a range of approximately 10^-38 to 10^38 (i.e. [10^-38 , 10^38] ) Chapter 1 Intro. to Computers and the Fortran Language

  27. Evolution of Fortran: Fortran I: Fig. 1-5 Fortran 77: Fig. 1-6 Fortran 90/2003: Fig 1-7 Chapter 1 Intro. to Computers and the Fortran Language

  28. Recommended Problems (not to be handed in) Page 12 , Quiz 1 (answers are on the back): 1(a),2(a),6,7 Chapter 1 Intro. to Computers and the Fortran Language

  29. CHAPTER 2 CS1073

  30. Chapter 2: Basic Elements of Fortran 2.2 The Fortran Character Set (Table 2-1) The following are valid in a Fortran 90/95 program: alpha-numeric: a-z, A-Z, 0-9, and _ (the underscore); arithmeticsymbols: +, -, *, /, ** miscellaneous symbols: e.g. , comma . decimal point < less than etc

  31. Chapter 2: Basic Elements of Fortran 2.3Structure of a FORTRAN Statement A program consists of a series of statements designed to accomplish the goal to be accomplished. There are two basic types of statements: Executable statements describe the actions taken by the program (additions, subtractions, multiplications, divisions). Non-executable statements provide information necessary for proper operation of the program.

  32. Chapter 2: Basic Elements of Fortran Rules on Fortran statements: Each line may be up to 132 characters long. A statement too long to fit in a single line may be continued on the next line by ending the current line with an & (ampersand). e. g. output = input1 + input2 ! sum the inputs output = input1 & ! Also, sum the inputs + input2

  33. Chapter 2: Basic Elements of Fortran The above two statements are equivalent. Commenting your code is very important.  To comment in FORTRAN, one uses the exclamation point (!). All comments after the ! are ignored by the compiler

  34. Chapter 2: Basic Elements of Fortran One can use labels in some statements. A label can be any number between 1 and 99999. Statement labels are less common in modern FORTRAN. 

  35. Chapter 2: Basic Elements of Fortran 2.4 Structure of a FORTRAN Program A FORTRAN program can be divided into three sections: Declarations- This section consists of a group of non-executable statements at the start of the program. Execution - This section consists of one or more statements describing the actions to be performed by the program. Termination - This section consists of a statement (or statements) telling the computer to stop/end running the program.

  36. Chapter 2: Basic Elements of Fortran The program (in Fig. 2-1) reads two numbers as input, multiplies them, and prints out the result PROGRAM my_first_program ! Purpose: ! To illustrate some of the basic features of a Fortran program. ! ! Declare the variables used in this program. INTEGER :: i, j, k ! All variables are integers ! Get two values to store in variables i and j WRITE (*,*) 'Enter the numbers to multiply: ' READ (*,*) i, j

  37. Chapter 2: Basic Elements of Fortran ! Multiply the numbers together k = i * j ! Write out the result. WRITE (*,*) 'Result = ', k ! Finish up. STOP END PROGRAM my_first_program

  38. Chapter 2: Basic Elements of Fortran Discussion of Program Above The first statement of this program begins with the word PROGRAM.  This is a non-executable statement that specifies the name of the program to the FORTRAN compiler. The name may be up to 31 characters long and be any combination of alphabetic characters, digits, and the underscore. The first character must be a letter. The PROGRAM statement must be the first line of the program.

  39. Chapter 2: Basic Elements of Fortran The Declaration Section This section begins with a comment stating that variable declarations are to follow. The declaration begins with the data type (INTEGER) followed by two colons and then the variable name. A comment follows the variable name.  Every variable must be commented as to its purpose in the program. These statements are non-executable.

  40. Chapter 2: Basic Elements of Fortran The Execution Section The first statement in this section is the WRITE statement that tells the user to enter the input. The second statement will read the input and assign the values to the corresponding variables. The third statement multiplies the two variables and the product is assigned to a third variable. The last executable statement prints the product to the screen.

  41. Chapter 2: Basic Elements of Fortran The Termination Section The STOP statement tells the computer to stop running the program.  The use of the STOP command is optional here.  The END PROGRAM statement informs the compiler that no more statements exist.

  42. Chapter 2: Basic Elements of Fortran Compiling and Executing the FORTRAN Program Before a program can be run (executed) it must be compiled into an executable program. In this process the code may also be linked to various system libraries.

  43. Chapter 2: Basic Elements of Fortran 2.5 Constants and Variables A constant is a data object that is defined before a program is executed and it does/can not change during the execution of the program. Constants are used in developing a good/correct programs in solving math problems (e.g. the circle constant PI ).

  44. Chapter 2: Basic Elements of Fortran A variable is a data object that can change value during the execution of a program. Referring to the sample program above: The data objects i,j,k are variables. Each variable (or constant) must have a unique name inside the program.

  45. Chapter 2: Basic Elements of Fortran The names may contain up to 31 characters and any combination of alphabetic characters, digits, and the underscore. The first character must always be alphabetic. The name we assign a variable (or constant) should be meaningful in terms of the purpose that it is used to solve the problem (e.g. time, distance, grade etc).

  46. Chapter 2: Basic Elements of Fortran 2.5.1 - 2.5.3 Types of Data There are five intrinsic (or "built-in") types of data: Integer Real Character There are two other intrinsic types that will be introduced later (Logical, Complex).

  47. Chapter 2: Basic Elements of Fortran Integers Integers are numbers without a decimal point. e.g. -999 , +17, 1234 (not allowed: 1,234 , +17. ) No commas may be embedded within an integer constant. If the number is positive, the sign + is optional, but the - is required to for negative. Integer variables contain the value of an integer data type. There is a maximum and a minimum value that an integer can take.  The range is determined by how much memory is (in terms of no. of bytes) given to a variable of the integer type.

  48. Chapter 2: Basic Elements of Fortran Real Numbers Real (or floating-point) numbers represent values with a fraction. Real constants can be written with or without an exponent. e.g. 10. , -999.9, 1.0E-3 (i.e. 0.001 or .001 ) Not allowed: 1,000. , 111E3, -12.0E1.5, 1.0x 10^-3

  49. Chapter 2: Basic Elements of Fortran The mantissa should contain a decimal point.  A real variable is a variable that contains the value of a real data type. There is a maximum and a minimum value that a real var/constant can take.  The range is determined by how much memory is (in terms of no. of bytes) given to a variable of the real type.

  50. Chapter 2: Basic Elements of Fortran Characters A character constant is a string of characters enclosed in a single or double quotes. Any characters representable on a computer (not just the Fortran characters) are legal in a character context (i.e. if enclosed in quotes). e.g. ‘this is’, ‘ ‘, ‘[^]’, “3.1345”

More Related