Professional Documents
Culture Documents
CSC211 Material
CSC211 Material
com
SCIENTIFIC
PROGRAMMING
(using FORTRAN)
COURSE OUTLINE
Programming Paradigms
Variables
Data Types
Introduction / Definitions
Types
Introduction / Definitions
Types
CHAPTER ONE
PROGRAMMING PARADIGMS
Programming Paradigms
Programming Paradigms
Programming can be viewed as an act of coding solution for a problem. It can also be defined as
It simply implies the ability of a programmer to communicate with the computer system in a
It is a transformational act that brings about changes in the status of task – e.g. manual task to
computational one.
PROGRAM: This implies the written instruction, which the computer takes in and act upon. It
expert in writing computer codes. His major responsible is to write programs that performs a
These indicate the language of choice for which the programmer communicates with the
computer machine.
In other words, programming language refers to the language with which a programmer codes.
We have 3 types:
Machine Language
Low-Level Language
This refers to the language which the computer understands. It is a language in which all
This language is very much close to machine language. It needs little translator to real machine
ASSEMBLY Language.
This is a language that is close to normal human English Language. It isn’t a languages the
computer understands, rather it needs the help of an interpreter or a compiler before computer
could understand it. Examples here includes QBasic (Quick – Beginners’ All Purpose Symbolic
and Instruction Code), Fortran (FORmulaTRANslator), Pascal (Delphi), Java, C, C++, C#,
Real Program Developed => Fortran (1954) John Backus & IBM Team.
paradigms
IMPERATIVE PROGRAMMING
This is the type of programming that deals with scientific computation i.e. It is found useful in
Examples of this programming paradigm are: FORTRAN, COBOL ,C / C++ and PASCAL
This is a type of programming in which object interact by sending messages that change their
state. In this programming type, inheritance, polymorphism and object modeling are essential
element.
FUNCTIONAL PROGRAMMING
This programming paradigms deals with mathematical functions in which the domain serves as
This programming paradigms deal with collection of logical declaration about what outcome a
EVENT-DRIVEN PROGRAMMING
This programming paradigms deals with design of program that respond to unpredictable events.
These event are normally generated from users’ actions e.g. mouse click, key press.
Example of this paradigm includes: VISUAL BASIC, VISUAL BASIC.NET, VISUAL C++,
CONCURRENT PROGRAMMING
This programming paradigm deals with collection of cooperating process by sharing resources
These include:
complex computations. They are also very useful for teaching and learning. Example of this
MANAGEMENT INFORMATION SYSTEM: This are program that are used for handling
organization information system e.g. payroll system, accounting system, inventory system,
online sale and marketing system. Example of this programming language includes: SQL,
ARTIFICIAL INTELLIGENCE: This are programming types that are used to develop
software that model after human intelligent behavior e.g. robot. Example of this programming
NOTE: AI falls under programming language paradigm of functional and logic programming
language.
SYSTEM PROGRAMMING: This is a programming type which assist in the design and
development of software on which other software runs i.e. system programming deals with
WEB-CENTRIC: This programming type involves developing programs that run on the
internet. Web-centric programming is like a two-edged sword, as it derives from both OOP and
Programming languages used include: JAVA, VISUAL BASIC, VISUAL BASIC.NET, PHP,
ADA: It was invented in 1982 by CHARLES BABBAGE but was named after a woman called
ADA
APL: (A programming language) It was design to handle matrix calculation and the designer is
PSEUDOCODE
Pseudocode does the same function as Algorithm but the language of expression differs. While in
algorithm, more technical terms are used, raw English-like statements are used in Pseudocode.
PSEUDOCODE - ILLUSTRATION
1) Literally, take for instance if you are hungry and intend ‘executing’ a rice – what are the
psudocode steps/procedures for ensuring this? (varied depending on how you want to
2) Design a pseudocode that solves the need to compute for the scores of ten (10) students in
SOLUTION I
iv. Wash the rice in a basin and put unto the water-pot
v. Depending on the type of rice been cooked, put salt or any other ingredient.
SOLUTION II
iv. Continue this steps until there is no more student to input for
ALGORITHM
Algorithms are sequence of steps taken in solving a problem. It involves highlighting intended
In programming context, an algorithm can be described as a guide to logical coding that yields
PSEUDOCODE - ILLUSTRATION
Literally, take for instance if you are hungry and intend ‘executing’ a rice – what are the
algorithmic steps/procedures for ensuring this? (varied depending on how u want to program to
Design an algorithm that solves the need to compute for the scores of ten (10) students in
SOLUTION I
Set stove/cooker ON
Water-Pot = Rice
SOLUTION II
Stop
5 CLS
10 REM
12 LET N = 0
46 N = N + 1
47 IF N<=10 GOTO 15
50 END
Instruction #1: Fill in the spaces left above, use the information to submit your assignment,
especially the M_No (meaning Material No)
Instruction #2: Solve in a sheet of paper and submit with the requested info in Instruction #1
Questions
x2 + y3 – (z * w/2)
(2) Write a Pseudocode and Algorithm to compute a candidate’s age given the birthyear and
(3) Write a Pseudocode and Algorithm to accepts the UTME and PUTME scores of candidates,
CHAPTER TWO
Variables
Data Types
2.1 VARIABLES
The type/nature of data a variable holds depends on the data type description specified for the
variable.
The acceptability of a variable depends on its conformity to the rules that pertain to forming
variables.
Character Sets
These are sets of alphanumeric strings that are acceptable for use in coding/programming.
Numbers: 0-9
Alphabetic: a-z or A-Z (not case sensitive, so, a-z is the same as A-Z)
The followings are rules that govern the formation of Variable Names (VN):
• VN must start with letter; it cannot start with numeric/digit or underscore e.g. 7up, _myname
are not an acceptable variable names. But, up7, my_name & myname_ are all acceptable
Data Types described the nature of data that would be stored in a variable. Specifying a particular
data type for a particular variable indicates the type of data permissible to be captured for that
CHARACTER (LEN): For specifying ‘string’/ ‘alphanumeric’ data with specified number
of expected characters.
i. INTEGER:
ii. REAL:
iii. COMPLEX:
iv. CHARACTER:
v. CHARACTER (LEN):
vi. LOGICAL:
(ii) Declaring VARIABLEs in FORTRAN is expected to be explicit and therefore requires that
all VARIABLEs to be used must be declared. To indicate compliance with this, the FORTRAN
Compiler expects issuance of a command called IMPLICIT and it been set to NONE.
However, for VARIABLES that are not explicitly defined, FORTRAN takes over the
responsibility of deciding their DATA TYPES (to know the type of data expected to be stored in
• Variable names that start with the lettering I, J, K, L, M, N are auto-ascribed INTEGER
• While variable names starting with other lettering (in the letter of alphabets) are taken
as REAL
COMMENTING
Expressions are toward the right-hand side of our equation (without the identifier). They are
X + Y is an expression
We have:
• Arithmetic Expression
• Logical Expression
• Relational Expression
PROGRAM myfirstFORTRAN
IMPLICIT NONE
REAL :: W
! How do I now capture values for X & Y? Be patient till the next lecture/chapter.
CHAPTER THREE
PROGRAM myfirstFORTRAN
IMPLICIT NONE
REAL :: W
! How do I now capture values for X & Y? Be patient till the next lecture
OUTPUT STATEMENT
Print, and
Write Statements
While PRINT is used for achieving simple output statement, WRITE is more suitable for
formatted statement.
A Fortran program to illustrate simple use of PRINT and WRITE Statements (will be
INPUT STATEMENT
2) A Fortran program to illustrate the use of the three (3) discussed commands, viz:
At our last lecture/session, we dwelt so much on Simple I/O Statements in FORTRAN, where we
Solution
program illustration
implicit none
integer::p,q,r,s
real::z
write(*,*) "Enter value for P: "
read (*, *) p
write (*,*) "Enter value for Q: "
read (*,*) q
write (*,*) "Enter value for R: "
read (*,*) r
write (*,*) "Enter value for S: "
read (*,*) s
z = ((((p+q)/2)-((r**2)/(s))/3))/5
print *,"The value of z is: ", z
end program illustration
As you examined the last output, you will observed that the output was without ‘control’. The
To format output, we use the format specifier ‘f’ with appropriate specification as it relates to the
As stated earlier, the format specifier is ‘f’ specified alongside the data type. For REAL data
Where:
f is the formatter; w is the total no of digit expected; and d is the no of decimal to the right;
program illustration
implicit none
integer::p,q,r,s
real::z
write(*,*) "Enter value for P: "
read (*, *) p
write (*,*) "Enter value for Q: "
read (*,*) q
write (*,*) "Enter value for R: "
read (*,*) r
write (*,*) "Enter value for S: "
read (*,*) s
z = ((((p+q)/2)-((r**2)/(s))/3))/5
! little modification using write formatted statement
write(*,'(f6.2)') "The value of Z is: ", z
end program illustration
fw OR rfw
Where:
repeat count
program illustration
implicit none
integer::p,q,r,s
real::z
Instruction #1: Fill in the spaces left above, use the information to submit your assignment,
especially the M_No (meaning Material No)
Instruction #2: Solve using the system, print out and submit with the requested info in #1
Questions
Q1
Given three numbers, num1, num2 & num3. Captures values for them in an interactive way and
then compute: (i) their summation (ii) their product (iii) their average
Q2
On your own, input values for two variables X and Y. Find (i) their Division value (ii) the
Q3
‘Bring down’ your brain, device a mathematical problem on your own/by yourself, solve using
Q4
CHAPTER FOUR
CONTROL STRUCTURE
Introduction / Definitions
Types
INTRO / DEF
Control Structure can be defined as statements that caused diversion in the normal flow of
program execution. They can be seen or described as program constructs that change or obstruct
TYPES
A fortran program to calculate the sum of squares and sum of roots of two variables X and Y.
program loop_prog1
implicit none
real::x,y,sumsquare,sumroot
write(*,*) "Enter value for X: "
read(*,*)x
write(*,*) "Enter value for Y: "
read(*,*)y
sumsquare = (x**2)+(y**2)
sumroot = sqrt(x)+sqrt(y)
write (*,*) "The sum of square of the two variables is: ",sumsquare
write (*,*)
write (*,*) "The sum of roots of the two variables is: ", sumroot
end program loop_prog1
…the output
TYPES…
Selection C. S.
These are control statements that bring about changes in the normal flow of program execution
They caused branching depending on the outcome of the evaluation of the condition specified.
TYPES…Selection C. S.
We have:
i. IF – THEN Selection C. S.
IF-THEN
This is a mono-directional selection control statement in which only an option is presented for
execution and it is executed iff the evaluation of the condition specified is TRUE, else no
Format:
IF-THEN format
If (condition) Then
program statement
End If
Program Illustration
of x and y
…d codes
PROGRAM controlprog1
implicit none
integer::x,y,z
write(*,*) "Enter 1st value: "
read (*,*)x
write(*,*) "Enter 2nd value: "
read (*,*)y
if (x .gt. y) then
z=x+y
end if
write(*,*) "The value of z is: ", z
end program controlprog1
…d output!
IF-THEN-ELSE
This is a bi-directional selection control statement in which two options are presented but only
one can be executed at a time based on the result of the condition specified.
Format:
IF-THEN-ELSE format
If (condition) Then
program statement 1
Else
program statement 2
End If
Program Illustration
…d codes
PROGRAM controlprog2
implicit none
integer::x,y,z
write(*,*) "Enter 1st value: "
read (*,*)x
write(*,*) "Enter 2nd value: "
read (*,*)y
if (x .gt. y) then
z=x+y
else
z=x-y
end if
write(*,*) "The value of z is: ", z
end program controlprog2
…d output!
IF-THEN-ELSEIF
This is a multi-directional selection control statement in which many options are presented but
only one can be executed at a time based on the result of the condition specified.
Format:
IF-THEN-ELSEIF format
If (condition) Then
program statement 1
program statement 2
program statement 3
Else
program statement n
End If
Program Illustration
…d codes
PROGRAM controlprog2
implicit none
integer::x,y,z
write(*,*) "Enter 1st value: "
read (*,*)x
write(*,*) "Enter 2nd value: "
read (*,*)y
if (x .gt. y) then
z=x+y
elseif (x .lt. y) then
z=x-y
else
z=x
end if
write(*,*) "The value of z is: ", z
end program controlprog2
…d output
SELECT CASE
This is also a multi-directional selection control statement in which many options are presented
but only one can be executed at a time based on the result of the condition specified.
Program Illustration
A fortran program to implement a computing service operation, which allows users to specify
TYPES…Selection C. S. (d codes)
PROGRAM controlprog3
implicit none
integer::x,y, opcode
real::z
write(*,*) "Enter opcode: 1 for ADD, 2 for SUB, 3 for PROD, 4 for DIV"
read (*,*)opcode
write(*,*) "Enter 1st value: "
read (*,*)x
write(*,*) "Enter 2nd value: "
read (*,*)y
…d codes…
…d output!
Iterative C. S.
These are control statements that are used for repetitive execution of program statements. They
We have:
Do…Counter Statement
Do…Conditional Statement
Do…Counter Statement
As the name suggests, counter variable is used alongside the DO Statement specification.
End do
Illustration
Write a fortran program to compute the sum of the first five (5) even integers.
…d codes!
program do_counter_pro
implicit none
integer::count_var,sumeven=0
write (*,*) "The even numbers are: "
do count_var = 2,10,2
sumeven = sumeven + count_var
write (*,*)
write (*,*)count_var
end do
write(*,*)
write (*,*) "Their sum is: ", sumeven
end program do_counter_pro
…d output!
IF-CYCLE: This is used to skip/jump a Do-Counter Loop. Execution got skipped at the
IF-EXIT: (format)
if (condition) exit
end do
IF-CYCLE: (format)
do counter = …, …, …
if (condition) cycle
end do
IF-EXIT: (Illustration)
IF-EXIT: d codes…
program if_exit_pro
implicit none
integer::matno
write (*,*) "The First Five are: "
do matno = 160404001,160404020
if(matno>160404005)exit
write (*,*)"No.", matno - 160404000, " ", matno
end do
end program if_exit_pro
IF-EXIT: d output!
IF-CYCLE: (Illustration)
program if_cycle_pro
implicit none
integer::matno
write (*,*) "The Eligible Students are: "
do matno = 160404001,160404020
if(matno==160404004 .or. matno==160404008 .or. matno==160404016 .or.
matno ==160404020)cycle
write (*,*)matno
end do
end program if_cycle_pro
IF-CYCLE: d output!
Do…Conditional Statement
Do while (condition)
….code goes here
End do
programs.
ii. Converts all the Do_Counter Supplementary Commands (if-exit and if-cycle)
Note: In using If-exit and if-cycle under Do-Conditional, the while statement will not be used.
e.g.
do
program statement
if(condition) if-exit or if-cycle
end do
Instruction #1: Fill in the spaces left above, use the information to submit your assignment,
especially the M_No (meaning Material No)
Instruction #2: Solve using the system, print out and submit with the requested info in #1
Questions
Q1
Write a fortran program to compute the sum of the first ten (10) odd integers.
Q2
Write a fortran program to compute the sum of the first ten (10) integers.
Q3
Modify the program in page 35 such that it outputs only the expunge students and title the
heading as appropriate.
Q4
Given the table below, code in Fortran to output candidates based on company policies
that absorbed Male and Female that are less than 30years and of High Experience.
CHAPTER FIVE
ARRAY STRUCTURE
Introduction / Definitions
Types
DEFINITIONS
An array is a collection of data element, captured with the same name (storage location name)
The nature of data stored in an array depicts the type of array been created (i.e. numeric array,
PICTORIAL REPRESENTATION
TYPES
LINEAR ARRAY
DECLARING AN ARRAY
e.g.
ILLUSTRATION I
PROGRAM array_pro2
implicit none
INTEGER, DIMENSION (6):: my_elements=(/4,6,2,8,9,34/)
write (*,*) "The whole elements are (all elements): ", my_elements(:)
write (*,*) "Sub-section of the array (from d 2nd to d 5th step1): ", my_elements(2:5:1)
write (*,*) "Sub-section of the array (from d 1st element to d last step2): ", my_elements(::2)
end program array_pro2
…output
…d codes…
PROGRAM array_pro3
implicit none
INTEGER, DIMENSION (5):: my_array
INTEGER :: i
write(*,*) "Capturing Data into the Array"
write(*,*)
do i = 1,5
write (*,*) "Element ", i, " : "
read (*,*)my_array(i)
end do
end program array_pro3
..d output…
Some of the array operations that can be performed on ARRAY include but not limited to:
PROGRAM array_pro1
implicit none
INTEGER, DIMENSION (5):: my_array
INTEGER :: i
write(*,*) "Capturing Data into the Array"
write(*,*)
SPECIFIC ARRAY OPERATIONS…code illustration
do i = 1,5
write (*,*) "Element ", i, " : "
read (*,*)my_array(i)
end do
write (*,*) "The highest element in the array is: ", MAXVAL(my_array)
write (*,*) "The lowest element in the array is: ", MINVAL (my_array)
write (*,*) "The sum of the elements in the array is: ", SUMVAL(my_array)
write (*,*) "The whole array elements are: ", my_array
end program array_pro1
NB: Run the program and show the output to the next person close to you!
Some of the arithmetic operations that can be performed on ARRAY include but not limited to:
Summation
Multiplication
Comparison
program array_pro4
implicit none
integer, dimension(4)::mydata,yourdata, sum_all_data, product_both_data
integer::i
do i = 1,4
write (*,*) "Enter value for array mydata element ", i, " : "
read (*,*) mydata(i)
write (*,*) "Enter value for array yourdata element ", i, " : "
read (*,*) yourdata(i)
sum_all_data(i)= mydata(i) + yourdata(i)
product_both_data(i) = mydata(i) * yourdata(i)
end do
end program array_pro4
…output!
Scientific programming (fortran) 41
ajayiprogrammer@gmail.com
MULTI-DIMENSIONAL ARRAY
An array that consist of rows and columns of data. It is also called MATRIX.
3 5 6 7
6 3 2 1
9 18 10 5
integer, dimension(2,4)::my_matrix
integer, dimension(2,5)::my_matrix1, my_matrix2
integer, dimension(2,3)::my_matrix1
integer, dimension(2,7)::my_matrix2
program matrix_pro
implicit none
integer, dimension(2,3)::mytable
integer i,j
write (*,*) "Capturing data into matrix"
do i=1,2
do j=1,3
write (*,*) "Enter data for Row ", i, " Column ", j
read (*,*) mytable(i,j)
write(*,*)
end do
end do
end program matrix_pro
MULTI-DIMENSIONAL ARRAY – output
Instruction #1: Fill in the spaces left above, use the information to submit your assignment,
especially the M_No (meaning Material No)
Instruction #2: Solve using the system, print out and submit with the requested info in #1
Questions
Q1
Assuming with me is N54,890. out of this, my intention is to have 2/3 of it to take care of my
domestic expenses, 35% for academic expenses, 65.7% for religious commitment, 2.3% for
electrical & utility bills, and 18.4% for miscellaneous expenses. Help FORTRAN-code my left-
over or deficit as the case may be. Your code must be such that logically detect if the expenses
Q3
Quadratic equation is not a mathematical problem strange to you. Using FORTRAN, fully code
Q4
Coding/programming/developing software most times also require the need to search and acquire
implemented. Weather forecasting is such a domain where u may need to do some searching to
know how the prediction is done. So, while preparing to code, pause a little, browse how weather
are been forecasted. From your search, code in FORTRAN to implement a generic weather
forecasting program.
Q5
Modify the program in page 41 (Code Illustration II) to output the values of sum_all_data and
product_both_data.