You are on page 1of 35

BITP 1113 Programming Technique

Lecture 2 – Problem Solving


Learning Outcomes
 At the end of this lecture, you should be able to
 explain the software development methodology
 describe basic problem solving techniques
 design algorithms to solve programming problems by
using flow chart and pseudo code

FTMK - UTeM - Semester 1 2019/2020


System Development
 The critical process determines the overall quality
and success of the program
 If any program is design carefully using good

structured development techniques, the program will


be efficient, error-free and easy to maintain.
 Most programming projects are built using system

development life cycle.


 One of the popular development life cycle is known

as the waterfall model.

FTMK - UTeM - Semester 1 2019/2020


System Development Life Cycle(SDLC)

FTMK - UTeM - Semester 1 2019/2020


Program Development
 A multi-step process that requires you understand the problem,
design a solution, write the program, and test the program.

 Program design - To determine how to take inputs and convert


them into the output that have been specified in the program
requirements statement

FTMK - UTeM - Semester 1 2019/2020


4 Steps in Solving Problem
 Step 1: Analysis
 Understand the problem (if top down design -> draw structure
chart)
 Step 2: Design the solution
 Design the algorithm
 Step 3: Code
 Develop (or write) the program
 Step 4: Test the program

FTMK - UTeM - Semester 1 2019/2020


Problem Solving – Example Problem1

Problem Statement :

Your summer surveying job requires you to study some


maps that give distances in miles. You and your co-workers
prefer to deal in metric measurements (km). Write a
program that performs the necessary conversion. The
program will display the distance in km.
Formula: (1 mile = 1.609 kilometers)

FTMK - UTeM - Semester 1 2019/2020


Solving Problem1 – Step 1
1. Understand the problem:
 Statement to highlight: Your summer surveying job
requires you to study some maps that give distances
in miles. You and your coworkers prefer to deal in
metric measurements (km). Write a program that
performs the necessary conversion. The program will
display the distance in km.
Formula: (1 mile = 1.609 kilometers)

What are the INPUTs ??? What are the OUTPUTs ???

What are the PROCESSes ???

FTMK - UTeM - Semester 1 2019/2020


Solving Problem1 – Step 1
 Input
 the distance in miles

(M)

 Output
 the distance in kilometers

(K)

 Process (or formula)


 1 mile = 1.609 kilometers
 K = M x 1.609

FTMK - UTeM - Semester 1 2019/2020


Solving Problem1 – Step 1
Structure Chart
(if using top-down design)

Main
Problem

Convert the
Input the Display the
distance in
distance in distance in
miles to
miles kilometers
kilometers

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 1 – Step 2
2. Design the solution (or Algorithm):
 Algorithm is a step-by-step problem solving process
in which a solution is arrived at in a finite amount of
time.
 Algorithm can be designed using:
a) Pseudo code or
b) Flowchart

FTMK - UTeM - Semester 1 2019/2020


Pseudo code
 Precise algorithmic description of program logic
 Its purpose is to describe, in precise algorithm detail, what
the program being design is to do.
 Requires the defining the steps to accomplish the task in
sufficient detail so that they can be converted into a
computer program.

FTMK - UTeM - Semester 1 2019/2020


Pseudo code for Problem 1

1.0 Start

2.0 Call input function


2.1 Read distance in miles (M)
2.2 Return M

3.0 Call process function


3.1 Calculate distance in kilometer (K= M x 1.609)
3.2 Return K

4.0 Call output function


4.1 Display the distance in kilometers (K)

5.0 End
FTMK - UTeM - Semester 1 2019/2020
Flowchart – Basic symbols represent
various operations

Direction Preparation

Terminal –
Process
beginning / end

Decision Input /
Output

Connector
Predefined Process
(Subroutine)

FTMK - UTeM - Semester 1 2019/2020


Flowchart for Problem 1
Input () Process (M)

Start
Read distance
in miles (M) K = M * 1.609

M = Input() Return Return


M K

K = Process(M) Output (K)

Display distance
in kilometers (K)
Output(K)

Return
End FTMK - UTeM - Semester 1 2019/2020
3. Develop (or write) the program
Solving Problem 1 – Step 3

//written by: Nuridawati Mustafa Comments


/* Problem Solving Exercise : Introduction
to C++ */
Preprocessor directives Global
#include <iostream>
using namespace std; Namespace Declaration
Area
double input(); //global declaration
void display(double K);
double calculate(double M);

void main()
{
double M, K; //local declaration Main Function
//M represents distance in miles
//K represents distance in kilometers

M = input();
K = calculate(M);
display(K);
} FTMK - UTeM - Semester 1 2019/2020
double input()
{
double miles;
User-Defined
cout<<"Please enter distance in miles :";
Function
cin>>miles;
return miles;
}

double calculate(double distance_in_miles)


{
double km; User-Defined
km = distance_in_miles * 1.609; Function
return km;
}

void display(double distance_in_km) User-Defined


{ Function
cout<< "Distance in kilometre is "<< distance_in_km;
cout<< "\n";
}

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 1 – Step 4
4. Test the program
Output of the program:

* Check for any syntaxFTMK


/ run-time / logic errors
- UTeM - Semester 1 2019/2020
Another Approach to Solving Problem
1(without using top-down) – Step 1
 Step 1 – Understand the problem
Your summer surveying job requires you to study some maps that
give distances in kilometers and some that use miles. You and
your coworkers prefer to deal in metric measurements. Write the
program that performs the necessary conversion.
Formula: (1 mile = 1.609 kilometers)
 Input
 the distances in miles, miles
 Process (or Formula)
 1 mile = 1.609 kilometers
 kms = miles x 1.609
 Output
 the distance in kilometers, kms

FTMK - UTeM - Semester 1 2019/2020


Another Approach to Solving Problem
1(without using top-down) – Step 2
The flowchart Start
Step 2 – Design the solution (or
algorithm)
Get distance in
The pseudocode: miles, miles

1.0 Start
Calculate distance
in kilometers
2.0 Get the distance in miles, miles
kms = miles x 1.609
3.0 Calculate the distance in kilometer,
kms = miles x 1.609 Display distance in
kilometers, kms
4.0 Display the distance in kilometers, kms

5.0 End End


FTMK - UTeM - Semester 1 2019/2020
Another Approach to Solving Problem
1(without using top-down) – Step 3
Step 3 – Develop (or write) the program
# include <iostream>
using namespace std;

# define mile_to_km 1.609

int main( )
{

double miles,kms;

cout << "Enter the distance in miles that need ";


cout << "to be converted to kilometers : ";

cin >> miles;

kms = mile_to_km * miles;

cout << endl << miles <<" in miles is "<< kms;


cout <<" in kilometers "<< endl << endl;
FTMK - UTeM - Semester 1 2019/2020
}
Another Approach to Solving Problem
1(without using top-down) – Step 4
Step 4 – Test the program

* Check for any semantic / logic errors


FTMK - UTeM - Semester 1 2019/2020
Problem Solving – Example Problem 2
(without using top-down)

 Write a program that inputs 2 integers n1 and n2. Calculate


and print the value of n12 and n23

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 2 – Step 1
Step 1 – Understand the problem

 The program should be able to calculate the power of 2 and the power of
3 of two integer data, and print out both values.

Input : n1 and n2
Process : ans1 = n1*n1
ans2 = n2*n2*n2
Output : ans1, ans2

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 2 – Step 2
Step 2 – Design the solution (or algorithm)
The pseudocode The flowchart Start

1. Start
2. Input n1 and n2 Input
3. Calculate n12 and n23 n1,n2
3.1 ans1 = n1* n1
3.2 ans2 = n2 * n2 * n2
Calculate
4. Print ans1, ans2
ans1=n1*n1 , ans2 = n2*n2*n2
5. End

Prints ans1, ans2

End
FTMK - UTeM - Semester 1 2019/2020
Solving Problem 2 – Step 3 and 4
Step 3 – Write the code Step 4 – Testing and Evaluation
#include <iostream>
#include <cmath>
Output
using namespace std;

void main()
{
double n1,n2,ans1,ans2;

cin>>n1>>n2;

ans1 = pow(n1,2); * Check for any semantic / logic


ans2 = pow(n2,3); errors
cout<<ans1<<endl;
cout<<ans2<<endl;
}
FTMK - UTeM - Semester 1 2019/2020
Problem Solving – Example Problem 3
(without using top-down)

Ohm's law is often expressed in the form V=IR, where V is the voltage
measured in volts, I is the current measured in amps, and R is the resistance
measured in ohms.

Write a program that can calculate how much current (I) would flow through
an input ohm of resistor (R) if you were to connect the resistor to an input
volts of power supply (V).

However you need to check for the input volts of the power supply. If it is >
10 volts, don’t calculate the current, you just print out a message “ The volts is
too big”. If the volt is <= 10, calculate and print the current (I) and if the volts
is negative, print out a message “Not a valid input”.

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 3 – Step 1
 Step 1 – Understand the problem
 Input
 V, R
 Process ( or Formula)
 If V >10 , print msg “The volts is too big”
 If V <= 10, I = V/R
 If V < 0 , print msg “Not a valid input”
 Output
 I

FTMK - UTeM - Semester 1 2019/2020


Solving Problem 3 – Step 2
Start
Step 2 – Design the solution (or algorithm)
Pseudocode Flowchart Get V,R

yes Print msg


V > 10? “ The volts is too big”
1.0 Start
no

2.0 Get the input data for V, R yes Print msg


V < 0? “Not a valid input”
3.0 If V > 10
no
print message “The volts is too big”
else yes
If V<0 , print msg “Not a valid input” V <=10? I=V/R
else
If V<=10, I = V/R, print I
no Print I
4.0 End

End
FTMK - UTeM - Semester 1 2019/2020
Solving Problem 3 – Step 3
Step 3 – Write the code
# include <iostream>
using namespace std;

int main( )
{
double V,R,I;
cout<<"Enter V : ”; cin>>V ;
cout<<"Enter R : ”; cin>>R;
if (V>10)
cout<<“The volts is too big\n”;
else
if (V<0)
cout<<“The input is invalid\n”;
else
if (V<10)
{
I = V/R;
cout<<“I = “<< I<<endl;
}
}
FTMK - UTeM - Semester 1 2019/2020
Solving Problem 3 – Step 4
Step 4 – Testing and Evaluation

* Check for any semantic / logic errors


FTMK - UTeM - Semester 1 2019/2020
Solving Problem 3 – Step 4
Step 4 – Testing and Evaluation

* Check for any semantic / logic errors


FTMK - UTeM - Semester 1 2019/2020
Solving Problem 3 – Step 4
Step 4 – Testing and Evaluation

* Check for any semantic / logic errors


FTMK - UTeM - Semester 1 2019/2020
Exercises
Identify
the input, process and output to solve each problem.
Then, write the pseudocode and draw the flowchart for each one :
Problem 1
Calculatethe roots (x1 and x2) of a quadratic equation:
Formula : d = sqrt(b² - 4ac), x1=(-b + d)/2a, x2=(-b - d)/2a

Problem 2
Calculate the area of a circle based on a given radius.
Formula : Area of a circle = 3.14 * radius * radius

Problem 3
Sam has appeared in the final examination comprising of 3 papers. Input the marks
Sam has secured in each of the subjects and find out his average marks and
display the results.

FTMK - UTeM - Semester 1 2019/2020


Ask yourself
 Do you understand the software development
methodology?
 Can you describe the steps involved in solving a problem?
 Do you know how to identify the input, the process and
the output if given a problem?
 Do you know how to write the pseudocode and draw the
flowchart in designing the solution to the problem?

FTMK - UTeM - Semester 1 2019/2020

You might also like