Key Learnings from Exams: Algorithm Strategies & Representations

Slide Note
Embed
Share

Exploring vital lessons from exams, including algorithm development tips, representation examples, and pseudo-code explanations. Understand the importance of reading questions carefully, creating precise steps, and terminating algorithms effectively. Dive into the world of algorithms, pseudo-code, and flow charts to enhance your problem-solving skills.


Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lessons we learned from the exams Damian Gordon

  2. General Tips

  3. General Tips Read the question, this might seem obvious, but several of you didn t, particularly with the following question: Develop an algorithm (with between 5 to 10 steps) for making a cup of tea. Refine each of the above steps of this algorithm (with up to 5 sub-steps) illustrating the use of sequence, condition and iteration. You write 5-10 steps, then you take each of those steps and write up to 5 sub-steps FOR EACH OF THE 5-10 STEPS.

  4. Algorithms

  5. Algorithms An algorithm is a series of instructions to achieve a particular outcome. Of note is that the series of instructions must TERMINATE to be considered an algorithm.

  6. Algorithm Representation

  7. What is this representation of an algorithm? PROGRAM BinarySearch: integer First <- 0; integer Last <- 40; boolean IsFound <- FALSE; WHILE First <= Last AND IsFound = FALSE DO Index = (First + Last)/2; IF Age[Index] = SearchValue THEN IsFound <- TRUE; ELSE IF Age[Index] > SearchValue THEN Last <- Index-1; ELSE First <- Index+1; ENDIF; ENDIF; ENDWHILE; END.

  8. PseudoCode PROGRAM BinarySearch: integer First <- 0; integer Last <- 40; boolean IsFound <- FALSE; WHILE First <= Last AND IsFound = FALSE DO Index = (First + Last)/2; IF Age[Index] = SearchValue THEN IsFound <- TRUE; ELSE IF Age[Index] > SearchValue THEN Last <- Index-1; ELSE First <- Index+1; ENDIF; ENDIF; ENDWHILE; END.

  9. What is this representation of an algorithm? START A = 1 A = A + 1 No Is A==6? Print A Yes END

  10. Flow Chart START A = 1 A = A + 1 No Is A==6? Print A Yes END

  11. What is this representation of an algorithm? 1. Organise everything together 2. Plug in kettle 3. Put teabag in cup 4. Put water into kettle 5. Turn on kettle 6. Wait for kettle to boil 7. Add boiling water to cup 8. Remove teabag with spoon/fork 9. Add milk and/or sugar 10. Serve

  12. Numbered List 1. Organise everything together 2. Plug in kettle 3. Put teabag in cup 4. Put water into kettle 5. Turn on kettle 6. Wait for kettle to boil 7. Add boiling water to cup 8. Remove teabag with spoon/fork 9. Add milk and/or sugar 10. Serve

  13. What is this representation of an algorithm? To make a cup of tea we organise everything together, Plug in kettle , Put teabag in cup, Put water into kettle, Wait for kettle to boil, Add water to cup, Remove teabag with spoon/fork, Add milk. if (sugar is required) then we add it end we serve the tea.

  14. Structured English To make a cup of tea we organise everything together, Plug in kettle , Put teabag in cup, Put water into kettle, Wait for kettle to boil, Add water to cup, Remove teabag with spoon/fork, Add milk. if (sugar is required) then we add it end we serve the tea.

  15. Sequence, Selection and Iteration

  16. What are Sequence, Selection, and Iteration? Sequence is the principle that the computer executes the code one line at a time, starting at the start of the code and executing each successive line until it gets to the end (excluding selection, iteration and methods). Example: A = 4; B = 5; Sum = A + B; Print Sum;

  17. What are Sequence, Selection, and Iteration? Sequence is the principle that the computer executes the code one line at a time, starting at the start of the code and executing each successive line until it gets to the end (excluding selection, iteration and methods). Example: A = 4; B = 5; Sum = A + B; Print Sum; you are talking about Always include an example, it shows that you understand what

  18. What are Sequence, Selection, and Iteration? Sequence is the principle that the computer executes the code one line at a time, starting at the start of the code and executing each successive line until it gets to the end (excluding selection, iteration and methods). Example: A = 4; B = 5; Sum = A + B; Print Sum; you are talking about Sequence is to do with how the computer processes a computer program, and is nothing to do with processing an array. Always include an example, it shows that you understand what

  19. What are Sequence, Selection, and Iteration? Selection is the principle that the computer can execute a choice in the code and can run one of two blocks of code depending of the outcome of that choice, and will then resume sequence. Example: IF (A > 4) THEN Print Hi ELSE Print Bye ENDIF;

  20. What are Sequence, Selection, and Iteration? Selection is the principle that the computer can execute a choice in the code and can run one of two blocks of code depending of the outcome of that choice, and will then resume sequence. Example: IF (A > 4) THEN Print Hi ELSE Print Bye ENDIF; you understand what you are talking about Always include an example, it shows that

  21. What are Sequence, Selection, and Iteration? Selection is the principle that the computer can execute a choice in the code and can run one of two blocks of code depending of the outcome of that choice, and will then resume sequence. Example: IF (A > 4) THEN Print Hi ELSE Print Bye ENDIF; you understand what you are talking about Selection is to do with how the computer processes a computer program, and is nothing to do with processing an array. Always include an example, it shows that

  22. What are Sequence, Selection, and Iteration? Iteration is the principle that the computer can execute a block of code a number of times and will continue to execute it depending of the outcome of a choice, and will then resume sequence. Example: WHILE (A > 4) DO Print A A <- A + 1; ENDWHILE;

  23. What are Sequence, Selection, and Iteration? Iteration is the principle that the computer can execute a block of code a number of times and will continue to execute it depending of the outcome of a choice, and will then resume sequence. Example: WHILE (A > 4) DO Print A A <- A + 1; ENDWHILE; you understand what you are talking about Always include an example, it shows that

  24. What are Sequence, Selection, and Iteration? Iteration is the principle that the computer can execute a block of code a number of times and will continue to execute it depending of the outcome of a choice, and will then resume sequence. Example: WHILE (A > 4) DO Print A A <- A + 1; ENDWHILE; you understand what you are talking about Iteration is to do with how the computer processes a computer program, and is nothing to do with processing an array. Always include an example, it shows that

  25. Arrays

  26. Arrays This is an array: A = [45, 32, 61, 12, 76, 23] This is NOT an array: A = 45 B = 32 C = 61 D = 12 E = 76 F = 23

  27. Arrays If you are asked to swap the successive values of an array: A = [45, 32, 61, 12, 76, 23]

  28. Arrays If you are asked to swap the successive values of an array: A = [45, 32, 61, 12, 76, 23]

  29. Arrays If you are asked to swap the successive values of an array: A = [45, 32, 61, 12, 76, 23] You only need a single loop, do don t need a double-loop, this isn t sorting the array we are just doing one pass of the array swapping each element with the successive element.

  30. Prime Numbers

  31. Prime Numbers PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A % B == 0) THEN IsPrime <- FALSE; ENDIF; B <- B 1; ENDWHILE; IF (IsPrime = FALSE) THENPrint Not Prime ; ELSEPrint Prime ; ENDIF; END.

  32. Prime Numbers The program needs a loop to make it work, you need the loop to divide the input number by every number less than it, and greater than 1. PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A % B == 0) THEN IsPrime <- FALSE; ENDIF; B <- B 1; ENDWHILE; IF (IsPrime = FALSE) THENPrint Not Prime ; ELSEPrint Prime ; ENDIF; END.

  33. Prime Numbers The program needs a loop to make it work, you need the loop to divide the input number by every number less than it, and greater than 1. PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A % B == 0) THEN IsPrime <- FALSE; ENDIF; B <- B 1; ENDWHILE; IF (IsPrime = FALSE) THENPrint Not Prime ; ELSEPrint Prime ; ENDIF; END. The condition on the IF statement is: (A % B == 0) and not (A % A -1 == 0)

  34. Prime Numbers The program needs a loop to make it work, you need the loop to divide the input number by every number less than it, and greater than 1. PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A % B == 0) THEN IsPrime <- FALSE; ENDIF; B <- B 1; ENDWHILE; IF (IsPrime = FALSE) THENPrint Not Prime ; ELSEPrint Prime ; ENDIF; END. The IF statement works as it is without any ELSE statement. It does not work if you do the following: IF (A % B == 0) THEN IsPrime <- FALSE; ELSE IsPrime <- TRUE; ENDIF; Because you are trying to remember any time there is no remainder, but you shouldn t reset it. The condition on the IF statement is: (A % B == 0) and not (A % A -1 == 0)

  35. Stacks and Queues

  36. Stacks and Queues A stack is a data structure that allows you to add to the top of the structure, and insists that you remove only from the top. It is therefore conforms to a Last In, First Out (LIFO) principle. A queue is a data structure that allows you to add to the back of the structure, and insists that you remove only from the front. It is therefore conforms to a First In, First Out (FIFO) principle.

  37. Stacks and Queues A stack is a data structure that allows you to add to the top of the structure, and insists that you remove only from the top. It is therefore conforms to a Last In, First Out (LIFO) principle. A queue is a data structure that allows you to add to the back of the structure, and insists that you remove only from the front. It is therefore conforms to a First In, First Out (FIFO) principle.

  38. Python

  39. Comments in Python Comments in Python start with a hash (#) If you are asked to comment code, you should stick a line on top of the code explaining the purpose of the program. You should add comments on the purpose of each of the main variables. You should add comments explaining the purpose of the complicated bits of the code.

  40. Printing in Python Printing needs brackets. This works print( x is bigger ) This doesn t print x is bigger

  41. Indentation in Python Python doesn t work without indentation, you ve got to get it right, otherwise the code doesn t make sense. This works if x > y: print( x is bigger ) else: print( y is bigger ) This doesn t if x > y: print( x is bigger ) else: print( y is bigger )

  42. IF Statement in Python if x > y: # THEN print( x is bigger ) else: print( y is bigger ) This doesn t #IF x > y: then: print( x is bigger ) else: print( y is bigger ) This works

  43. IF Statement in Python if x > y: # THEN print( x is bigger ) else: print( y is bigger ) This doesn t #IF x > y: then: print( x is bigger ) else: print( y is bigger ) This works

  44. etc.

Related