Vous êtes sur la page 1sur 26

CHAPTER 2: Problem Solving

CSEB113 PRINCIPLES of PROGRAMMING

By
Badariah Solemon

BS (May 2013) 1
Topics
1. Problem Solving and Programming
2. Structured C Programs Development
a) Specify the Problem
b) Analyze the Problem
c) Design the Solution
d) Implement the Solution
e) Test and Verify Solution
f) Provide Documentation to Solution

BS (May 2013) 2
Topic 1
PROBLEM SOLVING AND
PROGRAMMING

BS (May 2013) 3
Problem Solving in Everyday Life
People make decisions every day to solve problems that affect
their lives.
The problems may be as unimportant as what to watch on
television or as important as choosing a degree program.
If a bad decision is made, time and resources are wasted, so
its important that people know how to make decisions well.
Common steps to solve problems:
1) Identify the problem
2) Understand the problem
3) Identify alternatives
4) Select the best way to solve problem
5) Solve the problem
6) Evaluate the solution

BS (May 2013) 4
Programming and Problem Solving
Programming is a problem solving activity.
When you write a program, you are actually
writing instructions for the computer to solve
something for you

Problem solving (within the context of programming)


refers to analyzing a problem with the intention of deriving
a solution for the problem.

BS (May 2013) 5
Topic 2
STRUCTURED C PROGRAMS
DEVELOPMENT

BS (May 2013) 6
Overview
Programmers solve problem using a planned process.
Programs should not be created haphazardly
Programmers solve problem systematically using an
approach known as software development method
(SDM).
Specify the Problem

Analyze the Problem

Design the Solution

Implement the Solution

Test and Verify the Solution

Provide Documentation

BS (May 2013) 7
1: Specify the Problem
This activity is so critical otherwise you will solve the
wrong problem.
In this activity:
State the problem clearly and unambiguously
Identify what is needed to solve the problem
Determine what the solution should provide
Problems specification examples:
Write a program that displays Welcome to the world of programming
and where the world of black and white awaits you messages
on the screen. Display the messages in two separate lines.

Write a program that asks users to enter 3 numbers. Displays on the screen the
numbers in reverse order.
BS (May 2013) 8
2: Analyze the Problem1
1) Identifying IOFC:
Inputs to the problems, their form and the input media to
be used
Outputs expected from the problem, their form and the
output media to be used
Formula or equations to be used
Condition that must be handled by the program
2) Determining the required format in which the input
and output should be displayed
The input/output format directly affects the procedures
(also called algorithm) to solve the problem
3) Developing a list of problem variables (if any)
To be discussed in Chapter 3
BS (May 2013) 9
2: Analyze the Problem2 - Example
Specification of Problem:
Write a program that displays Welcome to the world of programming and where the
world of black and white awaits you messages on the screen. Display the messages in
two separate lines.

Problem Analysis:
1) Identify IOFC:
Input n/a
Output two lines of string messages on the screen, separated by one blank line
Formula n/a
Condition n/a
One blank line
2) Input/output format:
Line 1 Message 1
Line 2
Line 3 Message 2

3) Variables n/a
n/a: not applicable

BS (May 2013) 10
3: Design the Solution1
Mainly focuses at designing and verifiying the algorithm of
the solution to solve the problem.
An algorithm is:
A list of steps to be executed with the right order in which these steps
should be executed.

Most algorithms have 3 major steps listed in a specific order.


The steps:
1) Input - get the input data
2) Process such as performing the computations
3) Output such as displaying the result on the screen
Often, these steps and orders of an algorithm are influenced
by the input/output format of the solution
BS (May 2013) 11
3: Design the Solution2
Algorithm steps must be specified in the correct order for
execution to get the correct output .
Would you get the same result if the same set of steps are
performed in a slightly different order?
Change your dress Change your dress
Get your beg from the study table Take a bus to the campus
Take a bus to the campus Get your beg from the study table
Go to your class Go to your class

A good algorithm:
Must have output(s)
Should not be ambiguous (there should be only single interpretation 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 intended problem using the
resource currently available on the computer
BS (May 2013) 12
3: Design the Solution3
Any algorithm can be described using only 3 types of
orders (also called control structures):
1) Sequence
2) Selection*
3) Repetition*
Explanation for the selection and repetition structures are detailed in Chapter 5: Decision
Making and Looping

To solve a problem, you may use more than one control


structures in a program.
Algorithm may be represented using:
1) Pseudocode
2) flowchart

BS (May 2013) 13
3: Design the Solution4
Sequence structure:
Algorithm steps are executed sequentially one after
another from the start until the end.
The beginning and end of an algorithm often marked with
the keywords Begin and End.
The functionality is limited since it flows in single direction.
From this chapter up until Chapter 3, you will use only this
type of program structure.
The structure: Begin
Algorithm step-1
Algorithm step-2
Algorithm step-3
...
Algorithm step-n
End

BS (May 2013) 14
3: Design the Solution5
Pseudocode:
A semiformal, English-like language with limited
vocabulary that can be used to design and
describe algorithms.
A good pseudocode:
Is easy to understand, precise and clear
Gives the correct solution in all cases
Eventually ends
Flowchart:
A diagram used to depict or show the algorithm
steps using symbols.
BS (May 2013) 15
3: Design the Solution6
Flowchart commonly used symbols:
Shape Name Purpose
Terminal Indicates the beginning and end points of an
algorithm
Input/Output Shows an input or an output operation

Process Shows an algorithm step other than input,


output or selection
Selection Shows a selection process for two-way
selection
On-page Provides continuation of logical path at
Connector another point in the same page
Off-page Provides continuation of a logical path on
Connector another page.
Flow lines Indicate the logical sequence of execution
steps in the algorithm
BS (May 2013) 16
3: Design the Solution7 Example 1
Input/output format:
Line 1 Message 1
Line 2
Line 3 Message 2

Start

OUTPUT
Begin
first message
OUTPUT first message
OUTPUT second message
End
OUTPUT
Pseudocode first message

End

Flowchart
BS (May 2013) 17
3: Design the Solution8 Example 2
Input/output format:
Line 1 Enter first integer: xx
Line 2 Enter second integer: xx
Line 3 Start
Line 4 Sum = xx
SET N1=0, N2=0

INPUT first number(N1)


Begin
SET N1=0, N2=0, sum=0
INPUT first number(N1) INPUT second number(N1)
INPUT second number (N2)
COMPUTE sum=N1+N2 COMPUTE
Pseudocode
OUTPUT sum on the screen sum=N1+N2

End
OUTPUT sum
Pseudocode
End

N1 and N2 variables
sum=N1+N2 a formula
Flowchart
(To be explained in Chapter 3)
BS (May 2013) 18
3: Design the Solution9
In general, writing algorithm is a difficult task
especially for someone new to programming.
The difficulty level often depends on the complexity of the
problem that youre trying to solve.
Regardless of the problem, youre highly
recommended to use an approach called top-down
design, where:
1) List the major steps of your program.
2) Perform step-refinement by breaking-down the steps into
a more detailed list of steps.
3) Verify that the algorithm works as intended by
performing desk-check (Manually read and inspect the algorithm
steps line by line)
BS (May 2013) 19
4: Implement the Solution1
This activity:
involves implementing the designed algorithm by writing a
complete program using a programming language (in our
case, using C language).
depends on the input/output format
Example:
Input/output format
Line 1 Message 1 C program
Line 2 #include <stdio.h>
Line 3 Message 2 void main (void)
{
printf(Welcome to the world of programming\n\n);
Pseudocode
Begin printf(where the world of black and white awaits you);
}
OUTPUT first message
OUTPUT second message
End

BS (May 2013) 20
4: Implement the Solution2
Another example
Input/output format: C Program:
Line 1 Enter first integer: xx #include <stdio.h>
Line 2 Enter second integer: xx void main (void)
Line 3 {
Line 4 Sum = xx int N1=0, N2=0, sum=0;

Pseudocode: printf(Enter first integer:);


scanf(%d, &N1);
Begin
SET N1=0, N2=0, sum=0 printf(Enter second integer:);
INPUT first number(N1) scanf(%d, &N2);
INPUT second number (N2)
COMPUTE sum=N1+N2 sum = N1 + N2;
OUTPUT sum on the screen
End printf(\nSum = %d\n, sum);
}

Note:
An algorithm step may be converted into one or two instructions
BS (May 2013) 21
5: Test and Verify the Solution1
This activity involves:
1) Program testing - the process of compiling and
executing a program to demonstrate its
correctness.
Errors or mistakes may occur in your program as well as
in your input/output.
The process of looking for and correcting errors or
mistakes that cause your programs to behave
unexpectedly is called debugging (Chapter 2)
2) Program verification - the process of ensuring
that a program meets user requirements (i.e.,
solve the problem).
BS (May 2013) 22
5: Test and Verify the Solution2
Steps to test and verify:
1) Identify a set of test inputs, and expected outputs
2) Execute the program with the set of test inputs
3) Compare the actual outputs with the expected outputs
There is error if the actual outputs is different than the expected output

Example:
Inputs Expected Output Actual Output Pass (y/n)?
10 Enter first integer: 10 Enter first integer: 10
Enter second integer: 16 Enter second integer: 16
16 y
Sum = 26
Sum = 26
-7 Enter first integer: -7 Enter first integer: -7
Enter second integer: 10 Enter second integer: 10
10 y
Sum = 3
Sum = 3
* If a program does not require any input, ensure that the output is as expected
BS (May 2013) 23
6: Provide Documentation to Solution1
In this activity:
provide appropriate information to your
program/software either by:
1) Writing comments between your line of instructions
(detailed in Chapter 2)
/*****************************************************************
Author: Badariah Solemon
Date: 01/01/2013
Purpose: Print two lines of messages separated by one blank line
******************************************************************/
#include <stdio.h>
void main (void)
{
printf("Welcome to the world of programming\n\n");

printf("where the world of black and white awaits you");


}

2) Creating a separate text file to explain the program.


BS (May 2013) 24
6: Provide Documentation to Solution1
Important because:
You may return to this program in future to use the whole
of or a part of it again
Other programmer or end user will need some information
about your program for reference or maintenance
You may someday have to modify the program, or may
discover some errors or weaknesses in your program
Without proper documentation, you and others may
have problem because believe me, you will forget the
details of your own program after sometime!

BS (May 2013) 25
Summary
Programming is a problem solving activity.
Problem solving refers to analyzing a problem with the intention of
deriving a solution for the problem.
Programmers solve problem systematically using an approach
known as software development method (SDM).
6 main activities of SDM:
1) Specify the problem
2) Analyze the problem (identify IOFC, determine input/output format,
specify variables)
3) Design the solution (program control structures sequence,
selection, repetition using pseudocode or flowchart)
4) Implement the solution (write the program)
5) Test and verify the solution (program testing and verification)
6) Provide documentation to the solution (comments or separate text
file) BS (May 2013) 26

Vous aimerez peut-être aussi