Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Problem Solving We do it all the time Approaches: Problem Solving and Algorithms Less successful Grope blindly toward a solution Fail to complete a chain or reasoning Successful Begin with what is understood Draw on other information sources Problem Solving in CS Algorithmic thinking SJAllan 2005 Algorithm Problem Solving and Algorithms 2 Computers and Algorithms A detailed sequence of actions A computer carries out an algorithm to (instructions) performed to accomplish some task process information Named after an Iranian mathematician, Al-Khawarizmi This algorithm is called a program Languages for expressing algorithms SJAllan 2005 Problem Solving and Algorithms 3 SJAllan 2005 Problem Solving and Algorithms 4 1 Requirements of an Algorithm Useful Methodology Be well-ordered Be unambiguous Halt in a finite amount of time Be “doable” Produce a result SJAllan 2005 Problem Solving and Algorithms Assumptions? Top down design Stepwise refinement 5 SJAllan 2005 Problem Solving and Algorithms Steps in Program Development Step 1: Analyze the Problem 1. 2. 3. 4. 5. 6. 7. Output Input Given Values Processing SJAllan 2005 Analyze the problem Outline the solution Develop an algorithm Test the algorithm Code the algorithm Run and test the program Document and maintain the program. Problem Solving and Algorithms 7 SJAllan 2005 Problem Solving and Algorithms 6 8 2 Example Perform Step 1 for the following problem statement: Write an algorithm that accepts the length and width of a rectangular city lot, the length and width of the house built on that lot, and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. Step 2: Outline the Solution Top-down design Stepwise refinement Consider order What is done first, second, etc.? Is there any repetition? Are there any situations where we need to select from alternate choices? SJAllan 2005 Example Refer to the results for the following problem statement of Step 1 and perform Step 2: Write an algorithm that accepts the length and width of a rectangular city lot, the length and width of the house built on that lot and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. Problem Solving and Algorithms 10 Step 3: Design an Algorithm Constrained by what a computer can do NOTE: As you gain more experience, you may find yourself combining Steps 2 and 3. SJAllan 2005 Problem Solving and Algorithms 12 3 Step 3: Algorithm Design (cont.) Step 3: Algorithm Design (cont.) Variables CONTROL STRUCTURES Storage locations in memory Address Contents Destructive write Non-destructive read Naming conventions SJAllan 2005 Problem Solving and Algorithms Sequence Selection Repetition 13 Step 3: Algorithm Design (cont.) SJAllan 2005 Pseudo code keywords for: Input Read – from a file Get – from the keyboard Output Print, Display – to the monitor Write – to a file Prompts and Labels Keywords Operators Punctuation Problem Solving and Algorithms 14 Step 3: Algorithm Design (cont.) Elements of our pseudocode: SJAllan 2005 Problem Solving and Algorithms 15 SJAllan 2005 Problem Solving and Algorithms 16 4 Step 3: Algorithm Design (cont.) Example Assigning a value to a variable Refer to the results for the following problem statement of Steps 1 and 2 and perform Step 3 : Initialization and/or Assignment variable = value Set variable to value Initialize variable to value Write an algorithm that accept the length and width of a rectangular city lot, the length and width of the house built on that lot and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. Mathematical Operators + − ∗ / (binary) − (unary) SJAllan 2005 Problem Solving and Algorithms 17 Step 3: Algorithm Design (cont.) Step 3: Algorithm Design (cont.) Order Of Execution Truth Tables Conditions Boolean values – true or false Relational Operators Value of Condition 1 Value of Condition 2 Condition 1 And Condition 2 True True True True False False False True False False False < > == <= >= != Complex Conditions Logical Operators And – Or – Not False SJAllan 2005 Problem Solving and Algorithms 19 SJAllan 2005 Problem Solving and Algorithms 20 5 Step 3: Algorithm Design (cont.) Step 3: Algorithm Design (cont.) Truth Tables Truth Tables Value of Condition 1 Value of Condition 2 True True Condition 1 Or Condition 2 True True False True False True True False False False SJAllan 2005 Problem Solving and Algorithms 21 SJAllan 2005 Value of Condition 1 True Not Condition 1 False True False Problem Solving and Algorithms 22 Step 3: Algorithm Design (cont.) Step 3: Algorithm Design Selection Structure: two alternatives Selection Structure: one alternative If condition Then thing(s) to be done when the condition is true Else thing(s) to be done when the condition is false End If (cont.) If condition Then thing(s) to be done when the condition is true End If 6 Step 3: Algorithm Design (cont.) Selection Structure: multiple alternatives If condition 1 Then thing(s) to be done when condition 1 is true… Else If condition 2 Then thing(s) to be done when condition 2 is true Else If condition 3 Then thing(s) to be done when condition 3 is true Example The Dollar Dress Store is having a sale where the price of any item previously marked down is reduced by 30%. Design an algorithm that asks for the price of an item and prompts for entry of Yes if the item was previously marked down. The algorithm then calculates the cost of the item, calculates 6.25% sales tax, and prints out a bill. Step 3: Algorithm Design (cont.) Selection Structure: multiple alternatives (cont.) … Else If condition n Then thing(s) to be done when condition n is true Else thing(s) to be done when none of the previous conditions are true End If Example Rah-Rah University has a scholarship available for a single mother who is a fulltime student. Design an algorithm that reads a student’s name, marital status (M for married, N for not married), gender (M for male, F for female), student status (F for full-time, P for part-time), and number of children from a file and displays the student’s name and an appropriate message concerning eligibility for the scholarship. 7 Repetition Structures Counted Loop Body of the loop Counted Loops Condition-controlled Loops SJAllan 2005 Problem Solving and Algorithms Example Design an algorithm that adds the integers from 1 to 100 and displays the sum on screen. Repeat numeric value Times statement(s) to be repeated End Repeat 29 Example Change the algorithm so that it determines the sum of the integers between two numbers entered by the user. 8 Condition-Controlled Loop (cont.) Condition-controlled Loop While condition Loop condition Loop Control Variable (LCV) Identify LCV(S) and condition(s) Prime the loop Change value of LCV inside loop statements to be repeated as long as the condition is true End While Sentinel value SJAllan 2005 Example Design an algorithm that prompts for and receives pairs of integer values from a user and displays their sum, product, and average on the screen. Entry of a pair of zero values signals the end of data entry. The algorithm also prints the number of pairs entered. Problem Solving and Algorithms 34 Multiple and Nested Structures Multiple Ifs – End Ifs Multiple Repeats and/or Whiles If – End Ifs and Repeats and/or Whiles If – End If inside Then and/or inside Else If – End If inside Repeat or While Repeat or While inside Repeat or While Repeat or While inside Then and/or inside Else SJAllan 2005 Problem Solving and Algorithms 36 9 Visualization of Proper Multiple/Nested Structures Example Revise the previous algorithm so that an asterisk is displayed beside the sum of any pair that is greater than 200. NO! Example Example A file of student records contains name, gender (M or F), age (in years), and marital status (S for single or M for married). Design an algorithm that reads through the file until a name of Quit is read and computes the numbers of married men, single men, married women, and single women. Output these counts to a file called StudentSummary. Output the names of any single men over 30 to a file called OlderSingleMen. A company needs a program to process the payroll for its 15 employees.The algorithm reads an employee’s number, pay rate, and the number of hours worked. The algorithm computes the employee’s pay and prints it along with the input data. According to the company’s rules, no employee is paid for more than 60 hours and the maximum hourly rate is $25.00. If more than 35 hours are worked, then payment for the overtime hours worked is calculated at time-and-a-half. If the hours worked or hourly rate for an employee is out of range, then the input data and an appropriate message is printed and no pay is calculated. 10 Step 4: Test the Algorithm Step 5: Code the algorithm Design a set of test data Compute the results by hand Step through the algorithm Programming Languages Machine language High-level languages – C++, Java Compilers Play the role of the computer Keep track of the values stored in the variables (hand trace) Syntax Compare results to hand-calculated results SJAllan 2005 Problem Solving and Algorithms 41 Step 6: Run and Test the Program 42 Documentation Syntax Errors Run-time Errors Logic Errors Internal (comments) User (external) Maintenance Test data Problem Solving and Algorithms Problem Solving and Algorithms Step 7: Document and Maintain the Program Errors SJAllan 2005 SJAllan 2005 43 SJAllan 2005 Problem Solving and Algorithms 44 11