Problem Solving and Algorithms Problem Solving We do it all the time Approaches: 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 Problem Solving and Algorithms 2 Algorithm A detailed sequence of actions (instructions) performed to accomplish some task Named after an Iranian mathematician, Al-Khawarizmi Languages for expressing algorithms Computers and Algorithms A computer carries out an algorithm to process information This algorithm is called a program 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 Assumptions? Top down design Stepwise refinement SJAllan 2005 Problem Solving and Algorithms 5 SJAllan 2005 Problem Solving and Algorithms 6 Steps in Program Development 1. Analyze the problem 2. Outline the solution 3. Develop an algorithm 4. Test the algorithm 5. Code the algorithm 6. Run and test the program 7. Document and maintain the program. Step 1: Analyze the Problem Output Input Given Values Processing SJAllan 2005 Problem Solving and Algorithms 7 SJAllan 2005 Problem Solving and Algorithms 8 2
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 Problem Solving and Algorithms 10 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. 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
Variables Storage locations in memory Address Contents Destructive write Non-destructive read Naming conventions CONTROL STRUCTURES Sequence Selection Repetition SJAllan 2005 Problem Solving and Algorithms 13 SJAllan 2005 Problem Solving and Algorithms 14 Elements of our pseudocode: Keywords Operators Punctuation 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 SJAllan 2005 Problem Solving and Algorithms 15 SJAllan 2005 Problem Solving and Algorithms 16 4
Assigning a value to a variable Initialization and/or Assignment variable = value Set variable to value Initialize variable to value Mathematical Operators + / (binary) (unary) Refer to the results for the following problem statement of Steps 1 and 2 and perform Step 3 : 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. SJAllan 2005 Problem Solving and Algorithms 17 Order Of Execution Truth Tables Conditions Boolean values true or false Relational Operators < > == <= >=!= Complex Conditions Logical Operators Value of Condition 1 Value of Condition 2 Condition 1 And Condition 2 And Or Not SJAllan 2005 Problem Solving and Algorithms 19 SJAllan 2005 Problem Solving and Algorithms 20 5
Truth Tables Truth Tables Value of Condition 1 Value of Condition 2 Condition 1 Or Condition 2 Value of Condition 1 Not Condition 1 SJAllan 2005 Problem Solving and Algorithms 21 SJAllan 2005 Problem Solving and Algorithms 22 Selection Structure: two alternatives If condition Then the condition is true Else the condition is false End If Selection Structure: one alternative If condition Then the condition is true End If 6
Selection Structure: multiple alternatives If condition 1 Then condition 1 is true Else If condition 2 Then condition 2 is true Else If condition 3 Then condition 3 is true Selection Structure: multiple alternatives (cont.) Else If condition n Then condition n is true Else none of the previous conditions are true End If 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. 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 Body of the loop Counted Loops Condition-controlled Loops Counted Loop Repeat numeric value Times statement(s) to be repeated End Repeat SJAllan 2005 Problem Solving and Algorithms 29 Design an algorithm that adds the integers from 1 to 100 and displays the sum on screen. Change the algorithm so that it determines the sum of the integers between two numbers entered by the user. 8
Condition-controlled Loop While condition statements to be repeated as long as the condition is true End While Condition-Controlled Loop (cont.) Loop condition Loop Control Variable (LCV) Identify LCV(S) and condition(s) Prime the loop Change value of LCV inside loop Sentinel value SJAllan 2005 Problem Solving and Algorithms 34 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. 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 Revise the previous algorithm so that an asterisk is displayed beside the sum of any pair that is greater than 200. NO! 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 Design a set of test data Compute the results by hand Step through the algorithm Play the role of the computer Keep track of the values stored in the variables (hand trace) Compare results to hand-calculated results Step 5: Code the algorithm Programming Languages Machine language High-level languages C++, Java Compilers Syntax SJAllan 2005 Problem Solving and Algorithms 41 SJAllan 2005 Problem Solving and Algorithms 42 Step 6: Run and Test the Program Errors Syntax Errors Run-time Errors Logic Errors Test data Step 7: Document and Maintain the Program Documentation Internal (comments) User (external) Maintenance SJAllan 2005 Problem Solving and Algorithms 43 SJAllan 2005 Problem Solving and Algorithms 44 11