You are on page 1of 15

 Problem Solving Phase

 Define the problem


 Find a solution to the problem
 Evaluate alternative solutions
 Represent most efficient solution as
algorithm
 Test the algorithm
 Implementation Phase

 Translate algorithm into specific


programming language
 Execute the program
 Maintain the program
 Decomposing a problem involves
three (3) steps

 What is given (input)


 The expected results (output)
 Tasks that must be performed (Processing)
Input Processing Output
2 numbers, say num1, 1. Read/get 2
num2 numbers
TOTAL
2. Add two numbers
3. Print total
 An algorithm is a sequence of precise
instructions for solving a problem in a finite
amount of time.
 Algorithms are:
 1. Precise
 2. unambiguous
 3. give the correct solution
 4. eventually terminate/end.
Every algorithm should have:

Header: Name or title


Declaration: Brief description of algorithm and
variables
Body: Sequence of steps
Terminator: End statement.
 The body of the algorithm could contain various
structures:
 Sequential Structures
Input statements – Read, get, accept
Output statements – Print, display
Arithmetic operations – Sum = num1+num2
Selection Structures
If-then-else statements –allows decisions to be
made based on some condition

Repetition Structures - For, Repeat, While


 Flowcharts use geometrical figures to show
the basic steps of a program.

Input/Output Processing

Start/Stop
Decision
 Program Heading
 Program Declaration
 Program Block
 Program Terminator (a period)
 program add; Heading
 {This program finds the sum of two numbers}

 VAR
 num1,num2:integer; Declaration
 sum:integer;

 BEGIN
 writeln('Enter the first number');
 readln(num1);
 writeln('Enter the second number'); Body
 readln(num2);
 sum:=num1+num2;
 Writeln('The sum of the two numbers is ',sum);
 END.
Terminator
 Program LargestNumber;
 {This program finds the largest of 3 numbers}

 VAR
 num1, num2, num3:integer;
 largest:integer;

 BEGIN
 Writeln('Enter the first number');
 Readln(num1);
 Writeln('Enter the second number');
 Readln(num2);
 Writeln('Enter the third number');
 Readln(num3);
 If num1>num2 then
 largest:=num1
 else
 largest:=num2;
 If largest<num3 then
 largest:=num3;
 Writeln('The largest number is ',largest);
 END.
 Program average;
 {This program finds the average of three numbers}

 VAR
 num1,num2,num3:integer;
 average:real;

 BEGIN
 writeln('Enter the first number');
 readln(num1);
 writeln('Enter the second number');
 readln(num2);
 writeln('Enter the third number');
 readln(num3);
 average:=(num1+num2+num3)/3;
 Writeln('The average of the three numbers is ',average);
 END.
 Program StudAverageScores;
 {This program find the average of 5 students scores}

 Var
 i:integer;
 Score:integer;
 AvgScore:real;
 TotScore:integer;

 BEGIN
 Score:=0;
 TotScore:=0;
 AvgScore:=0;

 For i:=1 to 5 do
 begin
 Writeln('Enter the student # ' , i, ' score');
 Readln(Score);
 TotScore:=TotScore+Score;
 end; {end for}

 AvgScore:=TotScore/5;
 writeln;
 Writeln('The average score is ',round(AvgScore));
 End.

 Program StopWhenISee_0;
 {This program accepts a value of 'a' until 0 is entered}
 var
 x,a:integer;

 BEGIN
 Writeln('Enter a value for a');
 Readln(a);
 While a<>0 do
 begin
 x:=a+5;
 Writeln('The value of x is ',x);
 writeln;
 writeln;
 Writeln('Enter a value for a');
 Readln(a);
 End; {endwhile}
 END.

You might also like