Sei sulla pagina 1di 35

Introduction to C Programming

Program design
CT018-3-1 Introduction To C Programming Program Design
Topic & Structure of the lesson
In this chapter you will learn about:

Problem Solving

Algorithm

Pseudocodes

Flowcharts
CT018-3-1 Introduction To C Programming Program Design
Learning Outcomes
If you have mastered this topic, you should be able
to use the following terms correctly in your
assignments:

Algorithm

Pseudocode

Flowchart
CT018-3-1 Introduction To C Programming Program Design
Problem Solving

First:
ou have to
understand
the problem!
U!"#ST$!I% T&" P#O'L"(
"hat is the un#nown$ "hat are the data$
"hat is the condition$
Is it possible to satisfy the condition$Is the
condition sufficient to determine the
un#nown$%r is it sufficient$%r &edundant$
%r Contradictory$
'raw a figure!Introduce suitable
notation!Separate the various parts of the
condition!Can you write them down$
CT018-3-1 Introduction To C Programming Program Design
Problem Solving

Second:
Find the connection
between the data and
the un#nown!
Au(iliary problems
may be devised if
needed!
ou should obtain
eventually a plan of
the solution!
!")ISI% $ PL$
)ave you seen it before$ %r have you
seen the same problem in slightly different
form$
'o you #now a related problem$
*oo# at the un#nown+ ,ry to thin# of a
familiar problem having the same or similar
un#nown! Split the problem into smaller,
simple sub-problems! If you cannot solve
the proposed problem try to solve first
some related problem! %r solve more
general problem! %r special case of the
problem! %r solve the part of the problem!
CT018-3-1 Introduction To C Programming Program Design
Problem Solving
,hird:

Carry out your
plan!
*$##+I% OUT T&" PL$
Carrying out your plan of the solution,chec#
each step! Can you see clearly that step is
correct$ Can you prove that it is correct$
Fourth:
.(amine the
solution
obtained!
LOO,I% '$*,
Can you chec# the result$ Can you derive
the result differently$ Can you use the
result, or the method, for some other
problem$
CT018-3-1 Introduction To C Programming Program Design
$lgorithm
Algorithmic problem:
Any problem whose solution can be
e(pressed as a set of e(ecutable instructions!
Algorithm:
A well defined computational procedure
consisting of a set of instructions, that ta#es
some value or set of values, as input, and
produces some value or set of values, as
output.
CT018-3-1 Introduction To C Programming Program Design
*haracteristics of an $lgorithm
.ach step of an algorithm must be e(act, preciously
and ambiguously described!
It must terminate, i!e! it contains a finite number of
steps!
It must be effective, i!e!!, produce the correct output!
It must be general, i!e!! to solve every instance of the
problem! An Algorithm is implemented in some
programming language.
program = Algorithm + Data Structures.
CT018-3-1 Introduction To C Programming Program Design
Problem Solving
Slide 26 (of 80)
/ow that we have an exact idea about how the
problem is solved, let us represent this in a clearer
manner, using the defining diagram!
Input Processing %utput
0alue1
0alue2
Sum
CT018-3-1 Introduction To C Programming Program Design
Problem Solving
Slide 2 (of 80)
,he ne(t step is to identify the actual processing
steps re3uired to convert the input to become the
output!
Input Processing %utput
0alue1
0alue2
Sum 14 &ead 0alue1, 0alue2
24 Calculate Sum
54 'isplay Sum
CT018-3-1 Introduction To C Programming Program Design
$lgorithm !evelopment
Slide 28 (of 80)
%nce the defining diagram has been
developed, the ne(t logical step is to develop
the algorithm 6which is much more detailed4!
Input Processing %utput
0alue1
0alue2
Sum 14 &ead 0alue1, 0alue2
24 Calculate Sum
54 'isplay Sum
,he developed processing steps have to be more
detailed in the algorithm!
CT018-3-1 Introduction To C Programming Program Design
Operators
Slide 2! (of 80)
,he basic mathematical operators used in algorithms
are as follows:-
7 addition
- subtraction
8 multiplication
9 division
: assignment
6 4 brac#ets for grouping calculations
CT018-3-1 Introduction To C Programming Program Design
$lgorithm !evelopment
Slide 30 (of 80)
.(ample of an algorithm 6using pseudocodes4 which
can be used to carry out the tas#s outlined in the
defining diagram is as follows:-
14 &ead 0alue1, 0alue2
24 Calculate
Sum : 0alue1 7 0alue2
54 'isplay Sum
CT018-3-1 Introduction To C Programming Program Design
Pseudocoding



Slide 31 (of 80)
A Pseudocode language is semiformal, .nglish-li#e
language with a limited vocabulary that can be used
to design and describe algorithms!
,he pseudocode language can be used for:

'esigning algorithms

Communicating algorithms as programs

Implementing algorithms as programs

'ebugging logic errors in program


CT018-3-1 Introduction To C Programming Program Design
Program -lowcharts
Slide 62 (of 80)
As humans are more inclined towards
understanding diagrams and pictures rather
than words, pseudocodes tends to become
tedious to understand if too lengthy!
Program flowcharts, because they are
represented graphically, ma#es understanding
easier!
CT018-3-1 Introduction To C Programming Program Design
-lowcharting



Slide 66 (of 80)
Another techni3ue used in designing and representing
algorithms!
Alternative to pseudocoing
A pseudocode description is verbal, a flowchart is
graphical in nature!
!efinition:
A flowchart is a graph consisting of geometrical shapes
that are connected by flow lines!
CT018-3-1 Introduction To C Programming Program Design
Program -lowcharts
Slide 63 (of 80)
,he following are the commonly used
symbols for drawing program flowcharts!
terminator
off-page
connector
process storage
decision
ma#ing
document
input9output connector
arrowheads
CT018-3-1 Introduction To C Programming Program Design
Pseudocode for the *ontrol Structures



Slide 32 (of 80)
The Se.uence *ontrol Structure:
,he sequence control structure is a series of steps
or statements that are e(ecuted in the order in which
they are written in an algorithm!
For .(ample:
read taxable income
read filing status
compute income tax
CT018-3-1 Introduction To C Programming Program Design
!ecision (a/ing



Slide 33 (of 80)
The Selection *ontrol Structure:
,he selection control structure defines two courses of
action, depending on the outcome of a condition! A
condition is an e(pression that, when evaluated, computes
to either true or false!
Synta( is:
if condition
thenpart
else
elsepart
endif
CT018-3-1 Introduction To C Programming Program Design
!ecision (a/ing
Slide 3 (of 80)
Commonly used relational operators in expressions:-
; <reater ,han
= *ess ,han
: .3uals ,o
= ; /ot .3uals ,o
;: <reater ,han or .3uals ,o
=: *ess ,han or .3uals ,o
6 4 >rac#ets used for prioritising certain calculations
CT018-3-1 Introduction To C Programming Program Design
!ecision (a/ing
Slide "0 (of 80)
Compound e(pressions can be represented
using the following operators:-
A/' .very e(pression must evaluate to be
true in order for the whole e(pression to
be true!
%& As long as any one of the e(pression
can be true, the entire IF statement will
be true!
/%, ,he inverse 6opposite4 of the entire
e(pression!
CT018-3-1 Introduction To C Programming Program Design
!ecision (a/ing 0 "1ample
Slide 38 (of 80)
A potential employer is waiting for you to give a reply 6on the
spot4 about the ?ob offer with a salary of &@2AAA! our
decision would be to only ta#e a ?ob worth more than
&@BAAA! "hat would you say$
IF 6Salary>40004 ,)./
Say C.S+D
.*S.
Say C/%+D
./'IF
CT018-3-1 Introduction To C Programming Program Design
!ecision (a/ing 0 "1ample
Slide 3! (of 80)
Certain conditions may give rise to more than
one e(pression being evaluated! ,hese are
#nown as compound expressions!
.!g! ou are interested in ta#ing up a ?ob which
pays more than &@BAAA and that the company
must also provide a credit card!
IF 6Salary;BAAA4 And 6CreditCard:.S4 ,)./
,a#e Eob++
./'IF
CT018-3-1 Introduction To C Programming Program Design
#epetition



Slide "# (of 80)
#epetition *ontrol Structure:
,he repetition control structure specifies a bloc# of
one or more statements that are repeatedly e(ecuted
until a condition is satisfied!
Synta( is:
while condition
loopbod!
endwhile
CT018-3-1 Introduction To C Programming Program Design
Looping *onstructs
Slide "6 (of 80)
*ooping constructs 6also #nown as repetition or
iteration constructs4 are a #ind of construct found
in pseudocodes which allows statements 6or a
group of statements4 to be repeated!
,he main reason why looping constructs are
provided is because most of the problems which
we encounter everyday re3uires some degree of
repetition!
CT018-3-1 Introduction To C Programming Program Design
Looping *onstructs
Slide "8 (of 80)
,he looping constructs available in pseudocodes
are as follows:-
'%")I*.!!!./''%
F%&F/.G,
&.P.A,!!!H/,I*
CT018-3-1 Introduction To C Programming Program Design
Looping *onstructs
Slide "! (of 80)
,he format of the '%")I*.!!!./''% construct is shown
below:-
'%")I*. 6expression4
:
:
:
./''%
<roup of
statements
An e(pression which
determines whether
the loop will
continue!
CT018-3-1 Introduction To C Programming Program Design
Looping *onstructs
Slide #0 (of 80)
,he format of the F%&!!!/.G, construct is shown below:-
F%& 6initialIe ,% e(pression4 S,.P increment
:
:
:
/.G,
<roup of statements
An e(pression which determines
whether the loop will continue!
0/ J variableKname
in J initial value
end J ending value
incre - incrementation
CT018-3-1 Introduction To C Programming Program Design
Looping *onstructs
Slide #1 (of 80)
,he format of the
&.P.A,!!!H/,I* construct is
shown below:-
&.P.A,
:
:
:
H/,I* 6expression4
<roup of
statements
An e(pression which
determines whether the loop
will continue!
CT018-3-1 Introduction To C Programming Program Design
.(ample
!owhile2 "nddo
Slide ## (of 80)
Dowhile (income is less than 50000)
print nter taxa!le income"
should !e greater than or e#ual
to 50000$
read income
nddo
CT018-3-1 Introduction To C Programming Program Design
$rray

Contiguous, homogeneous collection of data


values that share a common name!

lement - one value in an array!

%ndex 6subscript4 - an integer indicating a


position in an array! Array inde(es start with A!
3 4 5 6 7 8 9 : ;
1A 2A 55 BB 1L M 1N OO 2P
CT018-3-1 Introduction To C Programming Program Design
$rray 0 "1ample

&eplace every third element in the following


array with value 1A!

/ote: /ame: numberQ SiIe 6elements4 : O


3 4 5 6 7 8 9 :
5 B 2 5 L N 1 N
3 4 5 6 7 8 9 :
1A B 2 1A L N 1A N
F%& 6inde( : A ,% N4 S,.P 5
numberRinde(S : 1A
/.G, inde(
or
Set inde( to A
F%& every third element in the number
replace e(isting value with 1A
increase inde( by 5
./'F%&
CT018-3-1 Introduction To C Programming Program Design
!esign <-lowchart=
Slide 6" (of 80)
>egin
&ead 0alue1,
0alue2
Calculate
Sum : 0alue1 7 0alue2
'isplay
Sum
.nd
CT018-3-1 Introduction To C Programming Program Design
!esign <-lowchart=
Slide 6# (of 80)
>egin
&ead Amount
.nd
Amount;2A!AA$
Calculate
Actual:Amount 8 A!OA
Calculate
Actual:Amount
/%
.S
CT018-3-1 Introduction To C Programming Program Design
Summary
An algorithm is a se3uence of a finite number of steps
arranged in a specific logical order that, when e(ecuted,
produce the solution for a problem!
A pseudocode language is a semiformal,.nglish-li#e
language with a limited vocabulary that can be used to
design and describe algorithms!
$ -lowchart is a graphical representation of an
algorithm!
Slide " (of 80)

Potrebbero piacerti anche