0% found this document useful (0 votes)
133 views39 pages

CSC 224 Slide

It's about algorithm and programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views39 pages

CSC 224 Slide

It's about algorithm and programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

FOUNDATIONS OF

SEQUENTIAL PROGRAM:
COURSE CODE: CSC 224
CREDIT UNIT: 3 UNIT
Lecturer: Dr. SALAMI E. EMMANUEL
CONTACT: +234-9015-990-204
E-Mail: [email protected]
COURSE
CONTENT
 Principles of Programming
 Evolution of Programming Languages
 The relationships between High Level and Low Level languages.
 Computer Architecture that underlies their implementation High Level
and Low Level Languages.
 Basic machine architecture.
 Specification and Translation of P/L Block Structured Languages
 Parameter passing mechanisms.
PRINCIPLES OF PROGRAMMING
WHAT IS PROGRAMMING
The process of developing series of sequence of instructions to be given to computer so as to
perform a specific task.

A programmer requires some basic tools to be able to design, develop, test, implement or
maintain computer programs. These tools include text editors, compilers, interpreters,
diagnostic tools, etc.
Text editors provide the basic means of creating and modifying text files that is open, view,
and edit plain text. Examples include Notepad, ETextEditor, GNU Emacs, EditPlus, gedit,
Textpad, UltraEdit, etc.
A compiler is a special program that processes statements written in a particular
programming language and turns them into machine language or "code" that a computer's
processor uses.
An Interpreter directly executes instructions written in a programming or scripting
language without previously converting them to an object code or machine code. Examples
of interpreted languages are Perl, Python and Matlab.
programs are sometimes integrated in one graphical user interface. This integrated tool is
known as Integrated Development Environment (IDE) and it is used for rapidly developing
computer programs. Examples are Visual Studio, NetBeans, JBuilder, Eclipse etc.

PROGRAM DESIGN AND SPECIFICATION

Program design applies to the development or production of computer programs. A basic


knowledge of program design is needed to write programs of reasonable quality.
A program specification is usually part of a system specification, which defines the whole
system.

The aims of program design:


i. Reliability: The program can be depended upon always to do what is supposed to do.
ii. Maintainability: The program will be easy to change or modify when the need arises
iii. Readability: The program will be easy for a programmer to read and understand
iv. Performance: The program causes the tasks to be done quickly and efficiently
v. Storage saving: The program is not allowed to be unnecessarily long to achieve memory
Program Development Life Cycle

 The program development life cycle is a set of steps or phases that are used to develop a
program in any programming language.
 PDLC is a systematic way of developing quality software.
 It provides an organized plan for breaking down the task of program development into
manageable chunks, each of which must be successfully completed before moving on to
the next phase.
Program Development Life Cycle (PDLC)
Program Development process is divided into
six steps discussed below:

I.) Problem Definition


The first step is to define the problem. In major
software projects, this is a job for system
analyst, who provides the results of their work
to programmers in the form of a program
specification. The program specification defines
the data used in program, the processing
 In this phase, the problem statement is defined and the boundaries of the problem are
decided.
 In this phase there is need to understand the problem statement, what is the requirement,
and what should be the output of the problem solution.
 These are defined in this first phase of the program development life cycle. The
developers must obtain the program requirements from the users and document the
requirements.
 A program that meets its specification is said to be correct. An important factor in
determining program specification is to produce requirements specification. A system
analyst will discuss the requirements specification with the users.
 A REQUIREMENT is simply a statement of what the system must do or what
characteristics it needs to have.
 During a systems development project, requirements will be created that describe what
the business needs (business requirements); what the users need to do (user
requirements); what the software should do ( functional requirements); characteristics the
system should have (nonfunctional requirements); and how the system should be built
(system requirements).
II.) Problem Analysis
During analysis, a programmer review specifications to fully understand what the software
should do. The analysis of the problem to be solved involves having the basic understanding
of the problem, identification and designing of inputs and outputs and identification of any
suitable solution model. The requirements like variables, functions, etc. to solve the problem
are determined in this phase. That means the required resources to solve the problem defined
in the problem definition phase are gathered in this phase.

III.) Algorithm Development


During this phase, a step by step procedure to solve the problem using the specification
given in the previous phase is developed. This phase is very important for program
development. That means we write the solution in step by step statements. Program design
starts by focusing on the main goal that the program is trying to achieve and then breaking
the program into manageable components, each of which contributes to this goal. This
approach of program design is called top-bottom program design or modular
programming.
The various program design tools are described below:
a. Algorithms
An algorithm is a step-by-step description of how to arrive at a solution in the easiest way.
Algorithms are not restricted to computer world only. In fact, we use them in everyday life.

There are some basics steps to make an algorithm:

Start – Start the algorithm


Input – Take the input for values in which the algorithm will execute.
Conditions – Perform some conditions on the inputs to get the desired output.
Output – Printing the outputs.
End – End the execution.
Example. Find the greatest between 3 numbers.
Step 1: Start
Step 2: Take 3 numbers as input, say A, B, and C.
Step 3: Check if(A>B and A>C)
Step 4: Then A is greater
Step 5: Print A
Step 6: Else
Step 7: Check if(B>A and B>C)
Step 8: Then B is greater
Step 9: Print B
Step 10: Else C is greater
Step 11: Print C
Step 12: End
b. Flowcharts
A flowchart is a diagram that shows the logic of the program. For example:
c. Decision tables
A Decision table is a special kind of table, which is divided into four or more parts by a pair of
horizontal and vertical lines.
d Pseudo-code
A pseudo-code is another tool to describe the way to arrive at a solution. They are different
from algorithm by the fact that they are expressed in program language like constructs.
IV) Coding and Documentation
This phase uses a programming language to write or implement the actual programming
instructions for the steps defined in the previous phase. In this phase, we construct the actual
program. That means we write the program to solve the given problem using programming
languages like C, C++, Java, etc., Coding the program means translating an algorithm into
specific programming language. The technique of programming using only well-defined
control structures is known as Structured programming.

V) Testing and Debugging


After removal of syntax errors, the program will execute. However, the output of the
program may not be correct. This is because of logical error in the program. A logical
error is a mistake that the programmer made while designing the solution to a problem.

The process of identifying errors and eliminating them is known as Debugging.


During this phase, there is need to check whether the code written in the previous step is
solving the specified problem or not. That means we test the program whether it is solving
the problem for various input data values or not.
V) Maintenance
After testing the software project is almost complete. The structure charts, pseudo-codes,
flowcharts and decision tables developed during the design phase become documentation for
others who are associated with the software project. This phase ends by writing a manual
that provides an overview of the program’s functionality, tutorials for the beginner, in-depth
explanations of major program features, reference documentation of all program commands
and a thorough description of the error messages generated by the program.

SEQUENTIAL PROGRAM STRUCTURES


Sequential programming is when the algorithm to be solved consists of operations one after
the other. A sequential program explicitly waits in-line, for the expected events in various
places in the execution path.
e.g of sequence program is
First cooking dinner, then eating, and then washing the dishes is one sequence. However,
First eating, then washing the dishes, and then cooking is a much less sensible sequence.

 In sequential composition, different program components execute in sequence on all


processors.
 Based on the number of microprocessors, computers can be classified into Sequential
computers and Parallel computers.
 Any task complete in sequential computers is with one microcomputer only
 The parallel computer is relatively fast. The processors perform different tasks
independently and simultaneously thus improving the speed of execution of complex
programs dramatically. Parallel computers match the speed of supercomputers at a
 fraction of the cost.
Sequential program structures are forms in which program components are constructed,
organized and interrelated. In learning a programming language, there is need to learn
about two important aspects of the language which are: the SYNTAX AND SEMANTICS.
 The syntax of a language is the grammatical rules that govern the ways in which words,
symbols, expressions and statements may be formed and combined.
 The semantics of a language are the rules that govern its meaning.

 The main features features of the programming language are: storage, input and
output, operation on data, and control.

 STORAGE: Computers require a set of instructions and data to be stored in their


memory to perform a specific task. This stored program concept was originated ever
since the invention of Charles Babbage’s difference engine in 1822.

 In programming languages, data are identified by name rather than by their location
addresses in main storage. The names that associate stored data values are called
identifiers because an identifier is the name by which the data value may be identified.
are forms in which program components are constructed, organized and interrelated. In
learning a programming language, there is need to learn about two important aspects of the
language which are: the SYNTAX AND SEMANTICS.
 The syntax of a language is the grammatical rules that govern the ways in which words,
symbols, expressions and statements may be formed and combined.
 The semantics of a language are the rules that govern its meaning.

 The main features features of the programming language are: storage, input and
output, operation on data, and control.

 STORAGE: Computers require a set of instructions and data to be stored in their


memory to perform a specific task. This stored program concept was originated ever
since the invention of Charles Babbage’s difference engine in 1822.

 In programming languages, data are identified by name rather than by their location
addresses in main storage. The names that associate stored data values are called
identifiers because an identifier is the name by which the data value may be identified.
DATA DECLARATION
The act of defining a variable is called data declaration.

 A VARIABLE is a symbolic name assigned to a data item by the programmer.


 At any particular time, a variable will stand for one particular data, called the value of a
variable, which may change from time to time during a computing process.
 The value of a variable may change many times during the execution of a program.
 A variable is usually given a name by the programmer. The variable must be declared
that is to state its data type and its Value.

A DATA TYPE is a classification of data, which can store a specific type of information.
Data types are primarily used in computer programming in which variables are created to
store data. There are a number of traditional data types found in most languages.
EXAMPLE OF DATA TYPES IN PYTHON PROGRAMMING LANGUAGE

0 , 1, 2, 3, -1, -2, and -3 are all integers

1.0 and 3.14 are floating-point numbers.


It composed of real and imaginary parts. They have the form a +
bi, where a and b are real numbers, and i is the imarginary unit.
(2 + 3j)
They are sequence of character data. print("I am a string")

Bytes are immutable sequences of single bytes

These are used in conditions and conditional statements which control


how the program will behave. Logical operator examples include
AND (&&), OR (||) and NOT (~).
Purpose of Declarations:
i. Choice of storage representation: Translator determine the best storage representation
of data types that is why it needs to know primarily the information of data type and
attribute of a data object.
ii. Storage Management: It helps the computer to make best use of memory for data
object by providing its information so that computer can allocate the optimum size of
memory for the data.

INPUT AND OUTPUT


Programming languages have special functions for dealing with input and output.
Common names for these functions are; input, read, get, accept, output, write, print, put
and display.
This is illustrated using the following simple Java program:
ComputeArea.java

1 public class ComputeArea {


2 public static void main (String[] args) {
3 double radius; // Declare radius
4 double area; // Declare area
5
6 // Assign a radius
7 radius = 20; // New value is radius
8
9 // Compute area
10 area = radius * radius * 3.14159;
11
12 // Display results
13 System.out.println("The area for the circle of radius " +
14 radius + " is " + area);
OPERATIONS ON DATA
Operations are expressed in the form of statements. The simplest statement is the
assignment statement. It consists of a variable name, followed by the assignment operator
(=), followed by some sort of expression. The assignment operation has the form:
variable = expression

CONTROL is discribed as the process of designing an algorithm in an understandable


form. The key to better algorithm design and thus to programming, lies in properly defining
the control structures. These control structures can be grouped into three constructs namely
the sequence structure, the decision structure or selection structure and the repetition or
iteration structure.

The sequence structure


This structure is the most elementary structure. The sequence structure is a case where the
steps in an algorithm are constructed in such a way that, no condition step is required. The
sequence structure is the logical equivalent of a straight line.
Example 1: Suppose you are required to design an algorithm for finding the average of six
numbers, and the sum of the numbers is given.
The pseudocode will be as follows:

Start
Get the sum
Average = sum / 6
Output the average
Stop
Example 2: The following pseudo-code describes an algorithm which will accept two
numbers from the keyboard and calculate the sum and product displaying the answer on the
monitor screen.

Use variables sum, product, number1, number2 of type real display “Input two numbers”
accept number1, number2
sum = number1 + number2
print “The sum is “, sum
product = number1 * number2
print “The Product is “, product
end program
Decision Structure or Selection Structure
The decision structure or mostly commonly known as a selection structure, is case where
in the algorithm, one has to make a choice of two alternatives by making decision
depending on a given condition.
Selection structures are also called CASE selection structures when there are two or
more alternatives to choose from. This structure can be illustrated in a flowchart as
follows:
In pseudocode form:
If condition is true
Then do task A
else
Do Task-B
In this example, the condition is evaluated, if the condition is true Task-A is evaluated
and if it is false, then Task-B is executed A variation of the construct of the above figure
is shown below
Compound Logical Operators

Consider the following statement:


‘If I had the time and the money I would go on holiday’. The ‘and’ means that both
conditions must be true before any action is taken.

Another statement is ‘I am happy to go to the theatre or the cinema’. The logical link this
time is or. Conditions in if statements are linked in the same way. Conditions linked with
and only result in an action when all conditions are true. For example, if a >b and a > c
then display “a is the largest”. Conditions linked with an ‘or’ lead to an action when
either or both are true.
Example: The program is to input an examination mark and test it for the award of a
grade. The mark is a whole number between 1 and 100. Grades are awarded according
to the following criteria:

>=80 Distinction
>=60 Merit
>=40 Pass
< 40 Fail

The pseudo-code is
Use variables: mark of type integer
If mark >= 80 display “distinction”
If mark >= 60 and mark < 80 display “merit”
If mark >= 40 and mark < 60 display “pass”
If mark < 40 display “fail”
An if statement on its own is often not the best way of solving problems. A more elegant
set of conditions can be created by adding an else statement to the if statement. The else
statement is used to deal with situations as shown in the following examples.

Example: A person is paid at top for category 1 work otherwise pay is at normal rate.

If
Else
the work is category 1
pay-rate is top
pay-rate is normal
The else statement provides a neat way of dealing with alternative condition.

In pseudo- code this can be written as:


If
then
Else
work = cat1
p-rate: = top
p-rate = normal
The following example illustrate the use of if … else statements in implementing double
alternative conditions.
If
salary < 50000

then
Tax = 0
Else
If
salary > 50000 AND salary < 100000
then
Tax = 50000 * 0.05
Else
Tax = 100000 * 0.30
Repetition or Iteration Structure
A third structure causes the certain steps to be repeated.

The Repetition structure can be implemented using


• Repeat Until Loop
• The While Loop
• The For Loop

Any program instruction that repeats some statement


or sequence of statements a number of times is called
an iteration or a loop. The commands used to create
iterations or loops are all based on logical tests. There
three constructs for iterations or loops in our pseudo-
code.
1. The Repeat Until loop.
The syntax is REPEAT A statement or block of statements UNTIL a true condition.

Example: A program segment repeatedly asks for entry of a number in the range 1 to
100 until a valid number is entered.

REPEAT
DISPLAY “Enter a number between 1 and 100”
ACCEPT number
UNTIL number < 1 OR number > 100

2. The WHILE loop


The second type of iteration to be considered is the while iteration. This type of conditional
loop tests for terminating condition at the beginning of the loop. In this case no action is
performed at all if the first test causes the terminating condition to evaluate as false.
The syntax is
WHILE (a condition is true)
A statement or block of statements

ENDWHILE

Example: A program segment to print out each character typed at a keyboard until the
character ‘q’ is entered.
WHILE letter <> ‘q’
ACCEPT letter
PRINT “The character you typed is”, letter
ENDWHILE
Example: Write a program that will output the square root of any number input until the number input is
zero.
In some cases, a variable has to be initialise before execution of the loop as shown in the following
example.

Use variable: number of type real


DISPLAY “Type in a number or zero to stop”
ACCEPT number
WHILE number <> 0
Square = number * number
DISPLAY “The square of the number is”, square
DISPLAY “Type in a number or zero to stop”
ACCEPT number

The FOR Loop


The third type of iteration, that can be used when the number of iterations is known in advance,
is a for loop. The variable is incremented on each iteration until it reaches the required value.
Example. FOR (n = 1, n <= 4, n + 1) PRINT “loop”, n ENDFOR The fragment of code will produce the
output Loop 1 Loop 2 Loop 3 Loop 4 In the exaExample 12.
FOR (n = 1, n <= 4, n + 1)
DISPLAY “loop”, n
ENDFOR
The fragment of code will produce the output
Loop 1
Loop 2
Loop 3
Loop 4
In the example, n is usually referred to as the loop variable, or counting variable, or index of the
loop. The loop variable can be used in any statement of the loop. The variable should not be
assigned a new value within the loop, which may change the behaviour of the loop. mple, n is usually referred
to as the loop variable, or counting variable, or index of the loop. The loop variable can be used in any
statement of the loop. The variable should not be assigned a new value within the loop, which may change the
behaviour of the loop.

You might also like