Download Lecture2_ProblemSolving

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Exact cover wikipedia , lookup

Computational electromagnetics wikipedia , lookup

Lateral computing wikipedia , lookup

Inverse problem wikipedia , lookup

Simplex algorithm wikipedia , lookup

Mathematical optimization wikipedia , lookup

Simulated annealing wikipedia , lookup

Expectation–maximization algorithm wikipedia , lookup

Knapsack problem wikipedia , lookup

Multiple-criteria decision analysis wikipedia , lookup

Travelling salesman problem wikipedia , lookup

Pattern recognition wikipedia , lookup

Computational complexity theory wikipedia , lookup

Secretary problem wikipedia , lookup

Weber problem wikipedia , lookup

Planted motif search wikipedia , lookup

Halting problem wikipedia , lookup

Transcript
FTMK, UTeM – Sem 1 2013/2014
2. Problem Solving
BITP 1113 PROGRAMMING TECHNIQUE
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
 understand top down design

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.

SYSTEM DEVELOPMENT LIFE CYCLE(SDLC)
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
4 STEPS IN PROBLEM SOLVING

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
PROBLEM SOLVING – EXAMPLE PROBLEM A
Problem Statement :
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)
PROBLEM A– STEP 1
Understand the problem:
1.

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 ???
PROBLEM A – STEP 1 (CONTINUE)

Input


Output


the distance in miles
(miles)
the distance in kilometers
(km)
Process (or formula)
We know: 1 mile = 1.609 kilometers
 The formula: km = miles x 1.609

PROBLEM A – STEP 1 (CONTINUE)
SPLIT THE PROBLEM INTO GROUPS (WE CALL IT AS FUNCTIONS)
Main
Problem
Function 1:
Input the
distance in
miles
Function 2:
Convert the
distance in
miles to
kilometers
Function 3:
Display the
distance in
kilometers
PROBLEM A – 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)
b)
Pseudo code or
Flowchart
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.

PSEUDO CODE FOR PROBLEM A
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
FLOWCHART – BASIC SYMBOLS REPRESENT
VARIOUS OPERATIONS
Direction
Terminal –
beginning / end
Decision
Connector
Preparation
Process
Input /
Output
Predefined Process
(Subroutine)
FLOWCHART FOR PROBLEM A
Input ()
Start
M = Input()
K = Process(M)
Output(K)
Process (M)
Read distance
in miles (M)
K = M * 1.609
Return
M
Return
K
Output (K)
Display distance
in kilometers (K)
Return
End
PROBLEM A – STEP 3
3. Write the program
/* Problem Solving Exercise : Introduction
Comments: multiline
to C++ */
and single line
// I love to code
Preprocessor directives
#include <iostream>
Namespace
using namespace std;
//global declaration area
double input();
void display(double);
double calculate(double);
void main()
{
double miles, km;
miles = input();
km = calculate(M);
display(km);
}
function prototype
//declare the variable
Main Function
double input()
{
double miles;
cout<<"Please enter distance in miles :";
cin>>miles;
return miles;
}
User-Defined
Function
double calculate(double distance_in_miles)
{
double km;
km = distance_in_miles * 1.609;
return km;
}
User-Defined
Function
void display(double distance_in_km)
User-Defined
{
Function
cout<< "Distance in kilometre is "<< distance_in_km;
cout<< "\n";
}
SOLVING PROBLEM 1 – STEP 4
4.
Execute the program
* Check for any syntax / run-time / logic errors
Output of the program
WHAT IS FUNCTIONS
Break up the programs into smaller chunks so
that it is easier to develop.
 We can call the functions whenever we want to
use it (write once, use several times).
 Create functions especially if we want to use
the code repeatedly.

ANOTHER APPROACH TO SOLVE PROBLEM A
(WITHOUT FUNCTION) – 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


Process (or Formula)



the distances in miles, miles
1 mile = 1.609 kilometers
kms = miles x 1.609
Output

the distance in kilometers, kms
ANOTHER APPROACH TO SOLVE PROBLEM A
– STEP 2
Step 2 – Design the solution (or
algorithm)
The flowchart
The pseudocode:
1.0 Start
2.0 Get the distance in miles, miles
3.0 Calculate the distance in kilometer,
kms = miles x 1.609
Start
Get distance in
miles, miles
Calculate distance
in kilometers
kms = miles x 1.609
Display distance in
kilometers, kms
4.0 Display the distance in kilometers, kms
5.0 End
End
ANOTHER APPROACH TO SOLVE PROBLEM A–
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;
}
ANOTHER APPROACH TO SOLVE PROBLEM A–
STEP 4
Step 4 – Execute the program
* Check for any semantic / logic errors
PROBLEM SOLVING – EXAMPLE PROBLEM B

Write a program that inputs 2 integers n1 and n2. Calculate
and print the value of n12 and n23
SOLVING PROBLEM B – 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
SOLVING PROBLEM 2 – STEP 2
Step 2 – Design the solution (or algorithm)
The pseudocde
The flowchart
1. Start
2. Input n1 and n2
3. Calculate n12 and n23
3.1
ans1 = n1* n1
3.2
ans2 = n2 * n2 *
n2
4. Print ans1, ans2
5. End
Start
Input
n1,n2
Calculate
ans1=n1*n1
ans2 = n2*n2*n2
Prints ans1,
ans2
End
SOLVING PROBLEM B – STEP 3 AND 4
Step 3 – Write the code
#include <iostream>
#include <cmath>
Step 4 – Execute the program
Output
using namespace std;
void main()
{
double n1,n2,ans1,ans2;
cin>>n1>>n2;
ans1 = pow(n1,2);
ans2 = pow(n2,3);
cout<<ans1<<endl;
cout<<ans2<<endl;
}
* Check for any semantic / logic
errors
PROBLEM SOLVING – EXAMPLE PROBLEM C
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”.
SOLVING PROBLEM C – 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
SOLVING PROBLEM C – STEP 2
Start
Step 2 – Design the solution (or algorithm)
Pseudocode
Flowchart
Get V,R
V > 10?
1.0 Start
2.0 Get the input data for V, R
3.0 If V > 10
print message “The volts is too big”
else
If V<0 , print msg “Not a valid input”
else
If V<=10, I = V/R, print I
yes
Print msg
“ The volts is too big”
yes
Print msg
“Not a valid input”
no
V < 0?
no
yes
V <=10?
no
4.0 End
End
I=V/R
Print I
SOLVING PROBLEM C – 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;
}
}
SOLVING PROBLEM C – STEP 4
Step 4 – Execute the program
* Check for any semantic / logic errors
EXERCISES


Identify the input, process and output to solve each problem. Then, write the
pseudocode and draw the flowchart for each one :
Problem 1


Calculate the 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.
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?
