Sunteți pe pagina 1din 78

Introduction to

Programming
CSC 25

Prepared by:

Mengvi P. Gatpandan
Program Logic
Formulation
Definition of
Programming
 Programming
Computer process data under the
control of set of instructions.
 Programmer
Computer program guide the
computer through orderly sets of
actions by people.
Programming Cycle
The computer is basically a problem-
solving tool. Once it is properly
provided with a valid program, the
computer becomes self-operational,
that is, it requires no human
intervention during processing. To use
this tool effectively, the programmer
usually have to perform the following
set of well define steps.
Programming Cycle
 Defining the Problem
One of making sure what was has to be
done or what problem has to be solved.
A careful analysis of that need should
involved both prospective users of the
information and one or more trained
systems designers or programmers,
who can perceive the full nature of the
information need and how the computer
can be used to respond to it.
 Planning the Solution
This is the next step, after the
problem has been defined, select
the best method for solving the
problem. This commonly
involves determining the
sequence of processing steps
within individual programs.
To save on time and effort, the programmer
refers to a pictorial representation, the
program flow chart. The flow chart is an
all-purpose tool and a program blueprint.
The programmer constructs the flow chart
to lay out the program logic.
 Coding the Solution
Once the programmer is satisfied that all
processing steps have been identified and all
alternatives and exceptions provided for, the
program is then made. The program flow chart
serves as a guide in coding.
 Checking out the Program
Experience will prove that a program seldom
executes successfully the first time. Even the
simplest programs contain errors, and even
experienced programmers make mistakes.
 Documentation
After a program has been fully tested and
implemented, the programmer must write
up the full specifications for all users. The
technical documentation package should
include:
1. Complete statement of the problem.
2. Description of the program logic and
copies of the flow charts.
3.Final source listing
4. List and description or error tests along
with the disposition of these error.
5. Operating instructions, including
program and data structures, and test data
used.
6. Input and output specifications.
7. Schedules to be maintained.
If all the pertinent data is contained within the
documentation package, then the program can
be run and modified without requiring the
original programmer’s assistance.
The Programming Cycle
Write up the full
DOCUMEN specifications for other
TATION
program users
Defining what DEFINING
has to be done or THE
what problem PROBLEM
has to be solved

Perform
CHECKING debugging
OUT THE and testing the
PLANNING PROGRAM
THE
program,
SOLUTION using
representative
CODING
s input data
Select the best method for THE
solving the problem SOLUTION

Prepare the set of instructions


for the computer to execute
Program Design Tools:

Flowchart
 A flowchart is a
pictorial
representation of a
step-by-step
solution to a part
of a problem.
Flowchart Basics
 A flowchart
consists of arrows
to represent
direction the
program takes and
boxes and symbols
to represent
actions.
Pseudocode
 Pseudocode is an English-like
nonstandard language.
 It allows programmers to focus on the
program logic without being
concerned about the particulars of a
formal programming language.
Algorithms
 An algorithm is a logical sequence of steps
leading to the solution of ONE TASK in a
bigger problem
– Define expected OUTPUT
– Describe desired INPUT
– Determine PROCESSING needed to
produce desired output from expected
input
Sample Problem
What tasks need to be
done?
 Add Number 1 and
Number 2
– Inputs/Outputs?
– Process?
 Subtract Number 2
from Number 1
– Inputs/Outputs?
– Process?
Use Modularity for
Design
 Break the problem into separate tasks
 For EACH task

– What are the inputs (if any)?


– What are the outputs (if any)?
– What process must be done?
 Construct pseudocode or flowchart
Flowcharting
Types of Flowcharts

1. Program Flowchart

It describes graphically in detail the logical operations


and steps within a program and the sequence in which
these steps are to be executed for the transformation of
data to produce the needed output.
2. System Flowchart

It is a graphically representation of the


procedures involved in converting data on input media
to data in output form. It illustrates which data is used
or produced at various points in a sequence of
operation.

It portray the interaction among data, hardware,


and personnel.
Flowchart Symbols
Input/Output Start/Stop

Process Connector

Flow Lines
Decision

Flow Direction
Preparation Indicators
Flowcharting Symbols
Input/Output
Parallelogram

• It represents an instruction to an input or an output


device.

Process Rectangle

• This symbol is used to represent a group of program


instructions that perform a processing function of the
program such as to perform arithmetic operations, or to
compare, sort, etc.
Flowcharting Symbols

Decision Diamond

• It denotes a point in the program where more than


one path can be taken. It is used to document points
in the program where, based upon variable
conditions, a branch to alternative path is possible.
The particular path that is chosen depends on the
answer to a question or the result of a test which is
inserted in the symbols.
Flowcharting Symbols
Preparation
Hexagon

• This symbols used to represent an instruction or


group in instructions that will alter, or modify a
program’s course of execution. It is commonly
used to specify operations such as control, index
register, initialization, switch setting, and in
indicating loops.
Flowcharting Symbols
Start/Stop Oval

• It is used to designated the beginning and the end


of a program, or a point of interruptions.

Connector Small Circle

• This is a nonprocessing symbols which is used to


connect one part of a flowchart to another without
drawing flow lines. It denotes an entry or an exit from
another part of the flowchart and also used to change the
reading sequence of a flowchart on the same page.
Flowcharting Symbols

Arrowheads

• Used to show the direction of processing or data


flow. These are added to flow lines of a flowchart
appears confusing in its layout.
Flowcharting Symbols

Horizontal/
Vertical Lines

• It is used to show reading order or sequence in


which flowchart symbols are to be read.
Advantage and Disadvantage of
Flowcharts
 Advantage of Flowcharts

1. Since flowcharts are language-independent, they

can be learned and applied without formal knowledge

of a programming language.

2. It enforces users to give attention to significant

matters over the less significant ones.


3. Being graphically portrayed, flowcharts provide an

alternative to the usual narrative description of a

system or a program.

4. Flowcharts are easier to understand than a program

written in a particular programming language.


 Disadvantage of Flowcharts
1. Flowcharts do not represent a programming and

are more of a person-to person than a person-to-

computer means of communication. A computer

cannot accept a program describe in flowcharting.

2. Since thinking in graphic term is not normal,

flowchart cannot be viewed as a natural means of

communication.
3. Certain details often require long sequence of

interconnected symbols which could easily be

described in just a few lines of explanation.

4. It does not convey why a given set of operations is

made. Flowcharts only portray how.


5. Flowcharts do not highlight the important details

since each step receives as much attention in a

flowchart as any other.


Two flowcharts needed
Task 1 Task 2
Start Start

Get Get
Number 1 Number 1
Inputs
Get Get
Number 2 Number 2

Result = Result =
Number 1 + Number 2
Process Number 1 - Number 2

Display Result Outputs Display Result

End End
Sample Problem
Example 1 - Algorithms

 Algorithms for changing a flat


tire

1. Jack up the car


2. Unscrew the lugs
3. Remove the wheel
4. Put on the Spare
5. Screw the lugs
6. Jack the car down
Example 1 - Flowchart
 Flowchart for changing a flat
tire. Start A

Put on the spare


Jack up the Car

Screw on the lugs


Unscrew the lugs

Remove the wheel Jack the Car down

A End
Example 2

 Given the three numbers A, B,


C. Draw a flowcharts to
compute and print out the sum,
the average, and the product of
these values.
Example 2 - Algorithms
1. Read in the values of A, B, C.
2. Determine the sum of the three values
read.
3. Compute the average by dividing the
sum by 3.
4. Multiply the first value by the second
value then by the third value to
determine the product of the three
values.
5. Print out the computed values (sum,
average, and the product).
Example 2 - Flowchart
A
Start

Ave = Sum/3

Read
A, B, C
Prod = A*B*C

Sum = A+B+C Print Sum


Ave, Prod

A
End
Decision Making –
Sample Problem
Example 3
 The ABC Manufacturing Company plans
to give a year-end bonus to each of its
employees. Make an algorithms which
will compute the bonus of an employee.
Consider the following criteria: If the
employee’s monthly salary is less than
1,000.00 pesos, the bonus is 50% of the
salary; for the employees with salaries
greater than 1,000.00 pesos, the bonus
is 1,000.00. Print out the name and
corresponding bonus of the employee.
Example 3 - Algorithms
1. Read in the employee’s name and salary
2. Test if the employee’s salary is less than
1,000.00
3. If the salary is less than 1,000.00 pesos,
the bonus is 50% of the employee’s
salary. However, if the salary is more
than 1,000.00 pesos, the bonus is
1,000.00 pesos.
4. Print out the employee’s name and
bonus.
Example 3 - Flowchart
A
Start

Bonus = .5 Y Is Salary N
Read Bonus
- Salary < 1000
Name, = 1000
Salary
B

Print Name. B
A Bonus

End
Example 4

 Given two numbers X and Y. Draw a


flowchart to determine the difference
between X and Y. If X-Y is negative,
compute R=X+Y, if X-Y is zero, compute
R=2X+XY; and if X-Y is positive,
compute R=X*Y. Print out the values of
X, Y, and R.
Example 4 - Algorithms
1. Read in the values of X and Y
2. Subtract Y from X
3. If the difference between X-Y is negative,
compute R=X+Y, if zero, compute
R=2X+XY; and if X-Y is positive,
compute R=X*Y.
4. Print out the values of X, Y and R.
Example 4 - Flowchart
Start A

Read <0 >0


X, Y R = X+Y If Diff R = X*Y

B =0
Diff = X - Y
Print X, B
R = 2X+2Y
Y, R

End B
Example 5

 Create a Flowchart and algorithms that


would input five values of numbers and
compute for the average. If the average
is below 75, the remarks is failed, if not
remark is passed. Print the average,
remarks.
Example 5 - Algorithms
1. Read in the values of n1, n2, n3, n4, and n5
2. Compute the average of the five values read.
3. If the the average is below 75, the remark is
failed, if not the remark is passed
4. Print out the average and the remarks.
Example 5 - Flowchart
Start A

Read n1, Is
n2, n3, Y N
Rem = Rem =
n4, n5 average
Failed Failed
<75

ave =
(n1+n2+n3+n4+ Print Ave,
n5)/5 Rem

A End
Loop and Counter
Steps in Control Loop
 Initialization - the value of a counter used is initially
set equal to zero ( or one ). This process is always
done outside the loop.
 Test for Limit Conditions - before logic flows gets
out of a loop, a loop-terminating condition must first
be satisfied. The process of testing is usually found
either at the beginning or at the end of a loop.
 Incrementation - after each loop is executed, 1 is
added to the counter. Thus the counter reflects the
number of times the operation has been performed.
Note that process of incrementation is always done
within the loop.
 Looping
- is used when it is desired to make the same
calculation on more than one set of data.
- it consists of repeating a program, or a section of a
program, and substituting new data for each repetition.
 Counters
- is set up in a program loop to keep track of the
number of times the program segment is repeated.
- the program can then be terminated after the
completion of a predetermined number of passes.
- Prior to the execution of counting, all counters should
be initialized ( usually to zero) before the loop is
executed.
- For instance, if a program is supposed to read
and print five data values, a counter may be set
up in the program which counts the number of
data values; after a data value is read and printed,
the counter is increased by one. Before a new
data value is allowed to be read, a conditional
transfer may be used to determined whether or
not the desired amount of data has been reached.
If it has, the program is terminated; otherwise an
unconditional transfer causes the computer to
repeat the input/output and other processing
operations.
Example 1

 The initial value of the radius of a circle


is equal to one unit and each
succeeding radius is one unit greater
than the value before it. Draw a
flowchart to compute the area of the
circle starting with R=1.0 up to R=5.0,
then print out each radius and the
corresponding area of the circle.
Example 1 - Algorithms
1. Initialized the value of R to 1 and the value of
PI to 3.1416
2. Compute area by multiplying PI by the square
of R.
3. Print out the value of R and the square of R.
4. Increment the value of R by 1.
5. Test if R is less than or equal to 5
6. If R is less than than or equal to 5, loop back
and repeat step 2 through 5. However, if R is
greater than 5, stop processing.
Example 1 - Flowchart
A
Start

R = 1.0
Print R,
PI = 3.1416 AREA

B
AREA =
PI *R*R
R = R + 1.0

A Y Is R < = N
B 5.0 ? End
Example 2

 Given a set of 5 numbers that include


both positive and negative data values.
Draw a flowchart to read in these values
one at a time and count the number of
positive values (including zero values)
and the number of negative values
found in the set. After the required
values are determined, print out these
counted values.
Example 2 - Algorithms
1. Set up two counters and initialized their
values to zero (one counter, NNEG, is used for
counting the number of negative values and
the other, NPOS, for counting positive values).
2. Input the first data value, NO.
3. Test and determine if the the value read is
positive or negative (The decision symbol is
used and in it is written the question, NO<0?)
4. If the value of NO is negative ( or less than
zero), increment the NNEG counter by one.
However, if it is positive ( or equal to or
greater than zero), increment the NPOS
counter.
Example 2 - Algorithms
5. Determine the number of values
encountered (NVAL) by adding the two
counters (NNEG and NPOS).
6. Test if NVAL is equal to 5
7. If the value of NVAL is not equal to 5,
repeat steps 2 through 5. However, if it is
equal to 5, print out the current value
stored in these counters (NNEG and
NPOS)
Example 2- Flowchart
A
Start

NNEG = Y Is NO N NPOS =
NNEG = 0
NNEG + 1 <0 NPOS + 1
NPOS = 0
B
NVAL = NNEG +
NPOS
Read
NO
Is NNEG,
N Y
B NVAL
= 5? NPOS

A
End
Example 3

 Draw a flowchart and algorithms which


will read and print the names and
individual scores of 100 students for a
particular examination. Also, determine
their average and score, and print it out.
Example 3 - Algorithms
1. Initialize the counter (CTR) and accumulator
(sum) to zero.
2. Read in the student’s name and score.
3. Print out the student’s name and score.
4. Accumulate the data value (SCORE) into the
sum.
5. Test if the desired amount of data (100) has
been reached.
6. If the value of counter (CTR) is less than 100,
repeat the steps 2 through 5. However, if the
desired amount of data has been reached
(100), compute the average score by dividing
the sum by the value of counter.
Example 3 - Algorithms

7. Print out the average scores.


Example 3 - Flowchart
Start A

CTR = 0 SUM = SUM + SCORE


SUM = 0
B

Read Ave = Y Is CTR N CTR = CTR + 1


Name, SUM/CTR = 100?
SCORE
Print B
Ave
A

End
Example 4
 Given the lengths of 3 sides (a, b, and c)
of a triangle. Prepare a flowchart to read
these values, compute the perimeter and
the value of S, and determine the area of
the triangle using Hero’s formula (Hint:
Per = a + b + c; S=Per/2; and the
AREA=[s(s-a) (s-b) (s-c)]2. Then print
out the given and computed values.
Example 4 - Flowchart
Start A

CTR = 0 Perimeter = a + b + c

Read Is Y
Print
a, b, c Per <
CTR
0?

A
1
Example 4 - Flowchart
1 B

N
Is Print
S = Per/2
End Area = 0 ? a, b, c
Per, S,
Area

Area = s(s-a) (s-b) (s-c)


CTR = CTR + 1

B
C
Loop and Accumulator
•Accumulator
The variable used is thereby
continuously accumulating the numerical
quantities
Example 1

 Draw a flowchart to compute and


print the sum of the squares of
positive integers from 1 to 100.
Example 1 – Flowchart-Sol 1- K<=100
Start
A

K=1
Sum = 0
B
Is
Y N
K=k+1 Print
B k<=100?
sum

Sum = Sum + 1
End

A
Example 1 – Flowchart-Sol 2 – K=100
Start
Print Is
Y N
sum K =100? K=K+1
K=1
Sum = 0
B
End B
Sum = Sum + K2

A
Example 1 – Flowchart-Sol 3 – K>100
Start A

N
K=1
Sum = 0
Sum = Sum + K2
B

Is
Y Print
K > 100? sum K=k+1

End B
A
Example 2

 Make a flowchart that will display the


numbers from 1 – 5.
Example 2 - Flowchart
Start A

A=1 Print
A

F A=A+1
B If A < = End
10 ?

T
B
A
Example 3

 Draw a flowchart to compute and


print the sum of the square of
positive integer from 1 - 5
Example 3 - Flowchart
Start A

X=1 A=A+1
Sum = 0

F Print
If A < =
Sum = Sum + B Sum
5?
(A * A)
T
End
A B
Example 4

 Create a flowchart that will print the


product numbers from 1 - 5
Example 4 - Flowchart
Start A

A=1 Prod = A * Prod


Prod = 0

A=A+ 1

If A < = F Print
B Prod
5?
B
T
End
A

S-ar putea să vă placă și