You are on page 1of 7

1/15/2017

Learning Outcomes
Explain the concepts of problem-solving and structured
programming.

References:
Problem Solving and Programming Concepts (9 th Edition) . Maureen Sprankle, Jim Hubbard, Pearson Education Limited 2012.
Simple Program Design. A step by step approach (5 th Edition). Lesley Anne Robertson. Cengage Learning 2006.

Contents
6 steps in problem solving1 in everyday Life.
Types of problems1.
Difficulties with problem solving 1

Recognize 7 steps in program development process2.


Pseudocode and developing an Algorithm2 WARM UP

References:
Problem Solving and Programming Concepts (9 th Edition) . Maureen Sprankle, Jim Hubbard, Pearson Education Limited 2012.
Simple Program Design. A step by step approach (5 th Edition). Lesley Anne Robertson. Cengage Learning 2006.

Problem Solving in Everyday Life


Problem-solving process (6 steps)

How do we Step 1: Identify the problem


solve everyday Step 2: Understand the problem

problems Step 3: Identify alternatives

in our life? Step 4: Select the best way to solve the problem

Step 5: List the instructions

Step 6: Evaluate the solution

1
1/15/2017

Problem Solving in Everyday Life (cont) Problem Solving in Everyday Life (cont)
Step 1: Identify problem : you are hungry
Lets solve a problem using all 6 steps. Step 2: Understand the problem : you need food
Step 3: Identify Alternatives :
- Method 1: look for mum
- Method 2: go to the kitchen and look for some food
Problem: - Method 3: go to the kitchen and prepare food yourself
You are doing assignment at home, and you are hungry. - Method 4: make a food delivery order
- Method 5: go out to have a meal in a restaurant / hawker center

Problem Solving in Everyday Life (cont) Problem Solving in Everyday Life (cont)
Step 4: Select the best way (based on the alternatives that List out the instruction
you have listed) Go out from the room
- Method 1 : you arent a kid anymore; cant always be looking for mum Go to the kitchen
- Method 2 : it might be a good idea Search the table, cabinet or refrigerator for food
- Method 3 : you might not have the time since you are doing assignment. Consume the food
- Method 4 : it might be expensive to make a delivery order Evaluate the solution
- Method 5 : you might not have the time since you are doing assignment. Does the solution help to solve the problem?
Does the solution solve the problem efficiently?
After some consideration, lets say you choose Method 2 Should we follow the same solution next time?

Types of Problems
Algorithmic solutions
Can be solved with a series of actions (the algorithm)
Hunger solved! Logging on to an e-mail account
Withdrawing cash from ATM machine
Lets move on.
Heuristic solutions (rule of thumb)
(not covered in our syllabus)
Based on knowledge & experience
Requires a process of trial and error
Adding salt to food (during cooking)
Buying the best stock

2
1/15/2017

Types of Problems (cont) Problem Solving with Computers


The 6 steps can be used for both problems
Computers are built for dealing with algorithmic
But step 6 is challenging for heuristic solutions
solutions.
Correctness and appropriateness are far less certain
Iterations (repetitions) are needed Write instructions step-by-step. Computers need to be
Same solution may not work all the time told what to do step-by-step.
Heuristic solutions requires (Artificial) intelligence
Most problems require a combination of the two kinds The program can make decision like humans for each
of solutions. E.g.: situation based on previous solutions.
Driving a vehicle Need to transform into algorithmic format.

Fixing a malfunction computer

Difficulties with Problem Solving


Why?
One or more steps in problem solving process are not
completed adequately. Warm up done!
Problem-solving process requires time and practice.
Problem solving on the computer. Lets get serious.
Problem with writing the instructions
E.g. List 5 numbers and pick the largest one.
(Can you explain/list all the steps required?)
The computer is a tool that will perform only tasks that the
user can explain.
All steps must be in proper order.

Introduction to Programming
Computer programming is writing computer
instructions to tell the computer step-by-step how
to process data and carry out other basic operations
systematically3.
Problem solving The basic operations of computers include:
with computers - Receive inputs
- Make decisions
- Repeat operations
- Display results the outputs

References:
3. Computer Science for Matriculation Semester 2. Yew Kwang Hooi, Oxford Fajar 2014.

3
1/15/2017

Introduction to Programming (cont) Introduction to Programming (cont)


The executable file that stores the set of instructions is Hence, computer programs are used to make repetitive
called a computer program. and boring tasks fast and automatic.
Programming helps us to solve many problems that Examples:
require speed, memory and precision: (a)A banking system automatically updates the database
(a) Speed repetitive tasks can be done at lightning every midnight.
speed. (b)A spreadsheet program can help user find statistical
(b) Memory total recall of all stored data and figures automatically. It also generates charts using the
information during a search. wizards.
(c)Computers at point-of-sales at shopping outlets
(c) Precision a computer does not make careless
retrieve product information after the cashier scans
mistake the barcode.
References:
3. Computer Science for Matriculation Semester 2. Yew Kwang Hooi, Oxford Fajar 2014.
References:
3. Computer Science for Matriculation Semester 2. Yew Kwang Hooi, Oxford Fajar 2014.

Introduction to Programming (cont) Programming Languages


Not all problems can be solved by computer programs: A programmer writes codes of a program using a
(a)Computer programs are best suited for problems suitable language.
which have clearly defined steps, are repetitive and use Five generations of programming languages:
electronic information. 1. 1GL (machine language)
(b)Problems which are not suitable are those that are 2. 2GL (assembly language)
based on intuition and creativity. 3. 3GL (structured programming, object-oriented
programming) E.g. C (used in our syllabus)
4. 4GL (non-procedural programming)
5. 5GL (constraint-based programming)

References:
3. Computer Science for Matriculation Semester 2. Yew Kwang Hooi, Oxford Fajar 2014. References:
3. Computer Science for Matriculation Semester 2. Yew Kwang Hooi, Oxford Fajar 2014.

7 Steps of Program Development Process


1. Define the problem
2. Outline the solution
Before we start with programming, 3. Develop an algorithm (based on the outline)
lets look at the all 7 steps in 4. Test the algorithm (for correctness)
5. Code the algorithm (using a specific programming language)
developing a computer program.
6. Run the program (on the computer)
7. Document & Maintain the program

4
1/15/2017

1 Define the Problem 1 Define the Problem (cont)


To help with initial analysis, the problem should be Example: Shop A is offering a 20% discount for all
divided into three separate components: products. Calculate the payment for the customers.
the inputs
the outputs IPO Chart
the processing steps to produce the required outputs Input Process Output
price 1. Prompt and Get price payment
discount_rate 2. Calculate payment
Use IPO chart (Input Process Output Chart) to help 3. Display payment
4. END
separate and define the three components.

2 Outline the Solution 2 Outline the Solution (cont)


This initial outline is usually a rough draft of the Example (step-by-step to solve the problem) :
solution and may include: Get the price of product purchased by the customer
The major processing steps involved Get the discount rate
The major subtasks (if any) Calculate the discounted amount
The user interface (if any) (discount_amount=price*discount_rate)
The major control structures (e.g. repetition loops) Calculate the payment after discount
The major variables and record structures (payment=price-discount_amount)
The mainline logic Display the payment amount to the customer

3 Develop the Outline into an Algorithm 3 Develop the Outline into an Algorithm (cont)
Pseudocode Flowchart
The solution outline developed in Step 2 is expanded Calculate_payment Start
into an algorithm (a set of precise steps that describe Set discount_rate = 0.20
exactly the tasks to be performed and the order in Prompt and Get price discount_rate = 0.20
which they are to be carried out). payment = price price*discount_rate
Prompt and Get price
Develop flow chart and pseudocode from IPO chart. Display payment
END payment = price
price*discount_rate

Display payment

End

5
1/15/2017

C program structure
4 Test the Algorithm for Correctness
#include<stdio.h>
This step is one of the most important in the development
int main(void){ of a program, and yet it is the step most often forgotten.
//printf() is to print output on screen
int a,b; The main purpose of desk checking (test the algorithm) is
to identify major logic errors early, so that they may be
float c;
easily corrected.
a=5;
b=6 ; Early error identification is better because will save time
c=4.23; and cost.
sum=a+b;
printf(Sum is:%f\n,sum);
return 0;
}

4 Test the Algorithm for Correctness (cont) 4 Test the Algorithm for Correctness (cont)

5 Code the Algorithm into a Specific


6 Run the Program on the Computer
This step uses a program compiler (a system software that
Programming Language translates the entire source program into machine language)
Only after all design considerations have been met and programmer-designed test data to machine test the code
should you actually start to code the program into for syntax errors and logic errors.
your chosen programming language (e.g. C, C++ , Java, Syntax errors
C#, Pascal, MS Visual Basic). any violation of rules of the language results in syntax error.
E.g. End of statement marker: Many languages insist on a semi-
colon ; to indicate an end-of-statement, such as,
print z printf z;

Syntax: all programming languages have a set of rules on


what is, and is not allowed when forming a line of code. In
other words, all programming languages have a grammar.

6
1/15/2017

6 Run the Program on the Computer (cont)


Logic errors
7 Document and Maintain the Program
These errors are related to logic of the program execution.
Logic errors occur when the code does not run in the way it Program documentation should not be listed as the
was intended because of the way it is written. last step in the program development process, as it is
Logic errors can be easy-to-spot blatant mistakes or subtle really an ongoing task from the initial definition of the
problems that are extremely difficult to spot. problem to the final test result
E.g. Wrong operations in a mathematical formula to
calculate length of hypotenuse of a right-angle triangle. Documentation involves both external
h = sqrt(a*a b*b); documentation and internal documentation that
h = sqrt(a*a + b*b); may have been coded in the program

Algorithms What Is an Algorithm?


A program must be systematically and properly An algorithm is like a recipe: it lists the steps involved
designed before coding begins. in accomplishing a task.

This design process results in the construction of an It can be defined in programming terms as a set of
algorithm. detailed, unambiguous and ordered instructions
developed to describe the process necessary to
produce the desired output from a given input.

Tools to build algorithm Summary


To build algorithm, we also can use: The steps in program development were introduced
Pseudocode (more English-like) and briefly described below:
1. Define the problem
Flowcharts
2. Outline the solution
Nassi-Schneiderman diagrams (not used in our course)
3. Develop the outline into an algorithm
4. Test the algorithm for correctness
All the above tools are all popular ways of representing 5. Code the algorithm into a specific programming
algorithms. language
6. Run the program on the computer
7. Document and maintain the program

You might also like