Upload
craig-burey
View
23
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Notes on programming
Citation preview
Problem Solving Presented by Sheryl Robinson
Solving a problem on a computer involves the following activities:Definition of the problemAnalyze the problem.Propose and evaluate possible solutionsDevelop an algorithm (a method) for solving the problem.Test and validate the algorithm.Implement the algorithm( ie Writing the program for the algorithm)Run the programDocument and maintain the program.
Definition of the problem
One has to look carefully and identify exactly what the problem is and write it down. This is called the PROBLEM STATEMENT.
Analyze the problem.In this step you would :identify the inputs to be used or required (INPUT)Identify the processing required to achieve correct output (PROCESS)Identify values to be stored if any (STORAGE)Identify required outputs (OUTPUT)
Analyze the ProblemThis step helps you to take a complex problem and break it into smaller or easily manageable components. It is the most important step in problem solving
Narrative AlgorithmA narrative is a representation of an algorithm where each instruction is written in everyday languageProblem statement: see question#3Step1: StartStep2: Get the two numbersStep3: Add the numbersStep4: Store the results in SumStep5: Display sumStep6: Stop
IPO Chart
InputProcessingOutputA,BSum = A+ Bsum
Propose and evaluate possible solutions
This step involves examining all the possible solutions to the problem and then select the best one ,given the resources available (e.g.. time)
Develop an algorithmAn algorithm is a finite number of accurate, unambiguous steps that solve a problemThere are many different methods used for writing algorithms. Examples of algorithms are: narrative, flowcharts, pseudocode, design structures, top down design approach, bottom up design approach, etc. (We will focus on Narrative, Flowchart and Pseudocode. )
Characteristics of a good algorithmThe number of steps must be finiteThe steps must be preciseThe steps must be unambiguousThe steps must have flow control from one process to anotherThe steps must terminateThe steps must lead to an output
Steps for Developing an algorithmThe input stepThe processing step:-AssignmentDecisionIteration (Loop)ArraysThe output step
Control structuresSequenceSelection ( if-then, if-then-else)Iteration (while loop, repeat-until loop and for loop)
SequenceThe sequence control structure is used when you have instructions to be carried out in a specific or particular order.Example:-StartRead a, bSum = a + bPrint SumStop
SelectionThe selection control structure is used in problems with instructions to be carried out if a certain condition is met. The choice of the options will be dependent on whether the condition is true or false.If < condition> thentrue resultElse False result endif
Example of selectionStartRead ScoreIf score>=75 thenPrint Congrats you have passedElsePrint "Sorry you have failedEndifStop
IterationIterations or loops are statements or instructions that get repeated. There are two types of Iterations:-Bounded Repetition of a set of instructions a fixed number of times. E.g. for-endforUnbounded Repetition of a set of instructions a number of times until a particular condition becomes false. E.g. While-endwhile and repeat-until
Example of iteration-BoundedTo accept 100 numbers and calculate their sum.StartSum = 0For I = 1 to 100 doRead xsum = sum + xEndforStop
Example of iteration-UnboundedFind the sum of a set of numbers terminated by zeroStartsum = 0Read aWhile a 0 doSum = sum + aread aEndwhileStop
Variables, Constants and literals Each storage location in memory is represented with a label called an identifierThere are two kinds of identifiers:-1. variables2. Constants
Variables and constants A variable identifies a memory location in which the item of data can be changed. E.G. num2, x, y salaryA constant identifies a memory location where a fixed item of data is stored i.e. the item of data does not change. E.G. Week = 7 , pi = 3.14
literalsLiterals are constants that are written literally as itself rather than a value.Examples:- The average is, The smallest number is, The highest grade isLiterals are normally used with the input/output instructions that appear as a message for the user.
Data typesData type indicates or tells the type of data a variable can storeA variable can store the following data type:-1. Integers( these include whole numbers, positive or negative numbers without decimal places e.g. 230, -67, 0, -78
Data types (contd) 2. Floating point or Real (these are positive or negative numbers with decimal places e.g. 0.75 or -767.84
3. Characters:- are single letters of the alphabet or symbol. E.g. a, c, k,*,#,$4. String :- is a group of characters. A string can be any number of characters e.g. I love you
Data type Activity
InformationExampleData typeVariable or constantYour nameValue of pi# of weeks in a yearTemp. outside32# of boys 28
Pseudocode AlgorithmPseudocode algorithm uses words and symbols that closely resemble computer programming language instructionsExample Problem statement# 1StartRead a, bc = aa = bb = cStop
Arithmetic Operators
OperationArithmetic operatorAddition+Subtraction-Multiplication*Division/
Exponentiation (power)^ (caret)
Finding the reminder in a division operationMOD
Relational Operators
OperationRelational OperatorGreater than>Less than=Less than or equal to
Logical Operators
OperationOperatorAndANDOrORNotNOT
TRUTH TABLES- FOR AND
ABA and BABA and BTRUETRUETRUE111TRUEFALSEFALSE100FALSETRUEFALSE010FALSEFALSEFALSE000
TRUTH TABLES- FOR OR
ABA OR BABA OR BTRUETRUE11TRUEFALSE10FALSETRUE01FALSEFALSE00
TRUTH TABLES- FOR A OR (NOT B)
ABNOT B11100100
FLOW CHARTTerminator indicates start or stopProcess indicates processingInput/output indicates the input or output of the problemDecision used to make a decision between two options ie yes or noFlow control shows the flow of dataConnector used to connect part of a flowchart that cannot fit on the page
Testing AlgorithmOne good way of testing an algorithm is to use trace tableA trace table is a table that tests an algorithm for logical errors by calculating each of the variables in your algorithm one statement at a time.
Trace table Activity -Question 34(a)
XMYZ4131442284312440-4
Top-down Design
Top-down design approach analyses the problem and breaks it into smaller manageable problems or sub-problems.Each of the sub-problem is solved separately and then combine the various solutions to solve the original problem.This approach makes it easier to solve complex problems.
Top-Down design Question: The students from Grade10G wants to create an accounting software to sell to accounting firms in JamaicaSolutionResearch the requirements of the programDesign the algorithm for the programDevelopment the program into a programming languageIntegrate the different parts Test the program to ensure that it works
Break the problem into smaller parts
Stepwise refinementStepwise refinement is a more precise way to do top-down design. This breaks down a sub-problem into smaller problems, then those smaller problems are broken into smaller problems. For example the Research the requirements of the program can be broken down into smaller problems :-One group can investigate the requirements of the Sales Dept (Accounts Receivable).Another group can investigate the requirements of the Purchasing Dept (Accounts Payables).
ArraysAn array is a variable that can store a number of elements of the same data type.Example of an array called fruits with size of 6 and data type string. The size of an array determines the number of elements it can hold
item1item2item3item4item5item6orangegrapeskiwiapplepearmango
Declaring an arrayVar : array [range] of ;E.g. Varfruits: array[16] of string;Activity: Declare the following arrays
Variable nameData typeArray sizesalaryreal20Studentnamestring10
Writing to an arrayStartScore: array[1..10] of integerFor i = 1 to 10 doPrint please enter student scoreread (score [i])EndforStop
Reading from an arrayStartScore: array[1..10] of integerFor i = 1 to 10 doWrite the student score is Writeln (score [i])EndforStop
Traversing an arrayStartScore: array[1..10]For i = 1 to 10 doPrint please enter student scoreread (score [i])EndforFor i = 1 to 10 doscore [i] =score [i]* 2Writeln (score [i])EndforStop
Searching an array linear search
For i = 1 to 10 doif score [i] = 95 thenWriteln (Score found)ElseWriteln (Score not found)endifEndfor
The EndThank you!!!!!