Algorithms and Programming

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 17

ALGORITHMS AND PROGRAMMING TECHNIQUES

Introduction to Program Deveo!ment and


Pro"em Soution
Computers do what we tell them to do NOT what we want them
to do
Com!uter Programming involves writing instructions and
giving them to the computer to complete a task.
A com!uter !rogram or #o$t%are i#&
a set of instructions written in a computer language in order
to
be executed by a computer to perform a useful task
Ex Application software packages! such as word processors!
spreadsheets and databases are all computer programs
A Com!uter Programmer is
is a person who translates the task you want a computer to do
into a form that a computer can understand
A well designed com!uter !rogram must be
correct and accurate
easy to understand
easy to maintain and update
efcient
reliable
fexible
The !rogram deveo!ment !roce## involves the following
steps
document the program
program documentation is the text or graphics that provide
description of
the purpose of a function or a particular step
the purpose of instruction in a program
determine the user needs
design the program speci"cations
review the program speci"cations
design the algorithm
steps that will convert the available input into the desired output
step by step solution for a given problem is called the algorithm
a fowchart graphically details processing steps of a particular
program
code the program
write the program in a programming language using a program
editor
a program editor is a program that allows to type! edit and save a
program code on a disk
compile! test and debug the program
in order to "nd out possible errors in the program
types of errors may be syntax errors! run#time errors and logic
errors
get program to the user
install software on the users computer and ofer training
Types of !rogramming error#
Syntax of a programming language is the set of rules to be
followed when writing a program
syntax error occurs when these rules are violated
run-time errors occur when invalid data is entered during
program execution
e.g. program expects numeric data and alphabetic data is
entered
program will crash
logic error will not stop the program but results will be
inaccurate
The process of "nding and correcting errors in a program is
called debugging
$or #ucce##$u !rogramming
give no ambiguity in program instructions
give no possibility of alternative interpretations
make sure there is only one course of action
%rogramming task can be made easier
by breaking large and complex programs
into smaller and less complex subprograms &modules'
%rogramming task can be separated into ' !(a#e# &see $ig. ('
problem solving phase
produce an ordered se)uence of steps that describe solution of
problem
this se)uence of steps is called an algorithm
Example of an Algorithm a recipe! to assemble a brand new
computer ...
what else*
implementation phase
implement the program in some programming language &%ascal!
+asic! C'
)igure *& Pro"em Soving and Programming
,hat is #tructured !rogramming
a programming techni)ue that splits the program into smaller
segments &modules' to
decrease program development time
decrease program maintenance cost
improve the )uality of software
structured programming achieves these goals by using
top-down design and use of modules
use of limited control structures &se)uence! selection and
repetition'
management control
Top-down design starts with ma-or functions involved in a
problem
and divide them into sub#functions
until the problem has been divided as much as possible
Categories of programming languages are
machine
assembly
high#level
fourth#generation
"fth#generation
Machine language is
made up of binary (s and .s
this is the only programming language the computers can
understand
advantages of machine languages are
fast execution speed and e/cient use of main memory
disadvantages of machine languages are
writing machine language is tedious! di/cult and time consuming
Types of language-translator programs
assemblers
translates the program in assembly-language into machine-
language
compilers
translates high-level language program into machine-language
all at once
interpreters
translates high-level language into machine-language a ine
at a time
0a-or high-level languages are
FOT!"# $O%O&# '&()# %!S)$# '!S$!&# $# &)S'# 'rolog#
&ogo
FOT!"
FOmula T!"slator introduced in (123
4low#level languages
for use by scientists! engineers and mathematicians
well suited for complex numeric calculations
$O%O&
$Ommon %usiness Oriented &anguage
a programming language used for business data processing
designed to handle large data "les
'&()
'rogramming &anguage One created in (15.
general purpose language for powerful computations and
sophisticated data structures
today largely used in the oil industry
%!S)$
%eginners !llpurpose Symbolic )nstruction $ode created in
(15.
easy to learn interactive language on a time#sharing computer
'!S$!&
Named after +laise 'ascal and created in (15.
suited to both scienti"c and "le processing applications
designed to teach structured programming and top#down
design
$
6eveloped at +ell 7abs in (13.s
used advantages of both high#level and low#level languages
C gives programmers extensive control over computer
hardware
incorporates sophisticated control and data structures
C88 is the ob-ect oriented version of C
&)S'
is a language that processes symbol se)uences &lists' rather
than numbers
designed to handle data strings more e/ciently than others
'rolog
is a language for symbol processing
&ogo
is an interactive education oriented language
designed to teach inexperienced users logic and programming
techni)ues
includes list processing capabilities
employs a triangular ob-ect called turtle
to draw! animate and color images very simply
Ob*ect-Oriented 'rogramming &anguages +OO'&,
there are two main parts of a program! instructions and data
traditional prog. languages treat instructions and data as
separate entities
an OO%7 treats a program as a series of ob*ects and
messages
an ob*ect is a combination of data and instructions that work
on data
and is stored together as a reusable unit
messages are instructions sent between ob-ects
to )ualify as an OO%7 a language must incorporate concepts of
-. encapsulation
/. inheritance
0. polymorphism
encapsulation is
combining data and instructions into a reusable structures
encapsulation generates prefabricated components of a program
inheritance
is the ability of a programming language to de"ne a new ob-ect
that has all the attributes of an existing ob-ect
programmer inherits the existing ob-ect
writes the code that describes how the new ob-ect di9ers from the
existing one
polymorphism is
the ability of an ob-ect to respond to a message in many ways
for example! say you have a circle ob-ect and a s)uare ob-ect
each ob-ect has the same characteristics of drawing
when a drawing message is sent to a circle ob-ect it draws a circle
when a drawing message is sent to a s)uare ob-ect it draws a
s)uare
thus! each ob-ect has the same characteristics of drawing
but the characteristics is implemented di9erently
!dvantages of OO'&
code re#use rather than reinvention and adaptable code
these speed development : maintenance of applications and
reduce cost
;maltalk! Ob-ective#C and C88 are examples of OO%7
ALGORITHMS AND )LO+CHARTS
A typical programming task can be divided into < phases
'roblem solving phase
produce an ordered se)uence of steps that describe solution of
problem
this se)uence of steps is called an algorithm
Examples of Algorithm a recipe! to assemble a brand new computer
...
what else*
)mplementation phase
implement the program in some programming language
;teps in %roblem solving
$irst produce a general algorithm &=ou can use pseudocode'
>e"ne the algorithm successively to get step by step detailed
algorithm that is very close to a computer language.
'seudocode is an arti"cial and informal language that helps
programmers develop algorithms. %seudocode is very similar to
everyday English.

Example ( ,rite an algorithm to determine the students "nal
grade and indicate weather it is passing or failing. The "nal grade
is calculated as the average of four marks.
;olution
%seudocode Input a set of 4 marks
Calculate their average by summing and dividing
by 4
if average is below 50
Print !"I#$
else
Print P"%%$
6etailed Algorithm ;tep ( ?nput 0(! 0<!0@!0A
;tep < B>A6E C &0(80<80@80A'DA
;tep @ if &B>A6E E2.' then
%rint F$A?7G
else
%rint F%A;;G
T(e )o%c(art
Easier to convey ideas by picture than by words
E,am!e#&
?t is easier to show how to go somewhere on a map than
explaining
?t is easier to construct a toy if diagrams are shown
?t is easier to construct a new %C if diagrams are provided
Hence u#e !ictoria $ormat $or de#cri"ing an agorit(m
this is called a F&O1$2!T
A $lowchart
shows logic of an algorithm
emphasises individual steps and their interconnections
e.g. control How from one action to the next
standard fowchart symbols are shown below
)o%c(art&
Example ( ,rite an algorithm to determine the students "nal
grade and indicate weather it is passing or failing. The "nal grade
is calculated as the average of four marks.
P#eudocode Input a set of 4 marks
Calculate their average by summing and
dividing by 4
if average is below 50
Print !"I#$
else
Print P"%%$
6etailed Agorit(m ;tep ( ?nput 0(! 0<!0@!0A
;tep < B>A6E C &0(80<80@80A'DA
;tep @ if &B>A6E E2.' then
%rint F$A?7G
else
%rint F%A;;G
START
Input
M1,M2,M3,M4
GRADE=(M1+M2+M3+M4)/4
IS
GRADE<5
0
PRINT
PASS
PRINT
FAI
ST!P
"
N
TRA#E TA%E&
MAR' GRADE STAT(S
M1=)0 )0
M2=30 + 30
M3=40 + 40
M4=30 + 30 /4
= 45 FAI
Example < ,rite an algorithm and draw a Howchart to convert the
length in feet to centimeter.
P#eudocode Input the lenght in feet &#ft'
Calculate the length in cm &#cm' by multiplying #!(
with )0
Print #C*
Agorit(m ;tep ( ?nput 7ft
;tep < 7cm C 7ft x @.
;tep @ %rint 7cm
)o%c(art&
START
Input
*t
+, = *t - 30
P./nt
+,
ST!P
Example @ ,rite an algorithm and draw a Howchart that will read
the two sides of a rectangle and calculate its area.
P#eudocode Input the width &+' and #ength &#' of a rectangle
Calculate the area &"' by multiplying # with +
Print "
Agorit(m ;tep ( ?nput ,!7
;tep < A C 7 x ,
;tep @ %rint A
)o%c(art&
START
Input
0,
A = - 0
P./nt
A
ST!P
Example A ,rite an algorithm and draw a Howchart that will
calculate the roots of a )uadratic e)uation a,
<
8 b, 8 c C .
Iint d C s)rt &b
<
J Aac'! and the roots are ,
(
C &Jb 8 d'D<a and
,
<
C &Jb J d'D<a

P#eudocode Input the coe-cients &a. b. c' of the /uadratic
e/uation
Calculate the discriminant d
Calculate ,
(
Calculate x
<
Print x
0
and x
<
Agorit(m ;tep ( ?nput a! b! c
;tep < d = sqrt (b
2
4 x a x c)
;tep @ ,
(
C &Jb 8 d' D &< x a'
;tep A ,
<
C &Jb J d' D &< x a'
;tep 2 %rint ,
(
and ,
<
)o%c(art&
START
Input
1, 2, +
3 = 45.t(b
2
6 4 - a - c)
P./nt
x
1
1n3 x
2

ST!P
x
1
= (6b + d) / (2 - a)
X
2
= (6b 6 d) / (2 - a)
DECISION STRUCTURES
The expression AK+ is a logical expression
it describes a condition we want to test
if !3% is true +if ! is greater than %, we take the action on
left of
print the value of A &see $ig. <'
if !3% is false +if ! is not greater than %, we take the
action on right
print the value of + &see $ig. <'
I)-THEN-ELSE STRUCTURE
The structure is as follows
)f condition
then true alternative
else false alternative
endif
The algorithm for the Howchart in "gure < is as follows
)f !3%
then print !
else print %
endif
Iere F.G is called the relational operator. Table ( gives the possible
relational operators
/4
A7%
P./nt % P./nt A
" N
F/8u.9 2&
Reationa O!erator#
O!erator Meaning
. Greater t(an
/ Le## t(an
0 E1ua to

Greater t(an or
e1ua to

Le## t(an or e1ua


to

Not e1ua to

Example 2 ,rite an algorithm that reads two values! determines
the largest value and prints the largest value with an identifying
message.
Agorit(m ;tep ( Input LA7ME(! LA7ME<
;tep < if VALUE1 > VALUE2
then 0AN C LA7ME(
else 0AN C LA7ME<
endif
;tep @ Print (he largest value is$. *"1
)o%c(art&
MA: = ;A(E1
P./nt
The largest value is, MAX
ST!P
" N
START
Input
;A(E1,;A(E2
MA: = ;A(E2
/4
;A(E17;A(E2
NESTED I)S
One of the alternatives within an ?$JTIENJE7;E statement
may involve further ?$JTIENJE7;E statement
Example 5 ,rite an algorithm that reads t(ree numbers and
prints the value of the largest number.
Agorit(m ;tep ( Input N(! N<! N@
;tep < if N1>N2
then if N(KN@
then 0AN C N( ON(KN<! N(KN@P
else 0AN C N@ ON@KN(KN<P
endif
else if N<KN@
then 0AN C N< ON<KN(! N<KN@P
else 0AN C N@ ON@KN<KN(P
endif
endif
;tep @ Print (he largest number is$. *"1
)o%c(art& Dra% t(e 2o%c(art o$ t(e a"ove Agorit(m3
Example 3 ,rite and algorithm and draw a Howchart to
a' read an employee name &NA0E'! overtime hours worked
&OLE>T?0E'! hours absent &A+;ENT' and
b' determine the bonus payment &%A=0ENT'.
+onus ;chedule
OLE>T?0E J
&<D@'QA+;ENT
+onus %aid
KA. hours
K@. but A. hours
K<. but @. hours
K(. but <. hours
(. hours
R2.
RA.
R@.
R<.
R(.
Agorit(m ;tep ( Input NA0E!OLE>T?0E!A+;ENT
;tep < if OLE>T?0EJ&<D@'QA+;ENT K A.
then %A=0ENT C 2.
else if OLE>T?0EJ&<D@'QA+;ENT K @.
then %A=0ENT C A.
else if OLE>T?0EJ&<D@'QA+;ENT K <.
then %A=0ENT C @.
else if OLE>T?0EJ&<D@'QA+;ENT K (.
then %A=0ENT C <.
else %A=0ENT C (.
endif
endif
endif
endif
;tep @ Print F+onus forG! NA0E Fis RG! %A=0ENT
)o%c(art& Dra% t(e 2o%c(art o$ t(e a"ove agorit(m4

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