chapter 2: problem solving

26
CHAPTER 2: Problem Solving CSEB113 PRINCIPLES of PROGRAMMING By Badariah Solemon 1 BS (May 2013)

Upload: brendy

Post on 14-Jan-2016

59 views

Category:

Documents


3 download

DESCRIPTION

CHAPTER 2: Problem Solving. CSEB113 PRINCIPLES of PROGRAMMING By Badariah Solemon. Topics. Problem Solving and Programming Structured C Programs Development Specify the Problem Analyze the Problem Design the Solution Implement the Solution Test and Verify Solution - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER 2: Problem Solving

CHAPTER 2: Problem Solving

CSEB113 PRINCIPLES of PROGRAMMING

ByBadariah Solemon

1BS (May 2013)

Page 2: CHAPTER 2: Problem Solving

Topics1. Problem Solving and Programming2. Structured C Programs Development

a) Specify the Problemb) Analyze the Problemc) Design the Solutiond) Implement the Solutione) Test and Verify Solutionf) Provide Documentation to Solution

BS (May 2013) 2

Page 3: CHAPTER 2: Problem Solving

PROBLEM SOLVING AND PROGRAMMING

Topic 1

BS (May 2013) 3

Page 4: CHAPTER 2: Problem Solving

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 it’s important that people know how to make decisions well.

• Common steps to solve problems:1) Identify the problem2) Understand the problem3) Identify alternatives4) Select the best way to solve problem5) Solve the problem6) Evaluate the solution

BS (May 2013) 4

Page 5: CHAPTER 2: Problem Solving

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

BS (May 2013) 5

Problem solving (within the context of programming) refers to analyzing a problem with the intention of deriving a solution for the problem.

Page 6: CHAPTER 2: Problem Solving

STRUCTURED C PROGRAMS DEVELOPMENT

Topic 2

BS (May 2013) 6

Page 7: CHAPTER 2: Problem Solving

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).

BS (May 2013) 7

Specify the Problem

Analyze the Problem

Design the Solution

Implement the Solution

Test and Verify the Solution

Provide Documentation

Page 8: CHAPTER 2: Problem Solving

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:

BS (May 2013) 8

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.

Page 9: CHAPTER 2: Problem Solving

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 problem3) Developing a list of problem variables (if any)– To be discussed in Chapter 3

BS (May 2013) 9

Page 10: CHAPTER 2: Problem Solving

2: Analyze the Problem2 -

Example

BS (May 2013) 10

• Specification of Problem:

• 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

2) Input/output format:

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

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.

Line 1Line 2Line 3

Message 1

Message 2

One blank line

Page 11: CHAPTER 2: Problem Solving

3: Design the Solution1

• Mainly focuses at designing and verifiying the algorithm of the solution to solve the problem.

• An algorithm is:

• Most algorithms have 3 major steps listed in a specific order. The steps:1) Input - get the input data2) Process – such as performing the computations3) 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

A list of steps to be executed with the right order in which these steps should be executed.

Page 12: CHAPTER 2: Problem Solving

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?

• 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

Change your dressGet your beg from the study tableTake a bus to the campusGo to your class

Change your dressGet your beg from the study tableTake a bus to the campusGo to your class

Change your dressTake a bus to the campusGet your beg from the study tableGo to your class

Change your dressTake a bus to the campusGet your beg from the study tableGo to your class

Page 13: CHAPTER 2: Problem Solving

3: Design the Solution3

• Any algorithm can be described using only 3 types of orders (also called control structures): 1) Sequence2) 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

Page 14: CHAPTER 2: Problem Solving

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:

BS (May 2013) 14

BeginAlgorithm step-1Algorithm step-2Algorithm step-3...Algorithm step-n

End

Page 15: CHAPTER 2: Problem Solving

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

Page 16: CHAPTER 2: Problem Solving

3: Design the Solution6

• Flowchart – commonly used symbols:

BS (May 2013) 16

Shape Name PurposeTerminal 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 Connector

Provides continuation of logical path at another point in the same page

Off-page Connector

Provides continuation of a logical path on another page.

Flow lines Indicate the logical sequence of execution steps in the algorithm

Page 17: CHAPTER 2: Problem Solving

3: Design the Solution7 – Example 1

BS (May 2013) 17

Line 1Line 2Line 3

Message 1

Message 2

Pseudocode

BeginOUTPUT first messageOUTPUT second message

End

BeginOUTPUT first messageOUTPUT second message

End

Flowchart

Input/output format:

Page 18: CHAPTER 2: Problem Solving

3: Design the Solution8 – Example 2

BS (May 2013) 18

Line 1Line 2Line 3Line 4

Enter first integer: xxEnter second integer: xx

Sum = xx

Input/output format:

Pseudocode

BeginSET N1=0, N2=0, sum=0INPUT first number(N1)INPUT second number (N2) COMPUTE sum=N1+N2OUTPUT sum on the screen

End

BeginSET N1=0, N2=0, sum=0INPUT first number(N1)INPUT second number (N2) COMPUTE sum=N1+N2OUTPUT sum on the screen

End

Pseudocode

N1 and N2 – variables sum=N1+N2 – a formula(To be explained in Chapter 3)

Flowchart

End

INPUT first number(N1)

INPUT second number(N1)

OUTPUT sum

COMPUTEsum=N1+N2

SET N1=0, N2=0

Start

Page 19: CHAPTER 2: Problem Solving

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 you’re trying to solve.

• Regardless of the problem, you’re 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

Page 20: CHAPTER 2: Problem Solving

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:

BS (May 2013) 20

Line 1Line 2Line 3

Message 1

Message 2

PseudocodeBegin

OUTPUT first messageOUTPUT second message

End

BeginOUTPUT first messageOUTPUT second message

End

Input/output format

#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”);}

#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”);}

C program

Page 21: CHAPTER 2: Problem Solving

4: Implement the Solution2

• Another example

• Note:– An algorithm step may be converted into one or two instructions

BS (May 2013) 21

Input/output format:

Pseudocode:Begin

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

End

Input/output format:

Pseudocode:Begin

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

End

Line 1Line 2Line 3Line 4

Enter first integer: xxEnter second integer: xx

Sum = xx

C Program:#include <stdio.h>void main (void){ int N1=0, N2=0, sum=0;  printf(“Enter first integer:”); scanf(“%d”, &N1);  printf(“Enter second integer:”); scanf(“%d”, &N2);  sum = N1 + N2;  printf(“\nSum = %d\n”, sum);}

C Program:#include <stdio.h>void main (void){ int N1=0, N2=0, sum=0;  printf(“Enter first integer:”); scanf(“%d”, &N1);  printf(“Enter second integer:”); scanf(“%d”, &N2);  sum = N1 + N2;  printf(“\nSum = %d\n”, sum);}

Page 22: CHAPTER 2: Problem Solving

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

Page 23: CHAPTER 2: Problem Solving

5: Test and Verify the Solution2• Steps to test and verify:

1) Identify a set of test inputs, and expected outputs2) Execute the program with the set of test inputs3) Compare the actual outputs with the expected outputs

• There is error if the actual outputs is different than the expected output

• Example:

BS (May 2013) 23

Inputs Expected Output Actual Output Pass (y/n)?

10 Enter first integer: 10Enter second integer: 16 Sum = 26

y16

-7 Enter first integer: -7Enter second integer: 10 Sum = 3

y10

Enter first integer: 10Enter second integer: 16 Sum = 26

Enter first integer: -7Enter second integer: 10 Sum = 3

* If a program does not require any input, ensure that the output is as expected

Page 24: CHAPTER 2: Problem Solving

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)

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

BS (May 2013) 24

/*****************************************************************Author: Badariah SolemonDate: 01/01/2013Purpose: 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");}

/*****************************************************************Author: Badariah SolemonDate: 01/01/2013Purpose: 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");}

Page 25: CHAPTER 2: Problem Solving

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

Page 26: CHAPTER 2: Problem Solving

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 problem2) 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