0% found this document useful (0 votes)
32 views57 pages

1 ICS 2175 Lecture 2 Intro To Algo To Use1

SUMMARY

Uploaded by

sandraakinyi307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views57 pages

1 ICS 2175 Lecture 2 Intro To Algo To Use1

SUMMARY

Uploaded by

sandraakinyi307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

ICS 2175Programming

Methodology
Fred Omondi

fred@kca.ac.ke
Course Objective

• To appreciate methodology of structured


programming

• To apply structured programming


methodology to write, compile and test
programs
Reference Material
  Internet material
  Class notes (given/ taken during the lecture
time).
  Any book on programming using C

– An e-learning course (Uses C as a demonstrative


language)
• Administrative Issues
– Meeting times shall be as in the time table unless agreed upon
by both parties
– Notes will always be taken in class, Students can also request
for a copy of the notes for photocopy at their own expense

• Assessment
– The continuous assessment test will carry a total of 30%, and
will be examined as follows:
– CAT One will account for 10%
– Written assignment will account for 10%
– Programming Project will account for 10%
– The University final Examination will account for 70%
Content

• 1. Introduction to problem • Pre-requisites:


solving
• 2. Structured programming • None
using C
• 3. Program structure.
• 4. Stepwise refinement. • Delivery:
• 5. Data types and • Lectures,Labora
expressions.
• 6. Control structures.
tory exercises.
• 7. Functions and Procedures.
• 8. Records and files.
Recursion.
• 9. Report and display design.
• 10. Library procedures.
Introduction
• Programming is the process of translating a
problem’s solution into instructions that a
computer can process.
• Many different languages used to program a
computer.
– Machine language- most basic.
• The very lowest possible level at which you can program
a computer is in its own native machine code, using
strings of 1's and 0's and stored as binary numbers.
– Problems –
» it is very easy to make a mistake, and very hard to find
it once you realize the mistake has been made.
» Machine code cannot be ported because each machine
has its own instruction set.
Introduction
– Assembly languages. Another example of language.
It is symbolic representation of machine code where
we have statements like:
– LOAD BASEPAY
– ADD OVERPAY
– STORE GROSSPAY

– High level languages


• Its instruction set is more compatible with the human
languages and the human thought process
• Highly portable programs
• Needs translation into machine language before
execution –this is called compilation using compiler or
interpretation
Introduction
Computer languages

COMPUTER WHEN EXAMPLES


LANGUAGE INTRODUCED

Machine Language 1940s 0s and 1s only

Symbolic Language Late 1950s Assembly Language

High Level Mid to Late 1960s PL1, BASIC,


Languages Fortran, C
Natural Languages Soon ?
Program Development Environment:
Software resources
• Compilers –development software that convert the whole
source code into executable code that may be run several times.

• Text Editors –allow the user to create and maintain ASCII


texts. They are the most suitable means of writing programs.

• Interpreters –accepts source code but execute each line of


code by converting it to executable form, executes it, discards
the executable code then moves to the next until all lines are
executed. Examples include BASIC, APL.

• Debuggers –assist the process of error correction in an


environment where the compiler is used.
Introduction

• Because C is a Structured Language, it is an acceptable choice


for beginners.
• C is Structured
– Forces users to obey strict rules about how a
program can be created.
• C is Efficient
– C programs are comparatively small and fast
• C is Powerful and Flexible
• Shortcomings of C
– Because of its power and conciseness, it is possible
to create C programs that are difficult to follow.
Introduction to Computers

• A computer is made up of
– Hardware - the actual physical equipment
• Hardware has five distinct components
– Software -a collection of programs (computer
instructions) which allow the hardware to function.
• There are two types of software.
Introduction to Computers
Hardware components
• CPU – the brain of the computer, the CPU (central
processing unit) performs all the instructions
• Primary Storage – also known as Main Memory, used
by the computer to store information and instructions
while the machine is operating.
• Secondary Storage –used by the computer to store
information long term. Powering off a computer does
not affect data in Secondary storage. Example hard
drive
• Input Devices –allow users to enter data into the
computer. The most common are a keyboard and mouse
• Output Devices – output devices allow computer to
pass information to the user. Common ones include the
printer and monitor.
Introduction to Computers
H/W components
Introduction to Computers
H/W components
Introduction to Computers
Software types
• System Software – system software helps
the system run. System software includes
Operating Systems, System Support Software
and System Development Software

• Application Software – application software


includes General Purpose Software and
Application Specific Software
Introduction to Computers
Computing environments
• PC Environment- a computer that sits on its own, or may
be connected to the Internet as a peer

• Client Server Environment – Client and server machines

• Timeshare Environment- Users don’t work on PCs, but


work on what are called terminals. Terminals can do
input/output, but all the computing/printing etc is done by
the main server.
Introduction
Want to be a computer programmer??

• What exactly is a computer program?


• What does a computer programmer actually
do?
• How are computer programs created?
Introduction
System Development Life Cycle (SDLC)
– The SDLC is a very high-level set of instructions
that are followed in order to produce a program or
system in the most efficient way. Example
– 1. System Requirements (understand the problem
and create a high level solution)
– 2. Analysis (understand what is needed to create the
system and see if it’s technically possible and
economically beneficial)
– 3. Design (break the problem down into simple
components)
– 4. Development (also known as coding, translate the
simple components into computer code)
– 5.Test (test the working solution and fix the problems)
– 6. Maintain (release the product to the end-user, and
Problem Solving and Programming

– Good programmers, or rather true programmers,


are problem solvers.

– People solve problems every day, but programmers


must take the solution they create and break it
down into its simplest steps.

– Only when a problem’s solution has been broken


down this way can the solution be translated into
computer code.
Introduction
The general steps used in computer based problem solving

1. Identify the problem – decide on what the program


should do.
2. Formulate and represent a procedure for solving the
problem without the use of a computer (algorithm).
3. Represent the algorithm using any acceptable
convention (flowcharts or pseudo codes).
4. Check that the algorithm works.
5. Write a computer code from the algorithm.
6. Test the computer code and complete its
documentation (written material explaining the code
and the program). Produce the final program.
7. Run or use the computer program.
ALGORITHMS

• Is the series of steps that are used to solve a


programming problem
• a plan for solving a programming problem
that specifies the sequence of steps used in
the solution.
• 1. An algorithm must have a start and an end
point.
• 2. Every step must be doable or performable:-
(a) using a finite amount of effort;
– (b) in a finite amount of time.
Formulation of algorithms is a major step in
writing computer programs.
Introduction
Three Steps to Programming

– Good programmers write programs in three steps.


1.Solve the Problem
» This is done by creating a solution that takes what we
have (data), and manipulates it so that it produces
what we want (information).
2.Simplify the Solution
» This is the most difficult of the three steps. What we
try to do here is break down the solution into steps so
simple that they can not be broken down any further.
3.Code the Solution
» This is the easy step, if step 2 was done properly. We
simply take our simplified solution and translate it into
computer code.
» The Beginner’s Biggest Mistake
» Tend to skip step 2, or do it poorly
Representing algorithms
• The most common means of representing
algorithms is by the use of flowcharts or
pseudo codes.
ALGORITHMS …
Start/ Stop

Start Stop

Input/ Output

Read Write
Algorithms …
Process box
 Use not more than two statements in each box.
Sum:= Sum + 1;
Count:= Count + 1;
ALGORITHMS …
Decision/selection box
ALGORITHMS …
Direction line (next step)

 Natural left to right (no arrow indicator)

 Natural top to down (no arrow


indicator)

 Other directions are as


indicated:
ALGORITHMS …

Preparation box Connection circle


ALGORITHMS …
PSEUDO CODE
Pseudo codes are mixture of English statements and terms
that are used in programming. The programming terms may
include:
START, BEGIN, END, STOP, IF, THEN, WHILE,
DOWHILE, ENDWHILE, REPEAT, FOR, UNTIL,
DISPLAY, WRITE, READ, etc.
Algorithms…
STRUCTURED PROGRAMMING CONSTRUCTS
 Programming constructs are the building blocks of a program.
There are only three programming constructs.
 These are sequence, repetition, selection. These are considered
below:-
Start:
process A;
process B;
Process
Stop:
Algorithms …
REPETITION
These are steps that must be performed more than once.
DO WHILE

Start:
DO WHILE:
Process;
Do exit action;
ENDWHILE:
Algorithms …
REPETITION
These are steps that must be performed more than once.

Start:
REPEAT:
Process
Until condition adjustment
UNTIL condition holds
Algorithms …
DECISION/ SELECTION

IF condition holds
THEN perform Process
IF condition THEN
…. ELSE …
Algorithms
EXAMPLES OF ALGORITHMS
Algorithm to reads in, displays and exchanges integer values of
two variables.
Say Variable_A = 500 while Variable_B = 300.
Hint use temporary.
1. Start
2. Set up variables
3. Initialize variables
4. Read in value of Variable_A
5. Read in value of Variable_B
6. Display value of Variable_A
7. Display value of Variable_B
8. Copy value of Variable_A into Temporary
9. Copy value of Variable_B into
Variable_A
10.Copy value of Temporary into Variable_B
11.Display value of Variable_A
12.Display value of Variable_B
13.Stop
Algorithms
(Algorithm that reads in, displays and exchanges integer values of two
variables.)
Check if the algorithm works
Algorithms
Algorithm that reads in, displays and exchanges integer values of two
variables.
Pseudo Code
START
VARIABLES Variable_A, Temporary, Variable_B
Variable_A:=0; Temporary:=0; Variable_B:=0;
READ Variable_A;
READ Variable_B;
WRITE Variable_A;
WRITE Variable_B;
Temporary:= Variable_A;
Variable_A:=Variable_B;
Variable_B:=Temporary;
WRITE Variable_A;
WRITE Variable_B;
STOP.
Start
Algorithms
Algorithm That reads in,
displays and exchanges Variables Variable_A:=0
integer values of two :
Variable_A
Variable_B:=0

variables. Variable_B
Temporary

Flow chart Temporary:=0


Read
Variable_A
Read
Variable_B

Display
Variable_A
Display Temporary := Varible_A :=
Variable_B Varibale_A Varible_B;

Display
Variable_A Varibale_B :=
Display Temporary
Variable_B

Stop
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
Planning
 Track the number of times reading is done by setting up a
counter.
 When 6 numbers have been read, stop.
Algorithm
1. Start
2. Setup variables: Number, Count
3. Initialize variables: Number:=0; Count:=0;
4. Read Number
5. Display Number
6. Increase Count by 1
7. If Count<6 Repeat from 4
8. Stop
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
Pseudo Code
START
Variables: Number, Count
Number:=0;
Count:=0;
REPEAT
Read Number
Display Number
Increase Count by 1
UNTIL Count>6
STOP
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
START
Variables: Number, Count
Number:=0;
Count:=0;
DO WHILE Count<6
Read Number;
Display Number;
Count := Count + 1;
ENDWHILE
STOP
Algorithms
An Algorithm That Reads
& Displays A Set Of 6
Numbers
Flow Chart
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Let the numbers be 5,3,4,5,10,30 but they could be any numbers
ALGORITHM
1. Start
2. Set up variables: Sum, Count, Number, Number_of_items
3. Initialize variables ( Sum:=0; Count:=0; Number:=0)
4. Read Number_of_items
5. Read a Number
6. Add 1 to count
7. Add Number to Sum
8. If Count <Number_of_items then repeat from 5
9. Display Sum
10. Stop
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Algorithms
An Algorithm To
Compute The Sum Of N
Numbers
Flowchart
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Pseudo code
1. START
2. Setup variables: Sum, Count, Number, Number_of_items
3. Initialize Variables to 0
4. READ Number_of_items
5. REPEAT
6. READ Number
7. Add 1 to Count
8. Add Number to Sum
9. UNTIL Count> Number_of_items
10. DISPLAY Sum
11. STOP
Algorithms
Algorithm For Computing The Factorial Of A Number Less Than
12
Planning:
0!=1
1!=1
2!=1x2
3!=1x2x3
4!=1x2x3x4
10!=1x…x9x10
n!=1x…x(n-1)xn
Algorithms
 Algorithm For Computing The Factorial Of A Number Less
Than 12
Algorithm
1. Start
2. Setup variables: Count, Number, Factorial
3. Initialize variables: Count:=0;Number:=0;Factorial:=1;
4. Read a Number
5. Check exceptions: Number<0(advice and ask again);
Number>12(advice and ask again), Number:=0 or 1 (Factorial:=1);
6. If Number<2 then proceed to 10
7. Add 1 to Count
8. Factorial := Factorial * Count
9. If Count<Number Repeat from 7
10. Display Factorial
11. Stop
Algorithms
Algorithm For Computing The Factorial Of A Number
Less Than 12
Algorithms
Algorithm For Computing The Factorial Of A Number
Less Than 12
flowchart
Algorithms
Pseudocode for the factorial computation algorithm
1. START
2. Setup variables: Count, Number, Factorial
3. Initialize variables: Count:=0; Number:=-10; Factorial:=1;
4. DO WHILE (Number <0 or Number>12)
5. PROMPT ‘Enter a number between 0 and 12’
6. READ Number
7. If Number <0 or Number>12 WRITE ‘Number should be 0..12’
8. ENDWHILE
9. DO WHILE Number>1 and Count<Number
10. Count := Count + 1
11. Factorial := Factorial * Count
12. ENDWHILE
13. WRITE Factorial
14. STOP
Introduction
Arrays

• Semantic features: storage locations; sequential;


indexed;
• Arrays are used to store a list of data items of the
same type.

• Array Structure
VALUE 50 -199 300 200 100 60
VALUES
INDEX 1 2 3 4 5 6

• Structural features: Name eg. VALUE; Size[6], Index range


eg.Data type e.g. Int [6];.
VALUE 50 -199 300 200 100 60
• Representation of Arrays
INDEX 1 2 3 4 5 6
Representation VALUE[1] VALUE[2] VALUE[3] VALUE[4] VALUE[5] VALUE[6]
Introduction
Arrays

• Values are represented in arrays using the name


and index
• Describing an array
– Example: C version version: int VALUE [6] ;
– Pascal version VALUE:array[1..6] of integer
• This means that VALUE may only be used to store
integer numbers.
• Setting up an array
• To set up an array, the following considerations should be
made:
– Data type of contents- real, integer, strings are some basic
data types.
– The size of the array- maximum number of values that an
array will be allowed to hold.
Introduction
Arrays
Errors to avoid
• Using an index value outside the range
• Using a value of a different data type
• Examples of arrays
• INT_NUMBERS
VALUE 20 -3 4 12 10 30
INDEX 1 2 3 4 5 6

• REAL_NUMBERS
VALUE 5. -19.9 30.0 200.8 10.90 7.60
1
INDEX 1 2 3 4 5 6

ADDRESS VALUE Box 50, Box 30, Box 24, Kisumu Box 7, Mombasa
Nyeri Nakuru

INDEX 1 2 3 4
Introduction
Operations on Arrays

• Manipulating arrays of activities carried out


on arrays
• Accessing values of arrays

– Reading values into arrays


• Single cell (Scanf(arrayname[index]))
• Multiple cell (Repetition)

– Displaying values of an array


• Single cell (Printf(arrayname[index]))
• Multiple cell (repetition)
Introduction
Examples of algorithms that use Arrays

• Algorithm to determine and display the maximum and


minimum values of numbers held in an array
• Int_numbersVALUE 20 -3 4 12 10 30

INDEX 1 2 3 4 5 6

• Processing steps
• Set index to the first cell value index
• Make the first cell value both the maximum and the
minimum
• Repeat
– Move to the next cell by increasing the index
– Take a cell value
– Examine if it is maximum and set accordingly
– Examine if it is minimum and set accordingly
• Until All values are examined
Introduction
Examples of algorithms that use Arrays
Algorithm (above)
1. Start
2. Set variables: Maximum, Minimum, Index, Size; Assume: int
INT_NUMBERS[6]
3. Initialize variables: Maximum=0; Minimum=0; Index=1,
Size=0
4. Maximum= INT_NUMBERS[1]
5. Minimum= INT_NUMBERS[1]
6. Size=6
7. Index=Index+1
8. IF INT_NUMBERS[Index]> Maximum THEN Maximum=
INT_NUMBERS[Index]
9. IF INT_NUMBERS[Index]< Minimum THEN Minimum=
INT_NUMBERS[Index]
10. IF Index<Size THEN repeat from 7
11. Display Maximum, Minimum

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy