Fortran language 90 for high school, Exercises of Computer science

simple introduction about Fortran

Typology: Exercises

2020/2021

Uploaded on 01/10/2023

Zena_Rasheed
Zena_Rasheed 🇮🇶

1 document

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Syllabus
Introduction to Fortran language.
Constants and variables.
Declaration of variables.
Library functions.
Mixing of variable types.
Control statements:
- If statements.
- Go to, Exit, End.
- Loops
Files.
Matrices:
- One-dimensional array.
- Two-dimensional array.
Functions and subroutine.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Fortran language 90 for high school and more Exercises Computer science in PDF only on Docsity!

Syllabus

 Introduction to Fortran language.  Constants and variables.  Declaration of variables.  Library functions.  Mixing of variable types.  Control statements:

  • If statements.
  • Go to, Exit, End.
  • Loops  Files.  Matrices:
  • One-dimensional array.
  • Two-dimensional array.  Functions and subroutine.

Lecture

Fortran:

Fortran is the abbreviation of “formula translation” and it’s a compiled programming language, that is mainly used for numeric computation and scientific computing.

Important concepts:

Workspace: it’s an environment in which files are stored and it’s considered to be a hub for all the programs created within it.  Text file: it’s the space in which the program (code) is written.  Variable: every data stored within the computer memory can be labeled by giving it a name, and this name is the variable. Variables can be real numbers, integers, or texts.

Types of variables:

 Integer.  Real.  Character.  Complex.  Logical.

Mathematical symbols:

Mathematics Fortran

× *

÷ /

^ **

Fortran program (code) structure:

Example: Write a computer program to multiply two values.

Program example implicit none

real :: a , b , c

read *, a read *, b

c = a * b

print *, c

End program example

Header

Declaration of variables

Input

Treatment

Output

Hint: When declaring a variable, don’t declare it as a letter e.g. (x, r, a, etc.). Instead, declare it as a meaningful word e.g. (length, area, radius, etc.)

Hint : Use navigation messages in order to let the user know the data that must be entered into the program.

Classwork: Write a computer program to calculate the radius of a circle by entering a value for the radius.

Program Circle_Area implicit none real :: area , radius print *, “Enter the value of the radius” read *, radius area = 3.14159 * radius ** 2 print *, area end program Circle_Area

Example: write a computer program to switch between to values.

Solution:

Program Switch implicit none real :: a , b , c read *, a , b c = a a = b b = c print *, a , b end

Navigation message

Precedence in mathematical operations:

1- Parentheses. ( ) 2- Library functions. 3- Power. (*) 4- Multiplication and division. ( & /) 5- Addition and subtraction. (+ & -)

Mixing of variable types:

3- 𝑖𝑛𝑡𝑒𝑔𝑒𝑟/𝑟𝑒𝑎𝑙 = 𝑟𝑒𝑎𝑙 → 𝑦 = 3.0^1 → 𝑦 = 0.

Example: write a computer program to calculate the value of Y given by the following equation:

𝑦 = 𝑠𝑖𝑛^3 (
3𝜋^2

Solution:

Program calculate_y implicit none real :: y y = (sin(3.14159/6))3-sqrt(150(1.0/3)/(33.14159*2)) print *, y end program calculate_y

Note : in order to declare a parameter, we will write this piece of code:

Parameter type, parameter :: parameter’s name = parameter’s value

e.g. integer, parameter :: x = 5

Classwork: write a computer program to calculate the value of y given by the following equation:

√𝑦 3

𝑠𝑖𝑛^2 𝑥 − 3𝑥

Solution:

Program calculate_y implicit none real :: y , x read , x y = (3abs(((sin(x))2-(3x))/(x+(cos(x/3.14159))2)))* print *, y end program calculate_y

Lecture

Logical expressions:

Expression Explanation

> Greater than

< Less than

== Equals to

<> Does not equal to

Greater than or equals to

Less than or equals to .and. And .or. Or .not. not

Control statements:

If statements:

There are four types of conditional statements

1- IF (condition) statement

In this type of conditional statements, only one statement is written and it would be executed if the condition was true

Example: Write a computer program that tells if a student has passed a test by giving the student’s mark.

Solution:

Program status implicit none real :: mark read *, mark if (mark >= 50) print *, “The student has passed the test” end program status

3- IF (condition) then statement # else statement # end if

In this type the first statement will be executed, if the condition is true. Otherwise, the second statement will be executed.

Example: Write a computer program that tells if a student has passed or failed a test by giving the student’s mark.

Program status implicit none real :: mark read *, mark if (mark >= 50) then print *, "The student has passed the test" else print *, “The student has failed the test” end if end program status

4- IF (condition) then statement # else if (condition) then statement # else if (condition) then statement # end if

Example: Write a computer program that tells if a student has passed or failed a test by giving the student’s mark.

Program status implicit none real :: mark read *, mark if (mark >= 50) then print *, "The student has passed the test" else if (mark < 50 ) then print *, “The student has failed the test” end if end program status

Classwork: Write a computer program that estimates a student’s mark depending on their status (either “passed” or “failed”):

Solution:

program evaluator implicit none character * 50 :: status read *, status if (status == "passed") then print *, "The mark is equal to or above 50" else print *, "The mark is below 50" end if end program evaluator

Homework: Write a computer program to evaluate a student’s mark.

Solution:

Program evaluation implicit none real :: mark if (mark > 89) then print *, “The evaluation is Excellent” else if ( mark <= 89) .and. (mark >= 80) then print *, “The evaluation is very good” else if ( mark <= 79) .and. (mark >= 70) then print *, “The evaluation is Good” else if ( mark <= 69) .and. (mark >=60) then

print *, “The evaluation is intermediate” else if ( mark <= 59) .and. (mark >= 50) then print *, “The evaluation is Acceptable” else print *, “The evaluation is Failed” end if end