1 / 13

Computational Algorithms

Computational Algorithms. ...a lightning introduction to real-world algorithms. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…

ewalter
Download Presentation

Computational Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computational Algorithms ...a lightning introduction to real-world algorithms David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr

  2. IMPORTANT… • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! • Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you,David.

  3. Example… Exam average problem

  4. Exam average problem Requirements: Given a set of already graded exam papers,compute and report the average grade. Algorithm • Print welcome message • Given the set of exam papersfind the number of papers and the sum of all the grades on the papers • Compute average grade as sum of all the grades / number of papers • Report the average grade • Print “all done” message

  5. Exam average problem (avoiding divide-by-zero error) Revised Algorithm 1. Print welcome message 2. Given the set of exam papers find the number of papers and the sum of all the grades on the papers 3. ifnumber of papers is zero then3T Print msg “no grades entered” else 3F.1 Compute average grade as sum of all the grades / number of papers 3F.2 Report the average grade 4. Print “all done” message

  6. Exam average problem Solve… • Given the set of exam papersfind the number of papers and the sum of all the grades on the papers • Ask user for & get the number of papers • For each paper read the grade from the paper and add it to the sum of grades so far. • Sum of all the grades is now sum of grades so far

  7. Exam average problem Solve… (Alternative) • Given the set of exam papersfind the number of papers and the sum of all the grades on the papers • Set count of papers so far to zero, then for each paper add one to the count of papers so far. • Set sum of grades so far to zero, and then for each paper, read the grade from the paper and add it to the sum of grades so far. • Number of papers is now count of papers so far • Sum of all grades is now sum of grades so far

  8. Exam average problem Solve… (Yet another alternative) • Given the set of exam papersfind the number of papers and the sum of all the grades on the papers • Set count of papers so far to zero • Set sum of grades so far to zero • For each paper, read the grade from the paper, add it to the sum of grades so far and add one to the count of papers so far • Number of papers is now count of papers so far • Sum of all grades is now sum of grades so far

  9. Exam average problem Solve… (& yet another alternative ~ rewritten) • Given the set of exam papersfind the number of papers and the sum of all the grades on the papers 1. Set count of papers so far to zero 2. Set sum of grades so far to zero 3. For each paper 3.1 read the grade from the paper, 3.2 add grade to the sum of grades so far 3.3 add one to the count of papers so far 4. Number of papers is now count of papers so far 5. Sum of all grades is now sum of grades so far

  10. Of note…

  11. Sentence forms - control • Sequence • “Do this and then do that and then do the other and …” • Decision/alternation • “if this condition is true then do this else do that” • “if this condition is true then do this” • Repetition • “for each/every/all do this” • “repeat this until condition is true” • “while this condition is true do this” • “do this while condition holds”

  12. Sentence forms - data • Input • “get value from user” • Computation • “compute this as function of that, that, & …” • Output • “print this message” • “print/report this value” ERRORS • Syntax • Logical • Run-time

  13. Sequence Decision • step • step • step • step n. if condition then nT step else nF step indent Repetition n. while condition do step n. do step while condition indent indent Types & Layout(of algorithm steps) Any step can be replaced with one of the other types or input, output, assignment. n. for so many times do step

More Related