Vous êtes sur la page 1sur 35

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

UNDERSTANDING THE PROBLEM


First:
What is the unknown? What are the
You have to data?What is the condition?
understand Is it possible to satisfy the condition?Is the
the problem. condition sufficient to determine the
unknown?Or is it sufficient?Or Redundant?
Or Contradictory?
Draw 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

DEVISING A PLAN
Second: Have you seen it before? Or have you
Find the connection seen the same problem in slightly different
between the data and form?
the unknown. Do you know a related problem?
Auxiliary problems Look at the unknown! Try to think of a
may be devised if familiar problem having the same or similar
needed. unknown. Split the problem into smaller,
You should obtain simple sub-problems. If you cannot solve
eventually a plan of the proposed problem try to solve first
the solution. some related problem. Or solve more
general problem. Or special case of the
problem. Or solve the part of the problem.

CT018-3-1 Introduction To C Programming Program Design


Problem Solving

Third: CARRYING OUT THE PLAN

Carry out your Carrying out your plan of the solution,check


plan. each step. Can you see clearly that step is
correct? Can you prove that it is correct?
Fourth: LOOKING BACK

Examine the Can you check the result? Can you derive
solution the result differently? Can you use the
obtained. result, or the method, for some other
problem?

CT018-3-1 Introduction To C Programming Program Design


Algorithm

Algorithmic problem:
Any problem whose solution can be
expressed as a set of executable instructions.

Algorithm:
A well defined computational procedure
consisting of a set of instructions, that takes
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
Characteristics of an Algorithm

• Each step of an algorithm must be exact, 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

Now 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 Output

Value1 Sum
Value2

CT018-3-1 Introduction To C Programming Program Design Slide 26 (of 80)


Problem Solving

The next step is to identify the actual processing


steps required to convert the input to become the
output.

Input Processing Output

Value1 1) Read Value1, Value2 Sum


Value2 2) Calculate Sum
3) Display Sum

CT018-3-1 Introduction To C Programming Program Design Slide 27 (of 80)


Algorithm Development
Once the defining diagram has been
developed, the next logical step is to develop
the algorithm (which is much more detailed).

Input Processing Output

Value1 1) Read Value1, Value2 Sum


Value2 2) Calculate Sum
3) Display Sum

The developed processing steps have to be more


detailed in the algorithm.
CT018-3-1 Introduction To C Programming Program Design Slide 28 (of 80)
Operators

The basic mathematical operators used in algorithms


are as follows:-

+ addition
- subtraction
* multiplication
/ division
= assignment
() brackets for grouping calculations

CT018-3-1 Introduction To C Programming Program Design Slide 29 (of 80)


Algorithm Development

Example of an algorithm (using pseudocodes) which


can be used to carry out the tasks outlined in the
defining diagram is as follows:-

1) Read Value1, Value2


2) Calculate
Sum = Value1 + Value2
3) Display Sum
CT018-3-1 Introduction To C Programming Program Design Slide 30 (of 80)
Pseudocoding

A Pseudocode language is semiformal, English-like


language with a limited vocabulary that can be used
to design and describe algorithms.

The pseudocode language can be used for:


 Designing algorithms
 Communicating algorithms as programs
 Implementing algorithms as programs
 Debugging logic errors in program

CT018-3-1 Introduction To C Programming Program Design Slide 31 (of 80)


Program Flowcharts

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, makes understanding
easier.

CT018-3-1 Introduction To C Programming Program Design Slide 62 (of 80)


Flowcharting

 Another technique used in designing and representing


algorithms.
 Alternative to pseudocoing
 A pseudocode description is verbal, a flowchart is
graphical in nature.

Definition:
 A flowchart is a graph consisting of geometrical shapes
that are connected by flow lines.

CT018-3-1 Introduction To C Programming Program Design Slide 66 (of 80)


Program Flowcharts
The following are the commonly used
symbols for drawing program flowcharts.
terminator off-page
connector

process storage

decision document
making

input/output connector

arrowheads
CT018-3-1 Introduction To C Programming Program Design Slide 63 (of 80)
Pseudocode for the Control Structures

The Sequence Control Structure:


The sequence control structure is a series of steps
or statements that are executed in the order in which
they are written in an algorithm.
For Example:
read taxable income
read filing status
compute income tax

CT018-3-1 Introduction To C Programming Program Design Slide 32 (of 80)


Decision Making

The Selection Control Structure:


The selection control structure defines two courses of
action, depending on the outcome of a condition. A
condition is an expression that, when evaluated, computes
to either true or false.
Syntax is:
if condition (Condition)
False True
then-part
else
Action_2 Action_1
else-part
end-if

CT018-3-1 Introduction To C Programming Program Design Slide 33 (of 80)


Decision Making

Commonly used relational operators in expressions:-

> Greater Than


< Less Than
= Equals To
<> Not Equals To
>= Greater Than or Equals To
<= Less Than or Equals To
() Brackets used for prioritising certain calculations

CT018-3-1 Introduction To C Programming Program Design Slide 37 (of 80)


Decision Making

Compound expressions can be represented


using the following operators:-

AND Every expression must evaluate to be


true in order for the whole expression to
be true.
OR As long as any one of the expression
can be true, the entire IF statement will
be true.
NOT The inverse (opposite) of the entire
expression.
CT018-3-1 Introduction To C Programming Program Design Slide 40 (of 80)
Decision Making - Example

A potential employer is waiting for you to give a reply (on the


spot) about the job offer with a salary of RM2000. Your
decision would be to only take a job worth more than
RM4000. What would you say?
IF (Salary>4000) THEN
Say “YES!”
ELSE
Say “NO!”
ENDIF

CT018-3-1 Introduction To C Programming Program Design Slide 38 (of 80)


Decision Making - Example

Certain conditions may give rise to more than


one expression being evaluated. These are
known as compound expressions.
E.g. You are interested in taking up a job which
pays more than RM4000 and that the company
must also provide a credit card.
IF (Salary>4000) And (CreditCard=YES) THEN
Take Job!!
ENDIF

CT018-3-1 Introduction To C Programming Program Design Slide 39 (of 80)


Repetition

Repetition Control Structure:


The repetition control structure specifies a block of
one or more statements that are repeatedly executed
until a condition is satisfied.
Syntax is:
while condition
loop-body
end-while

CT018-3-1 Introduction To C Programming Program Design Slide 45 (of 80)


Looping Constructs

Looping constructs (also known as repetition or


iteration constructs) are a kind of construct found
in pseudocodes which allows statements (or a
group of statements) to be repeated.
The main reason why looping constructs are
provided is because most of the problems which
we encounter everyday requires some degree of
repetition.

CT018-3-1 Introduction To C Programming Program Design Slide 46 (of 80)


Looping Constructs

The looping constructs available in pseudocodes


are as follows:-

DOWHILE...ENDDO
FOR…NEXT
REPEAT...UNTIL

CT018-3-1 Introduction To C Programming Program Design Slide 48 (of 80)


Looping Constructs

The format of the DOWHILE...ENDDO construct is shown


below:-

DOWHILE (expression) Initialization

:
:
: (Condition) True

ENDDO False
Action_1

An expression which
Group of determines whether Incrementation /
Decrementation

statements the loop will


continue. Slide 49 (of 80)

CT018-3-1 Introduction To C Programming Program Design


Looping Constructs

The format of the FOR...NEXT construct is shown below:-


FOR (initialze TO expression) STEP increment
: VN
: in end
:
incre
NEXT

An expression which determines


whether the loop will continue. Action_1

Group of statements VN – variable_name


in – initial value
VN
end – ending value
incre - incrementation
CT018-3-1 Introduction To C Programming Program Design Slide 50 (of 80)
Looping Constructs

The format of the


REPEAT...UNTIL construct is
shown below:-

REPEAT
:
:
:
UNTIL (expression)

Group of An expression which


statements determines whether the loop
will continue.

CT018-3-1 Introduction To C Programming Program Design Slide 51 (of 80)


Example
Dowhile… Enddo

Dowhile (income is less than 50000)


print “Enter taxable income;
should be greater than or equal
to 50000”
read income
Enddo

CT018-3-1 Introduction To C Programming Program Design Slide 55 (of 80)


Array

• Contiguous, homogeneous collection of data


values that share a common name.
• Element - one value in an array.
• Index (subscript) - an integer indicating a
position in an array. Array indexes start with 0.
0 1 2 3 4 5 6 7 8
10 20 33 44 15 6 17 88 29

CT018-3-1 Introduction To C Programming Program Design


Array - Example

• Replace every third element in the following


array with value 10.
• Note: Name: number; Size (elements) = 8
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
3 4 2 3 5 7 1 7 10 4 2 10 5 7 10 7

FOR (index = 0 TO 7) STEP 3


number[index] = 10
NEXT index
or
Set index to 0
FOR every third element in the number
replace existing value with 10
increase index by 3
ENDFOR
CT018-3-1 Introduction To C Programming Program Design
Design (Flowchart)
Begin

Read Value1,
Value2

Calculate
Sum = Value1 + Value2

Display
Sum

End

CT018-3-1 Introduction To C Programming Program Design Slide 64 (of 80)


Design (Flowchart)
Begin

Read Amount

YES NO
Amount>20.00?

Calculate Calculate
Actual=Amount * 0.80 Actual=Amount

End
CT018-3-1 Introduction To C Programming Program Design Slide 65 (of 80)
Summary

• An algorithm is a sequence of a finite number of steps


arranged in a specific logical order that, when executed,
produce the solution for a problem.

• A pseudocode language is a semiformal,English-like


language with a limited vocabulary that can be used to
design and describe algorithms.

• A Flowchart is a graphical representation of an


algorithm.

CT018-3-1 Introduction To C Programming Program Design Slide 74 (of 80)

Vous aimerez peut-être aussi