Presentation on theme: "Chapter 2: Problem Solving"— Presentation transcript:
1Chapter 2: Problem Solving Software development methodspecification of needsproblem analysisdesign and algorithmic representationimplementationtesting and verificationdocumentation
2Introduction to Problem Solving Problem solving is a process of transforming the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools.Computers can be used to help us in doing problem solving.
3Specification of Needs To understand exactly:what the problem iswhat is needed to solve itwhat the solution should provideif there are constraints and special conditions.
4Problem AnalysisIn the analysis phase, we should identify the following:Inputs to the problem, their form and the input media to be usedOutputs expected from the problem, their form and the output media to be usedSpecial constraints (if any)Formulas or equations to be used
5Design and Algorithmic Representation An algorithm is a sequence of a finite number of steps arranged in a specific logical order which, when executed, produces the solution for a problem.An algorithm must satisfy these requirements:It must have an inputIt must have an outputIt should not be ambiguous (there should not be different interpretations to it)
6An algorithm can be represented using pseudocodes or flowcharts. It must be general (it can be used for different inputs)It must be correct and it must solve the problem for which it is designedIt must execute and terminate in a finite amount of timeIt must be efficient enough so that it can solve the intended problem using the resource currently available on the computerAn algorithm can be represented using pseudocodes or flowcharts.
7PseudocodesA pseudocode is a semiformal, English-like language with limited vocabulary that can be used to design and describe algorithms.Criteria of a good pseudocode:Easy to understand, precise and clearGives the correct solution in all casesEventually endsIn 1966, two researchers, C. Bohn and G. Jacopini, demonstrated that any algorithm can be described using only 3 control structures: sequence, selection and repetition.
8The sequence control structure A series of steps or statements that are executed in the order they are written in an algorithm.The beginning and end of a block of statements can be optionally marked with the keywords begin and end.Example:beginread birthDateage = today’s date - birth dateprint ageendThe selection control structureDefines two courses of action depending on the outcome of a condition. A condition is an expression that is either true or false.
9The repetition control structure The keyword used are if and else.Example:if age > 55print “sudah pencen”elseprint “kerja lagi”end_ifThe repetition control structureSpecifies a block of one or more statements that are repeatedly executed until a condition is satisfied.The keyword used is while.
10Example:while user still wants to playbeginSelect either to play on network or play against computerif play on networkcreate connection to remote machineplay game with connected computerelseselect missionplay game locallyend_ifAsk user whether he/she still wants to playendend_while
11FlowchartsFlowcharts is a graph used to depict or show a step by step solution using symbols which represent a task.The symbols used consist of geometrical shapes that are connected by flow lines.
12Terminal symbol - indicates the beginning and end points of an algorithm.Process symbol - shows an instruction other thaninput, output or selection.Input-output symbol - shows an input or an outputoperation.Disk storage I/O symbol - indicates input from oroutput to disk storage.Printer output symbol - shows hardcopy printeroutput.
13Selection symbol - shows a selection process for two-way selection.Off-page connector - provides continuationof a logical path on another page.On-page connector - provides continuationof logical path at another point in the samepage.Flow lines - indicate the logical sequence ofexecution steps in the algorithm.
14Begin End Play on network? Create connection Select mission Play game TrueFalsePlay on network?Create connectionSelect missionPlay gameremotelyPlay gamelocallyTruePlay again?FalseEnd
15ImplementationThe process of implementing an algorithm by writing a computer program using a programming language (for example, using C language)The output of the program must be the solution of the intended problem
16Testing and Verification Program testing is the process of executing a program to demonstrate its correctnessProgram verification is the process of ensuring that a program meets user-requirementAfter the program is compiled, we must run the program and test/verify it with different inputs before the program can be released to the public or other users (or to the instructor of this class)
17DocumentationContains details produced at all stages of the program development cycle.Can be done in 2 ways:Writing comments between your line of codesCreating a separate text file to explain the programImportant not only for other people to use or modify your program, but also for you to understand your own program after a long time (believe me, you will forget the details of your own program after sometime)
18Although documentation is listed as the last stage of software development method, it is actually an on-going process which should be done from the very beginning of the software development process.
19A Very Simple Example PROBLEM You are required to develop a complete system which will enable the sum of two values to be calculated.What do we have to do first?
20Calculating Electricity Bills The unit for electricity usage is kWh. For domestic usage, the tariff used is tariff A. The monthly rate for tariff A is 21.8 cents/unit for the first 200 unit, 25.8 cents/unit for the next 800 units and 27.8 cents/unit for each additional units. Given the amount of electricity units (in kWh) used by a customer, compute and print the amount of money needs to be paid by the customer to TNB.
21Calculating Electricity Bills BeginPrint “Enter the usage (unit): ”Read usageif (usage > 200)payment = 200 x 21.8usage = usage – 200if (usage > 800)payment = payment + (800 x 25.8)usage = usage - 800payment = payment + (usage x 27.8)elsepayment = payment + (usage x 25.8)end_ifpayment = usage x 21.8Print “Total payment is “, paymentEnd
22Calculating Uniten Tuition Fee This program will calculate the total tuition fee for a UNITEN student. The program ask for total number of subject registered. Then it will ask for the number of credit hours for each subject. The program will print out the total tuition fee in RM. Assume that tuition fee is RM300 per credit hour
23Calculating Uniten Tuition Fee Print “Enter total number of subject: “Read totalSubjectcounter = 0totalFee = 0while (counter < totalSubject)Print “Enter the number of credit of the subject: “Read credittotalFee = totalFee + (credit x 300)counter = counter + 1end_whilePrint “The total fee is “, totalfee
24Exercise1. Write pseudocode of a program that calculate the total sum of series starting from 1 to a number specified by user. Example, if user input 4, the result is =10.2. Write pseudocode of a program that will print the net income of a salesman in ABC Bhd given the total sales of a month. The basic salary is RM500. The commission is 5% of the first RM50, sales, 10% for next RM200, sales and 20% for the remaining. A sum of 11% of the gross income will be deducted for KWSP contribution.
25Answer Begin Print “Enter last number in the series: “ Read lastNumber counter = 1sum = 0while (counter<=lastNumber) dosum = sum + countercounter = counter + 1end_whilePrint “The sum of the series is “, sumEnd
26Answer Begin Print “Enter monthly sales: “ Read sales income = 500 if (sales>50000)income = income + (0.05 x 50000)sales = salesif (sales>200000)income = income + (0.1 x )sales = salesincome = income + (0.2 x sales)elseincome = income + (0.1 x sales)end_ifincome = income + (0.05 x sales)income = income – (income x 0.11)Print “The net income is “, incomeEnd