subject : (1) introduction to computers

18
Subject : (1) Introduction to Computers (2) FORTRAN 90 Programming Language Theory: Two Hours per week Practical: Two Hours per week Syllabus Items 1. Computer Organization 2. Generations of Computers 3. Classification of Programming Languages 4. Algorithms and Flowcharts 5. Basic Concepts of FORTRAN 90 6. Data Types, Declaration, and Parameterization 7. Input and Output Statements 8. Operators in FORTRAN 90 9. Conditional (Selection) Statements: 10. Repetitive Structures (Loops) 11. Formatted Output in FORTRAN 90 12. Procedures (Sub-programs) 13. Arrays and Matrices

Upload: others

Post on 12-Dec-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Subject : (1) Introduction to Computers

(2) FORTRAN 90 Programming Language

Theory: Two Hours per week

Practical: Two Hours per week

Syllabus Items

1. Computer Organization 2. Generations of Computers 3. Classification of Programming Languages 4. Algorithms and Flowcharts

5. Basic Concepts of FORTRAN 90 6. Data Types, Declaration, and Parameterization 7. Input and Output Statements 8. Operators in FORTRAN 90

9. Conditional (Selection) Statements: 10. Repetitive Structures (Loops) 11. Formatted Output in FORTRAN 90

12. Procedures (Sub-programs) 13. Arrays and Matrices

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

i

Computer Organization

A computer is mainly divided into two main parts: Hardware and Software.

Hardware : the hardware may be divided into four main units or sections as illustrated below (See Figure-1) :

1. Input Units receive information (data) from various input devices either to be stored or to be processed. Most information is entered into computers today through keyboard, mouse, scanner, camera, microphone …etc.

2. Output Units take information that has been processed by the computer and place it on various output devices such as monitor, printer, speakers…etc.

3. Memory Unit keep information that has been entered through the input units and the information that has to be sent to the output units. It is also divided into:

Primary (main) Memory: low-capacity, high speed, and high cost. It includes: RAM (Random Access Memory) for temporary storage. ROM (Read Only Memory) for permanent storage.

Secondary Storage Unit: it has higher capacity than primary memory but lower speed and cost. It is used for storing data and applications for a long time, such as Hard Disk, Compact Disk (CD), Floppy Disk.

4. Central Processing Unit (CPU): The central processing unit (or chip) is the "brain" of the computer. It is the essential component of a computer because it is the part that executes the programs and controls the operation of all the hardware. It consist of two parts:

Arithmetic and Logic Unit (ALU): It is responsible for performing arithmetic calculations such as addition, subtraction, multiplication and division, as well as logical operations such as AND, OR, NOT, and XOR.

Control Unit (CU): The CU tells the input unit when information should be read into the memory unit, tells the ALU when information from the memory unit should be used in calculations and tells the output unit when to send information from the memory unit to certain output devices.

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

ii

( Figure-1 )

Software: software is known as programs, consists of all instructions that tell the hardware how to perform a task. Computer software is divided into three major groups (See Figure-2) :

1. System Software: is responsible for running computer hardware. System software includes :

Operating Systems (MS-DOS, MS-Windows, UNIX, …etc.) Device Drivers.

2. Programming Software (Languages): assists a programmer in writing computer programs, such as (Machine Language, Assembly Language, BASIC, FORTRAN, BASCAL, C, and C++).

3. Applications Software: Allows users to perform one or more tasks such as Microsoft Office and games.

Input Unit Output Unit Arithmetic and Logic

Unit (ALU)

Control Unit (CU)

Central Processing Unit (CPU)

Main Memory

Secondary Storage devices

Memory Unit

Software

System Software

MS-DOS

MS-Windows

UNIX

Programming Languages

BASIC, FORTRAN BASCAL, C & C++

Applications

MS Office

Games

( Figure-2 )

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

iii

Generations of Computers

1. First Generation (1940 to 1956) (Vacuum Tubes):

The first generation of computers used vacuum tubes. Magnetic drums are used as memory, punched cards and paper tapes as input devices.

The instructions were written in machine language. The first generation computers could solve one problem at a time. The computation time was in milliseconds (low speed). These computers had a large size and require a large room for

installation.

2. Second Generation (1956 to 1963)( Transistors):

Transistors were used in place of the vacuum tubes. Transistors allowed computers to become smaller, faster, cheaper, energy efficient and reliable.

The instructions were written using the assembly language. The computation time was in microseconds (higher speed than 1st generation) . Transistors are smaller in size compared to vacuum tubes, thus, the size of the computer was also reduced.

3. Third Generation (1964 to 1971) (Integrated Circuits IC):

The third generation computers used the Integrated Circuits (IC). In an IC chip, multiple transistors are placed on a silicon chip.

High-level languages were used for programming, instead of machine language and assembly language.

The computation time was in nanoseconds. The size of these computers was quite small compared to the second generation computers.

The third generation computers used less power and generated less heat than the second generation computers. The cost of the computer reduced significantly.

vacuum tube

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

iv

4. Fourth Generation (1971 to 1991) (Microprocessors):

This generation uses the microprocessors. Microprocessor is a chip containing millions of transistors and components. The Intel 4004 chip was the first microprocessor. The components of the computer like Central Processing Unit (CPU) and memory were located on a single chip. In 1981, IBM introduced the first computer for home use. In 1984, Apple introduced the Macintosh.

Semiconductor memory replaced the magnetic core memory, resulting in fast random access to memory. Secondary storage device like magnetic disks became smaller in size and larger in capacity.

Several new operating systems like the MS-DOS and MS-Windows developed during this time.

This generation of computers supported Graphical User Interface (GUI) that allows user to interact with the computer via menus and icons.

High-level programming languages were used for the writing of programs.

The computation time is in picoseconds.

They are smaller than the computers of the previous generation.

5. Fifth Generation (Artificial Intelligence 1992 to present):

The fifth generation computers use Super Large Scale Integrated (SLSI) chips that are able to store millions of components on a single chip. These computers have large memory requirements.

This generation of computers uses parallel processing that allows several instructions to be executed in parallel, instead of serial execution. Parallel processing results in faster processing speed. The Intel dual-core microprocessor uses parallel processing.

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

v

Classification of Programming Languages

A "programming language" is a language designed to describe a set of actions to be executed by a computer. A "programming language" is a practical way for us (humans) to give instructions to a computer. Programming languages can be classified into ( See Figure-3):

1. Machine Language (Low-level language): uses 0's and 1's for coding the instructions. It is used in 1st generation computers.

2. Assembly Language: is an English-like terms representing binary code. It uses mnemonics like ADD for addition and SUB for subtraction …etc. It is easier to write instructions in assembly language, as compared to writing instructions in machine language. It is used in 2nd generation computers.

3. High-level language: High-level language has a major advantage over machine and assembly languages that it is easy to learn and use because it is closer to human language.

Machine Lamguage

# Binary Code (0's and 1's)

Assembly Language

# English-like terms representing binary

code

High-level Language

# closer to human language

( Figure-3 )

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

vi

Algorithms and Flowcharts

An algorithm is a procedure consisting of a set of rules (instructions) provides the solution to a problem. In other word, an algorithm is a step-by-step procedure to solve a given problem. A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows , each shape represents a step in the process.

Flowchart Symbols There are 6 basic symbols commonly used in flowcharting programs:

Symbol Name Function

Start / End Indicates the starting or ending of the program, process, or interrupt program.

Input / Output

Used for any Input/Output (I/O) operation. Indicates that the computer is to receive data or output results.

Process Indicates any type of internal operation inside the Processor or Memory

Decision

Used to ask a question that can be answered in a binary format (Yes/No, True/False)

Connector

Allows the flowchart to be drawn without intersecting lines or without a reverse flow.

Flow Lines (Arrows)

Shows direction of flow.

Decision (if … else)

Repetition (loop)

A>B?

Max = B Max = A

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

vii

V=(3/4)*PI*r3

START

Input r

PI=3.14

A=4*PI*r2

Print V, A

END

Ex: Draw a flowchart that reads the radius of a sphere "r", then it calculates its volume "V" and surface area "A" using formulas:

.

Ex: 6Ω, 3Ω resisters are connected in series across a 36v source, draw a flowchart to find the total current and the voltage of each resister.

Ex: Draw a flowchart to find the value of

It = Vs / Rt

START

Input R1, R2, Vs

Rt = R1 + R2

V1= It * R1

Print Rt, It, V1, V2

END

V2= It * R2

Input x

Start

End

y = x2 + 2

Print y

x ≥ 0 ? y = x + 2

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

viii

Ex: Draw a flowchart to find the sum of the first 25 odd natural numbers: Sum = 1 + 3+ 5 + ……

Ex: Draw a flowchart to find the sum of first 100 natural numbers: Sum = 1 + 2 + 3 + ……+ 99 + 100.

Ex: Draw a flowchart to find the largest of three numbers A, B, and C:

End

Print sum

i = 0

Start

i =25 ?

Sum = 0

sum = sum + j

i = i + 1

j = j + 2

i = 0

Start

Sum = 0

i = i + 1

End

Print sum

i =100 ?

sum = sum + i

End

Print A

Input A, B, C

Start

A ≥ B ? B ≥ C ? A ≥ C ?

Print B Print C

j = 1

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

ix

Ex: Draw a flowchart to solve an equation of second order given by: a x2 + b x + c = 0

END

X1 = (–b + sqrt(d)) / (2 * a) X2 = (–b – sqrt(d)) / (2 * a)

Print X1, X2

Input a, b, c

d = (b*b – 4 *a *c)

START

If d < 0

Print "complex roots"

If d > 0

X1 = –b / (2 * a) X2 = –b / (2 * a)

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

x

Ex: draw a flowchart to find the average of 10 numbers.

Ex: Draw a flowchart to read a number “x” then calculate y using the formula: (100 terms):

END

Print Av

Input xi

i = 0

Start

i =10 ?

Sum = 0

sum = sum + xi

i = i + 1

Av = sum / 10

END

Print y

y = y + ( k * xj / j)

Input x

y = 0

Start

i =100 ?

i = 0 j = 1

i = i+1

j = j+2 k = -1*k

k = 1

INTRODUCTION TO COMPUTERS ABDUL-MUTTALIB. A. H. AL-DOURI

xi

Ex: Draw a flowchart for computing the factorial of a positive integer N.

H.W. 1:

Draw a flowchart that reads a temperature in Fahrenheit degrees and convert it into Celsius degrees, using the formula

( )

H.W. 2:

Draw a flowchart that finds the value of y from the following series

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

1

1. Basic Concepts of FORTRAN 90

1.1 A Brief History

FORTRAN from its development was intended for translating scientific equations into computer code. It's name is derived from FORmula TRANslation. FORTRAN was developed between 1954 and 1957 by IBM, it was the first programming language that converted an algorithm into machine language that the computer could read and execute.

FORTRAN went through many revisions over the years. FORTRAN II came out in 1958 and FORTRAN IV in 1962. FORTRAN IV was renamed FORTRAN 66 when it became an ANSI standard in 1966. The next major update came in 1977 with FORTRAN 77. This is the version that has been used for the last 20 years. FORTRAN 90 was developed in 1991.

1.2 Structure of a FORTRAN Program

A FORTRAN program can be divided into three sections:

Program Name – All programs and subprograms have names. The name can consist of up to 31 characters (letters, digits, or underscore), starting with a letter. The name of the program is optional. Comments – Comments are non-execution statements used to describe each part of the program. Each comment starts with exclamation mark !. Declarations – This section consists of a group of statements at the start of the program which are used to declare the variables 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.

1.3 Data Types, Declaration, and Parameterization

FORTRAN 90 deals with Five types of data.

Real: There are two representations, Decimal Representation: Real data must contain the decimal point like 23.45, 0.123, 123.0, -0.12, -0.12.

Exponential Representation: It consists of an integer or a real number in decimal representation followed by the letter E followed by an integer (the exponent) like 12.3456E2 , -1.2E-3 , 12E3.

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

2

Real numbers are also stored in 4 bytes and have only about 7 significant digits. Real numbers can lie typically between approximately ±1038. Double precision numbers are stored in 8 bytes. They have about 15 significant digits and can lie in the approximate range 10-307 - 10308.

Real variables are declared as follows:

REAL :: A, B, C REAL (KIND = double) :: D OR REAL*8 :: D

A, B and C are variables of real type. D is a double precision real variable.

Integer: Integers are represented by a string of numbers not including decimal points. Integers are normally stored in 4 bytes (that is, 32 bits, i.e. 32 binary 0s and 1s). This allows integers from -2147483648 (-231) to +2147483647 (231 - 1) to be represented. For examples 12 , -2745 .

An integer variable is declared as follows:

INTEGER :: A, B A and B are variables of integer type.

Character: Fortran only uses the following characters: Letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

a b c d e f g h i j k l m n o p q r s t u v w x y z Digits: 0 1 2 3 4 5 6 7 8 9 Special Characters: Space ' " ( ) * + - / : = _ ! & $ ; < > % ? , .

A character variable is declared as follows: CHARACTER (LEN = 9) :: name = "age is 25" CHARACTER :: C

The above will declare a variable called name that can be up to 9 characters long. If the (len = ) is omitted, the length is assumed to be 1.

CHARACTER(LEN=*) :: Title, Position Here, the actual lengths of variables Title and Position are unknown and will be determined elsewhere.

Logical: Logical variables have one of two values: .TRUE. or .FALSE. They take storage space of 1 byte. A logical variable is declared as follows:

LOGICAL :: A, B Here A and B have been declared as logical variables. logical :: a, b a = .TRUE. b = a .AND. 3 .LT. 5/2

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

3

Complex: FORTRAN allows variables to be declared as complex numbers. The complex number is stored in 8 bytes (4 bytes for the real part and 4 bytes for the imaginary part). The following statement show a complex variable declaration :

COMPLEX :: C

1.4 Rules have to be followed in forming a variable name:

1. The first character must be a letter, either lowercase or uppercase;

2. Case is insignificant, uppercase and lowercase letters are same;

3. Variable names are composed of letters, numbers, and the underscore character without spaces.

Examples: sum , sum2 , my_age , AVE12rage

The following variable names are not allowed:

2X (starts with a number) Z2+ (contains an operator) my age (contains a space)

1.5 Parameter (Constant) Declaration

We declare a constant in FORTRAN 90 using the word PARAMETER after the type as follows :

INTEGER, PARAMETER :: N = 10 REAL, PARAMETER :: pi = 3.141593 CHARACTER(len = 7), PARAMETER :: ERROR= "error 1"

1.6 Input and Output Statements

One of the important features of programming is being able to read input (from the keyboard, a data file, etc.) and output results (to the screen, a data file, etc.). The general commands available for these actions are:

READ The general form is: READ *, input-list (unformatted )

PRINT The general form is: PRINT *, output-list (unformatted )

CHARACTER(len=8) :: name INTEGER :: x REAL :: y LOGICAL :: A COMPLEX :: comp READ*, name, x, y, A, comp The inputs are : "ALI" 23 5.654 T (2,-3)

PRINT *, name, x, y, A, comp The outputs are : ALI 23 5.654 T (2 , -3)

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

4

1.7 Operators in FORTRAN 90 1.7.1 Assignment Operator:

The basic assignment operator is ( = ) which is often called equal to. The assignment has the form: variable_name = expression Consider the following assignments: INTEGER:: x=5 , y=10 Logical:: a, b a = .TRUE. b=a .AND. (3 .LT. 5/2) z = x**2-3*x+7 w = x + y x = x + 0.5

REAL, PARAMETER :: PI = 3.14 REAL :: Area INTEGER :: Radius Radius = 5 Area = (Radius ** 2) * PI

1.7.2 Arithmetic Operators

Arithmetic operators in FORTRAN 90 are Explained in the table below:

Operator Usage Examples

+ Used for addition Sum = a + b

- Used for subtraction Difference = a – b

* Used for multiplication Product = a * b

/ Used for division Quotient = a / b

** Exponentiation z = a ** b

1.7.3 Relational Operators:

The relational operators are explained in the following table:

Operator Usage Examples

.LT. or < Less than The following expressions are TRUE

5 < 7 , 3 >= 2 , 'A' < 'B'

"HASAN" < "HASSAN"

The following expressions are FALSE

5 > 7 , 2 /= 2 , 'a' > 'b'

"AAA">"AAB"

.GT. or > Greater than

.LE. or <= Less than or equal

.GE. or >= Greater than or equal

.EQ. or == Equality

.NE. or /= Not equal to

1.7.4 Logical Operators

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

5

The logical operators are used to combine multiple conditions (logical statements). The following table describes the logical operators:

Operator Usage Example

.AND. The compound condition is true,

if both conditions are true.

(a>b .AND. a>c) (3>2 .AND. 'A'>'B')

.OR. The compound statement is true,

if any or both conditions are true.

(a>b .OR. a>c)

(3>5 .OR. 'A'>'B')

.NOT. It negates the condition. .NOT.(a>b) .NOT. (FALSE)

1.8 Precedence of Operators

Precedence is an important aspect of operators. A list of operators and their precedence are given in the following table:

Note: Parentheses ( ) are given the highest precedence over all the arithmetic and logical operators . Inner parentheses is evaluated first.

1.9 Expressions in FORTRAN 90

FORTRAN 90 expressions can be illustrated through the following examples:

| |

(1) (a * x + b ) / c (2) (1.0 - EXP(a*SQRT(x)))/(1.0 + x*EXP(-ABS(x))) (3) a*COS(b*t+c)/SQRT(a**2 + b**2) Ex: Evaluate the following expressions:

FORTRAN 90 LECTURES ABDUL-MUTTALIB. A. H. AL-DOURI

6

Solution (i) 4*6**2/(2+12-12)= 4*6**2/(14-12)= 4*6**2/2=4*36/2 = 144/2= 72 (ii) a*(a+2.5)/(2*b)=2.5*(2.5+2.5)/(2*2.5) =2.5*(5)/(2*3.5)= 2.5*(5)/5.0 =(12.5)/5.0=2.5

(iii) (3>2) .AND. (1+2)<3 .OR. (4<=3) =.TRUE. .AND. .FALSE. .OR. .FALSE. = .FALSE. .OR. .FALSE=.FALSE.

Ex: Write a program to convert a distance in feet to meters

PROGRAM Feet_to_Meters ! Program to convert a length given in feet ! to the corresponding length in meters.

IMPLICIT NONE REAL, PARAMETER :: FtoM = 0.3048 REAL :: feet, meters PRINT *, "Enter the length in feet" READ *, feet meters = feet * FtoM PRINT *, "Meters= ", meters

END PROGRAM Feet_to_Meters

Ex: Write a program to compute the area and circumference of a circle.

PROGRAM Area_Circumference

IMPLICIT NONE REAL,PARAMETER :: PI=3.141593 REAL :: radius, area, circum PRINT *,"Enter the radius:"

READ *, radius area=radius**2*PI circum = 2*PI*radius PRINT *,"Area=", area, " Circumference=", circum END Area_Circumference