Anda di halaman 1dari 54

PROGRAM PLANNING AND DESIGN

STUDENT LEARNING OBJECTIVES:


At the end of the chapter, the students are
expected to:
To

be able to explain what a program is.


To identify the first steps in programming.
To define the meaning and purpose of program
logic formulation.
To distinguish the tools used in the program logic
formulation.

A computer is a tool used for processing data into


information. The processing is controlled by a set of
detailed step-by-step instructions.

The list of instruction that the computer must follow to


be able to process data into information is called
program. These instructions consists of statements
written in specific programming language.

When a program is written, we are basically solving a


problem. To solve problem, we need to use our mind
power or logic to developed the detailed instructions.

The procedure of developing the details


instruction for the computer is called
programming. It is a five-step process namely:
1. Define

the problem. (Program Definition)


2. Design the solution / program (Program Design)
3. Code the program (Program Coding)
4. Test the program ( Program Testing)
5. Document the program (Program
Documentation)

The problem definition step means that there should be a clear


understanding of the problem. It requires performing the following:
1.
Specify the program objectives and identify the users of the
program.
2.
Specify the output requirements.
3.
Specify the input requirements.
4.
Specify the processing requirements.
5.
Study feasibility of implementing the program.
6.
Document the analysis.
In designing the program, the following steps are done:
1.
Determine the program logic through top-down approach and
modularization, using hierarchy charts.
2.
Design details using pseudo codes and/or flowchart.
3.
Test design in a structural walk through. Structured walkthrough
consists of reviewing process with other programmers and system
analyst and scrutinized the programmers work.

Program coding is the step, which translates the logic of the program
based on the pseudo-code (algorithm) or flowcharts into a higher
level programming language using the syntax and semantics of the
specific language. The steps includes:
1.
Determine the appropriate programming langauge to use.
2.
Code the program into the specific programming language
decided on.
Program Testing consists of desk checking, debugging and running
real data to make sure that the works. Desk checking is just
reading through or checking the program manually to ensure it is
free from errors and logic works. Debugging means detecting,
locating and removing errors in computer program. Errors may be
syntax errors and logic errors.
Syntax errors can be
typographical error or incorrect format of the statement used.
Logical error are caused by incorrect use of control structures.

Documenting the program consists of the written description of what


the program is all about and how to use it. The documentation
should prepare for the users, operators, and the programmers. The
steps to follow:
1.
2.

3.

Write user documentation. This is the manual that prepared to


help the user use the program.
Write the operator documentation. This is the manual for
computer operator on what to do when the program flashes an
error message.
Write the program documentation. The program documentation
consists of testing of the whole program. This documentation help
train new programmers to maintain existing system. Maintenance
here means keeping the program in working conditions, error free
and up to date.

Program Logic Formulation is a step-by-step development of a


solution to a given problem. This is actually the most difficult
part of solving a problem with a computer.
After you come up with a method of solution, it is a routine to
translate your method into the required language.
There are two most commonly used tools that will be of great
help for you to formulate a better solution. These are the
flowchart and algorithm.
TOOLS USED IN THE DEVELOPMENT OF PROGRAM SOLUTION
1.
FLOWCHART
The first tool is a flowchart. It consists of different shapes
that contain simple statements or sentence commonly
called instructions and connect together by lines. It is easy
to understand because the solution is represented in
picture.

FLOWCHART SYMBOLS

Symbols used in Flowcharts


Data - Indicates Data
Stored Data - Indicates data stored in formats suitable for
processing
Sequential Access Storage - Indicates only sequentially accessible
data such as those on magnetic tapes & cassette tapes
Direct Access Storage - Indicates directly accessible data, such as
those on magnetic disks & floppy disks
Documents - Indicates data on media human beings can read
Display - Indicates data on any medium that displays information
used by human beings, such as monitor
Loop Limit - Composed of two parts; condition for termination and
loop name are written in the symbol indicating the start & the end
of the loop respectively

Terminal Symbol - This oval, which contains either the word


start or the word stop, indicates the starting and stopping
point in the flowchart.
A flowchart can only have one starting point but it may
have many possible stopping points.
INPUT/ OUTPUT Symbol - This parallelogram is used for
INPUT and OUTPUT instruction.
INPUT means to enter data into a computer or to use the
computer to read the available data from magnetic disk.
OUTPUT means to display the result of compilation or
condition of the given solution.
Processing Symbol - This rectangle is used for ay data
processing operations for which requirements of the
problem are usually done here. Examples are computation
of average, and assignment of the value to the variable.

Decision Symbol - This diamond-shaped symbol contains a


condition. If the condition is true, the marked path TRUE is
to be followed. If the condition is false, the marked path
FALSE is to be followed.
Arrowheads/ Flow lines - Arrows are used to direct the
flow of the flowchart. It shows the flow of the solution.
Connector - It is used as a continuation symbol of a
flowchart. It shows the flow of the solution.
Initialization/ Preparation Symbol - It is used to initialize
variables not explicitly defined in the input but a
requirement in the production of the output.

TYPES OF FLOWCHARTS
1.
System flowchart indicates a flowchart for targeted system as a
whole
2.
Program flowchart - used to describe processing based on detailed
analysis.
FLOWCHART DEVELOPMENT
The steps that comprise a flowchart must be organized in an orderly,
understandable and clear manner so that the program that
implements this flowchart is similarly in an orderly, understandable
and clear manner.
An orderly, understandable and clear solution is called Structured
Flowchart. Structured flowchart are designed using the three most
basic methods of control. The basic methods will produces correct
structured flowchart necessary to prepare a correct program.
1. SEQUENTIAL steps are performed in a strictly sequential manner,
each step being executed exactly once. This is the simplest method
of control. Easy to develop and understand.

2. SELECTION STRUCTURE one of several alternative actions is


selected and executed. It involves the use of decision based on the
given condition. It uses decision block or the diamond-shaped
block. Alternative action will be represented by a processing block.
3. REPETITION STRUCTURE one or more steps are performed
repeatedly.

Algorithm is a set or series of instruction for carrying a particular


task. It is also a procedure to produce the required output from
the given input. It is similar with a paragraph that expresses a
single thought. It uses English language. It is also known as
pseudocode.
Algorithm Development
The steps that comprise an algorithm are similar with flowchart. It
must be organized in orderly, understandable, and clear manner
so that the program that is implements this algorithm is similarly
orderly, understandable and clear manner. The three basic
methods of control used in a structured flowchart are also
applicable in structured algorithm.
Sequential
Selection
Repetition

Pseudocode is a mixture of language and symbols, terms and


other feature commonly used one or more high-level
languages. High-level languages are programs used by the
computer. Typically, the common features of various
pseudocodes that appear in textbook are the following:

The usual computer symbols are used for arithmetic


operations: + for Addition. - for subtraction, * for
multiplication, and / for division.
Symbolic names (identifiers) are used to represent
quantities being processed by the algorithm.
Certain keywords that are common in high-level language
may be used; for example, read or enter to indicate input
operation; display, print and write for output operations.
Indentation is used to set off certain key blocks of
instructions.

The Structure of an algorithm can be displayed in a


structure diagram that shows the various that must be
performed and their relation to one another. These
diagrams are especially useful in describing algorithms for
more complex problem. This section will show similar
examples used in flowchart development that will illustrate
the three basic control structures sequential, selection,
and repetition and how to present algorithms in pseudo
code.
Sequential structure

Figure 1-7 Algorithm that shows the printing of the sum

ALGORITHM SUM
SUM = 0
INPUT A, B
SUM = A + B
OUTPUT SUM
END SUM

Figure 1-8 Algorithm that shows how to determine the


product of three numbers

ALGORITHM PRODUCT
PRODUCT
0
INPUT A, B, C
PRODUCT
A*B*C
OUTPUT PRODUCT
END PRODUCT

Figure 1-9 Algorithm that will determine the positive and


negative number.

ALGORITHM POS_NEG
INPUT N
IF N < 0 THEN
OUTPUT NEGATIVE
END IF
IF N = 0 THEN
OUTPUT INVALID
ELSE
OUTPUT POSITIVE
END IF
END POS_NEG

Figure 1-10 Algorithm that determines if the Average


is Passing or Failing.

ALGORITHM PASS_FAIL
INPUT AVE
IF AVE <75 THEN
OUTPUT FAILING
ELSE
OUTPUT PASSING
END IF
END PASS_FAIL

Figure 1-11 Algorithm that will print Odd numbers from 0-10

ALGORITHM ODD
N
0
OD
0
REPEAT
N
N+1
OD N Mod 2
IF OD = 1 THEN
PRINT OD
END IF
UNTIL N=10
END ODD

Characteristics of Algorithm
An algorithm is precise. Each step of an algorithm must
specify exactly what is to be taken. Theres no room for
vagueness. Whats more each step has to be given
explicitly. Steps developed are clear. None can be
understood or assumed.
An algorithm is effective. No instruction may be impossible
to carry-out for the person or machine executing the
algorithms.
An algorithm must terminate. It must reach a point where
the task is complete and no more instructions remain to be
executed. The execution of an algorithm must not go on
indefinitely.
An algorithm must be correct. Note that while the three
characteristics just given are necessary for an algorithm to
live up to its guarantee, they are not sufficient. An incorrect
algorithm must possess the first three characteristics, yet
still contain erroneous instructions that prevent it from
accomplishing the task for which it is designed.

Flowcharting problems
Make

a flowchart that will determine if a


number is divisible by 2. If it is an even
number, double the value of the number and
print it. Otherwise print the odd number.

Make

a flowchart that will print all numbers


divisible by 2 and 5 ranging from 1 to 20.

PROGRAM ELEMENTS AND STRUCTURE

STUDENT LEARNING OBJECTIVES:


At the end of the chapter, the students are expected to:
To know the basic elements that is used to construct
algorithms.
To understand the nature of each element.
To determine the basic structure that will be used to
formulate algorithm.

The basic elements of algorithm are as follows:


Constants
Variables
Expressions

CONSTANT

A constant is a literal value that does not change during program


execution. It is fixed value.

Two types of constant

Numerical constant may be an integer or real number.


Characteristics:
Commas and blank spaces cannot be included within the constant.
The constant can be preceded by a minus sign - if desired.
The value of constant cannot exceed specified minimum and maximum
bounds. For each type of constant, these bounds will vary from one
compiler of a program to another. This is controlled by the software.

Two types of Numeric constant

Integer Numeric Constant is a whole number. It


has an exact value and does not contain a decimal
point.

Example: Several valid decimal integers are shown


below:
90
-1
700
5000

The following decimal integer constants are


written incorrectly for the reason stated:
16,000
15 28 34
243-100-255

illegal character ,
illegal character blank space
illegal character -

Real Numeric constant number that contains a decimal point or an


exponent (or both). It has approximate value.
Example: Several valid real constants are shown below:
3.30
415.05

The following are not valid real constant for the reasons stated:
20 either a decimal point or an exponent must be present
61,456.8 illegal character ,

Character Constant is a single character, enclosed in an apostrophe


(single quotation marks)

Example: Several characters, constants are shown below:


F
w
7
*

String Constant consist of any number of consecutive characters


(including none) enclosed in double quotation marks.

Example: Several string constants are shown below:


computer
$23.05
The right answer is.

Variables

A variable is a portion of the computers main memory used


to store a numeric or a string constant. It is also a named
memory location. It represents a certain value. The value
stored in the variable may be changed during program
execution but it can only hold one value at a time.
Rules in giving variable names:
1. Must contain only letters, numbers and/or underscore
_
2. Must begin with letters.
3. Must end either with a letter or a number.
4. Must contain a maximum of eight (8)characters.
5. Underscores must not appear consecutively.
6. Must be unique, that is not a name of another variable.
7. Must not be a keyword or reserved word of any
programming language.

Example:
Several variables are shown below:
S
Sum sum
Total2
a5
Variable name are not case sensitive, that is upper
and lower case letters are not differentiated.
The following are not valid variable names for the
reason stated:

NO STUD
NO _STUDE
8CTR
CTR1_
MA__LE

illegal character (blank space)


exceeds eight characters
does not start with a letter
ends with an underscore
underscore appears consecutively

Types of Variables
Numeric Variable is a variable that can
contain data or hold numeric constants (or
value) only.
Types of Numeric Variables
Integer Numeric Variable holds only
integer constant
Real Numeric variable holds real constant.
Character Variable is a variable that can
contain character constant.
String Variable is a variable that can contain
or hold string constants (or values) only.

An expression is a group of program elements


consisting of operands and operators. Operands can
be a constant or a variable. Operators can be
arithmetic and relational. Expressions always yield a
value of a certain type depending on the type of
expressions used.
Types of Expressions
Arithmetic expressions
String Expressions
Boolean Expression
Arithmetic Expressions
An Arithmetic expression is an expression consisting
of numeric constants or numeric variables separated
by arithmetic operator which yields a numeric value.

The general format of a numeric expression is


as follows:
operand operator operand operator
operand
Example:
4*2
4 and 2 are operands
*(Multiplication symbol) is operator

Operator

Purpose

Addition

Subtraction

Multiplication

Division or div
operator
Modulus division

Truncated division is sometimes referred to a modulus


operator (%). It is a division in which the decimal portion of
the quotient is always removed or truncated without
rounding.

Examples:
10 %
98 %
3
%
1
%

3
10
100
2

=
=
=
=

1
8
3
1

Type rules for arithmetic expressions


How to be determine the type of an arithmetic expression:

Combining something of type real with something of type either


integer or real always yields something of type real.

Combining anything with the division sign always yields


something of type real( in C language, an integer division will
yield an integer)

Combining two things of type integer with either addition sign


(+), subtraction

sign (-) or the multiplication sign(*) yields something of type


integer

Placing a minus sign (-), in front of an arithmetic expression does


not change its type.

Examples:
Real and integer will yield real value
2.0 +
1
=
3.0
2.0 1
=
1.0
2.0 /
1
=
1.0
2.0 *
1
=
2.0
Division will yield real value.
2.0 /
1.0
=
2.0
2.0 /
1
=
2.0
Integer Division (It truncates the remainder part of the
quotient)
2
/
2
=
1
5
/
2
=
2

Complex arithmetic expressions


Numeric expression can be mixed to contain several
operands and operators. Parenthesis can be used as
well to group a certain portion of the expression.
Examples:
6 + 2/2 =
=
=

a.
b.
c.

6 + 2/2
6 + 1.0
7.0

(3 + 4) * 10 = a.
=
b.
=
c.

(3 + 4) * 10
7 * 10
70

10 * 4 * 2.0 = a.
=
b.
=
c.

10 * 4 * 2.0
40 * 2.0
80.0

Order of evaluation for arithmetic expression


1. If parentheses are present, then they
determine the order of operation.
2. If the order is not determined by
parenthesis, then *, / and % operations are
evaluated before + and -.
3. If there is still the question of which
operation to do first, the compelling
operations in their left to right order.

Examples of evaluation:
1. 20 / 4 * 9 % 3
=
=
=
=

a.
b.
c.
d.

20 / 4 * 9 % 3
5*9%3
45 % 3
0

2. 5 * 3 / 5 * 10 20 *2
=
=
=
=
=
=

a.
b.
c.
d.
e.
f.

5 * 3 / 5 * 10 20 *2
15 / 5 * 10 20 * 2
3 * 10 20 * 2
30 20 * 2
30 40
-10

3. (15 - 8) + 10 * (11 + 6)
=
a.
=
b.
=
c.
=
d.

-7 + 10 * (11 + 6)
-7 + 10 * 17
-7 + 170
163

4. 5 * (12 * 10 / (25 20) )


= a. 5 * (12 * 10 / (25 20) )
= b. 5 * (12 * 10 / 5)
= c. 5 * (120/5)
= d. 5 * 24
= e. 120

Constructing Arithmetic Expressions


In practice, arithmetic expressions are not normally
expressed in programmable form; instead they are
normally expressed in algebraic form. For example,
2X + XY
3XY
In any programming language, the example just
mentioned will be considered as incorrect or invalid.
For one, operands must be separated by an operator
and only valid symbols must appear in the expression.
In algorithmic language, only the following symbols
and elements must be included in the expression.
( ) * / + - symbols
Numeric constants
String constants
Variable names

Examples of conversion:
1.
2X + XY
= ((2 * X) +( X*Y)) / (3 * X * Y)
3XY
2.

(3 + 4) (8 + 9)

(3 + 4) * (8 + 9)

A string expression is an expression consisting of string


constants or string variables separated by string operator,
which yields a string value.
The general format of a string expression is as follows:
operand
operator
operand
operator
operand
Example:
A = Jesus
B = will
C = guide us
D=
D=A+B+C
D, A, B, and C are all string variables.
+ operator
Output:
D = Jesuswillguideus

String expressions use only one operator, the + operator.


The effect of + operator on string is concatenation.
Concatenation means appending the value of the
second operand to the end of the first value of the first
operand

Boolean Expressions
A Boolean expression is either a relational expression or
logical expression, which yields either a TRUE or FALSE
value. (Since this module is Algorithm based on C
programming language, the equivalent of TRUE is 1 and
FALSE is 0)

Relational Expressions
A relational expression is a Boolean expression consisting
of numeric or string expressions, numeric of string
variables and numeric or string constants, separated by a
relational operator.

The general format of a relational expression is as follows:


operand
operator
operand . . .

Both

of the operands must be of the same


type, that is either both numeric and both
string. The operand can be a numeric or
string expression, numeric or string variable,
or numeric or string constant.
The following are the relational operators:
Operator
>
> =
<
< =
=
< >

Meaning
Greater than
Greater than or equal to
Less than
Less than or equal to
Equal to (in C, equal is ==)
Not equal to (in C, equal is!=)

Figure 2-3 Rules in evaluating Relational expressions:

1. When evaluating a relational expression, the value of


the left operand is compared to the value of the right
operand to determine whether the entire expression is
true or false.
2. If the operand is a variable then its corresponding value
will be used for comparison.
3. If the operand is a numeric or string expression, the said
expression is evaluated first to determine its final value
that will be used for comparison.
4. If a portion of numeric or string expression is variable,
then its corresponding value will be used to arrive at the
final value of the said expression.
5. When comparing string values, each character or both
operands are compared according to its ASCII code
equivalent. The comparison is made from left to right one
character at a time using the value of the ASCII code as the
basis of comparison. If one operand is shorter (i.e.,
contain lesser number of characters) than the other, the
shorter operand is padded with spaces to make the length
of both operands equal.

Examples of relational Expressions evaluation:


Rate = 100
Total_CS = 50
Total_IT = 64
Comtech = 40
Names = JOY JOAN KZ

Relational Expression Interpretation


Rate > 50
Total_CS < > Total_IT
Comtech < > 40
Names = Arnie
CS > PUP
DOG = dog

TRUE
TRUE
FALSE
FALSE
FALSE
FALSE

Logical Expressions
A logical expression is an expression consisting of
one or more relational expressions separated by the
logical operator that yields either TRUE or FALSE.
The general format of a logical expression is as
follows:
The relational expression before the logical operator
is optional only if the logical operator NOT is used.

The following are the logical operators:

NOT
AND
OR
To evaluate a logical expression, the so-called truth
table must be noted. Each logical operator has
operator has a corresponding truth table and are as
follows:

NOT truth table


NOT True
NOT False
=

=
TRUE

AND truth table


True AND True
=
True AND False =
False AND True =
False AND False =

TRUE
FALSE
FALSE
FALSE

OR truth table
True OR True
True OR False
False OR True
False OR False

=
=
=
=

FALSE

TRUE
TRUE
TRUE
FALSE

When evaluating a logical expression, the relational


expressions must be evaluated first to determine whether
the said relational expression is true or false. Once
evaluated, the appropriate truth table must be referred
to determine the final evaluation.

Example of Logical expressions evaluation:


Consider the following variables and its value.
CS = 15
IT = 0
NAME = KC
BAYAD = 500
1. (CS > = 10) AND (CS < = 20)
TRUE
AND TRUE
2. (IT = 1)
FALSE

OR (IT = 2)
OR FALSE

FALSE

TRUE

3. NOT (NAME=JOHN)
NOT FALSE
=
4. NOT
NOT

(BAYAD > 1000)


FALSE
=

TRUE

TRUE

5. (BAYAD>600) AND (BAYAD < 800)


FALSE AND TRUE =
FALSE

Evaluating Complex Logical Expressions


When evaluating complex logical expressions, the
precedence rule of logical expressions or the hierarchy of
logical expression evaluation must be taken into
consideration to determine the order by which the
operations are to be evaluated. According to the
precedence rule, operators with a higher hierarchy or
priority are evaluated first.
Hierarchy or Priority of Logical Operators
Expressions in parenthesis
first
NOT
second
AND
third
OR
fourth
In the event that the operation involves operators with
equal priority, the left to right rule is followed. The socalled left to right states that when the operation involves
operators with equal priority, the left most operation is
evaluated first.

For nested parenthesis, that is, operation in


parenthesis within operation in parenthesis, the
inner parenthesis is evaluated first prior to the
outer parenthesis.

Examples of evaluation:
Consider the following variables and its value:
A=1
NAME = "JOANA
CCMIT = 100
1. (A = 1)
OR
(A = 2)
OR
(A = 3)
TRUE
OR
FALSE
OR
False =
TRUE

2. (NAME= JOANA) AND NOT

((A>=50)

(A<=150))
= TRUE
AND NOT((FALSE
AND
= TRUE
AND NOT
(FALSE)
= TRUE
AND TRUE
=

AND

TRUE))

TRUE

3. (NAME= JOANA) AND (CCMIT = 100) OR


(A>=50)
=TRUE
AND TRUE
OR
FALSE
=TRUE
OR
FALSE
=
TRUE

Anda mungkin juga menyukai