The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by j05stm21f504, 2022-10-01 02:18:41

Topic 2 - Problem Solving Methods

Topic 2 - Problem Solving Methods

TWO PAIR SHARING 51

In 5 minutes, write down
anything that you have
learned in chapter 2.
Choose your pair and share
it. (5 minutes)
Pick 3 students from magic
box

EXAMPLE 1

Step 1: Define Problem

Write a program that will get 3 numbers as input
from the users. Find the average and display the
three numbers and its average.

52

Example 1

STEP 2: THE PROBLEM ANALYSIS

Input:

numbers1, number2 and number3.

Process:

totalNumbers=number1+number2+number3
average=totalNumbers/3

Output:

The three numbers and its average

INPUT PROCESS OUTPUT

53

Example 1

Step 3: The Program Design
(using Algorithm)

To calculate the average of three numbers.

1. Set Total=0, Average=0; 54
2. Input 3 numbers
3. Total up the 3 numbers

Total= total of 3 number
4. Calculate average

Average=Total/3
5. Display 3 numbers and the average

Example 1

Step 3: The Program Design
(using Pseudocode)

55

Example 1

Step 4: The Program Coding

#include<iostream.h>
void main()
{
float a,b,c,sum,av;
cout<<"Enter three numbers:";
cin>>a>>b>>c;

sum=a+b+c;
av=sum/3;
cout<<"\nSUM="<<sum;
cout<<"\nAverage="<<av;

getch(); //to stop the screen
}

56

2.3 APPLY THE DIFFERENT
TYPES OF ALGORITHM TO
SOLVE PROBLEM

57

CONCEPT OF ALGORITHM

An algorithm is a list of steps to be executed with
the right order in which these steps should be
executed.
An algorithm can be represented using pseudocode
or flowchart.
Any algorithm can be described using only 3 control
program structures: sequence, selection and
repetition.

58

CONCEPT OF ALGORITHM

Algorithm:

⚫ Must may have input(s) and must have output(s)

⚫ Should not be ambiguous (there should not be
different interpretations to it)

⚫ Must be general (can be used for different inputs)

⚫ Must be correct and it must solve the problem for
which it is designed

⚫ Must execute and terminate in a finite amount of
time

⚫ Must be efficient enough so that it can solve the 59
intended problem using the resource currently
available on the computer

DISTINGUISH BETWEEN FLOWCHART AND
PSEUDOCODE

TYPES FLOWCHART PSEUDOCODE

Layout Graphical structure Structure for the
Benefits code of the program
For smaller
Structure concepts and More efficient for
Depth problems larger programming
Symbols and languages
shapes
Detail can cause Linear text-based
confusion structure

More flexibility with60
detail

EXAMPLE 1

A flowchart (and equivalent Pseudo code) to
compute the interest on a loan

Flowchart Pseudocode

61

EXAMPLE 2

A program that reads two numbers and displays
the numbers read in decreasing order

Flowchart Pseudocode

Read A, B
If A is less than B

BIG = B
SMALL = A
else
BIG = A
SMALL = B
Write (Display) BIG, SMALL

62

TIME FOR ACTIVITY!!
(30 MINUTES)

Form a group
Leader in group find the question in class
Go to your group
Discuss with your group member
Write your answer in Majung Paper
(Algorithm, flowchart, pseudocode)
Choose BEST GROUP as WINNER
(faster, correct , creative and tidiness)

63

EXERCISE

1. Write algorithm, pseudo code and flow chart for
the program which can calculate total sale by
multiply quantity and price.

2. Write an algorithm, pseudo code and flow chart
for this program: any students whom got CGPA
more than 2.0 will pass their study, and less than
2.0 will fail.

64

65


Click to View FlipBook Version