1 / 26

CHAPTER 2: Problem Solving

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

brendy
Télécharger la présentation

CHAPTER 2: Problem Solving

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2: Problem Solving CSEB113 PRINCIPLES of PROGRAMMING By BadariahSolemon BS (May 2013)

  2. 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 • Provide Documentation to Solution BS (May 2013)

  3. Topic 1 Problem solving and programming BS (May 2013)

  4. 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: • Identify the problem • Understand the problem • Identify alternatives • Select the best way to solve problem • Solve the problem • Evaluate the solution BS (May 2013)

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

  6. Topic 2 Structured C Programs Development BS (May 2013)

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

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

  9. 2: Analyze the Problem1 • 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 • 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 • Developing a list of problem variables (if any) • To be discussed in Chapter 3 BS (May 2013)

  10. 2: Analyze the Problem2 -Example • Specification of Problem: • Problem Analysis: • 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 • Input/output format: • 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. One blank line BS (May 2013)

  11. 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: • Input - get the input data • Process – such as performing the computations • 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 A list of steps to be executed with the right order in which these steps should be executed. BS (May 2013)

  12. 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 Change your dress Get your beg from the study table Take a bus to the campus Go to your class Change your dress Take a bus to the campus Get your beg from the study table Go to your class BS (May 2013)

  13. 3: Design the Solution3 • Any algorithm can be described using only 3 types of orders (also called control structures): • Sequence • Selection* • 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: • Pseudocode • flowchart BS (May 2013)

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

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

  16. 3: Design the Solution6 • Flowchart – commonly used symbols: BS (May 2013)

  17. Start OUTPUT first message OUTPUT first message End 3: Design the Solution7 – Example 1 Input/output format: • Begin • OUTPUT first message • OUTPUT second message • End Pseudocode Flowchart BS (May 2013)

  18. 3: Design the Solution8 – Example 2 Input/output format: Start SET N1=0, N2=0 INPUT first number(N1) • Begin • SET N1=0, N2=0, sum=0 • INPUT first number(N1) • INPUT second number (N2) • COMPUTE sum=N1+N2 • OUTPUT sum on the screen • End INPUT second number(N1) COMPUTE sum=N1+N2 Pseudocode OUTPUT sum Pseudocode End N1 and N2 – variables sum=N1+N2 – a formula (To be explained in Chapter 3) Flowchart BS (May 2013)

  19. 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: • List the major steps of your program. • Perform step-refinement by breaking-down the steps into a more detailed list of steps. • Verify that the algorithm works as intended by performing desk-check (Manually read and inspect the algorithm steps line by line) BS (May 2013)

  20. 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 C program • #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”); • } Pseudocode • Begin • OUTPUT first message • OUTPUT second message • End BS (May 2013)

  21. 4: Implement the Solution2 • Another example • Note: • An algorithm step may be converted into one or two instructions • Input/output format: • Pseudocode: • Begin • SET N1=0, N2=0, sum=0 • INPUT first number(N1) • INPUT second number (N2) • COMPUTE sum=N1+N2 • OUTPUT sum on the screen • End 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); } BS (May 2013)

  22. 5: Test and Verify the Solution1 • This activity involves: • 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) • Program verification - the process of ensuring that a program meets user requirements (i.e., solve the problem). BS (May 2013)

  23. 5: Test and Verify the Solution2 • Steps to test and verify: • Identify a set of test inputs, and expected outputs • Execute the program with the set of test inputs • Compare the actual outputs with the expected outputs • There is error if the actual outputs is different than the expected output • Example: Enter first integer: 10 Enter second integer: 16 Sum = 26 Enter first integer: -7 Enter second integer: 10 Sum = 3 * If a program does not require any input, ensure that the output is as expected BS (May 2013)

  24. 6: Provide Documentation to Solution1 • In this activity: • provide appropriate information to your program/software either by: • Writing comments between your line of instructions (detailed in Chapter 2) • Creating a separate text file to explain the program. /***************************************************************** 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"); } BS (May 2013)

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

  26. 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: • Specify the problem • Analyze the problem (identify IOFC, determine input/output format, specify variables) • Design the solution (program control structures – sequence, selection, repetition using pseudocode or flowchart) • Implement the solution (write the program) • Test and verify the solution (program testing and verification) • Provide documentation to the solution (comments or separate text file) BS (May 2013)

More Related