1 / 15

Exam1 – Coding Review

Exam1 – Coding Review. Various type of ERRORS. Syntax error “Grammar, spelling” Example: Wrong symbols: commas, semi-colons, quotes… Run time error MATLAB won’t crash until it RUNS that particular line… (interpreted) Example: Variables didn’t exist, misspelled Logic error

edmund
Download Presentation

Exam1 – Coding Review

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. Exam1 – Coding Review

  2. Various type of ERRORS • Syntax error • “Grammar, spelling” Example: Wrong symbols: commas, semi-colons, quotes… • Runtime error • MATLAB won’t crash until it RUNS that particular line… (interpreted) Example: Variables didn’t exist, misspelled • Logic error • Wrong symbols, wrong order of operations… etc Example: < instead of >, && instead of ||, checking <0 THEN <-5 Example: coding x/y+z instead of x/(y+z) Example: misplacing the end keywords

  3. Syntax ERRORS – easy fix Where are the syntax errors? Red Underlined Error messages!

  4. Runtime ERRORS Where are the runtime errors? • R-E-A-D the error message!

  5. Logic errors • Still the programmer’s fault… but… MATLAB will NEVER give error messages for those. • What is wrong here?

  6. And then… There’s just bad/horrible habits, such as: • Poor indent • ; on lines that do not want them • Output that are so ugly that no user would want to run/use the code • Keywords that are not necessary (else) • Redundant conditions (if)

  7. Example Problem: A house requires a certain amount of energy (kJ/day) to heat the house during winter. There are 3 possible source of energy: The homeowner only uses one source of energy. Knowing how many kilograms the homeowner has left of that source, how efficient that source is (10-100%), and how many days there are until the next delivery, determine: • how many kilograms are necessary to last until the next delivery • if there a need to order an emergency delivery?

  8. Step1: Givens Problem: A house requires a certain amount of energy (kJ/day) to heat the house during winter. There are 3 possible source of energy: The homeowner only uses one source of energy. Knowing how many kilograms the homeowner has left of that source, how efficient that source is (10-100%), and how many days there are until the next delivery, determine: • how many kilograms are necessary to last until the next delivery • if there a need to order an emergency delivery?

  9. Step1: Find Problem: A house requires a certain amount of energy (kJ/day) to heat the house during winter. There are 3 possible source of energy: The homeowner only uses one source of energy. Knowing how many kilograms the homeowner has left of that source, how efficient that source is (10-100%), and how many days there are until the next delivery, determine: • how many kilograms are necessary to last until the next delivery • if there a need to order an emergency delivery?

  10. Step2: Diagram • “A picture is worth a thousand words”

  11. Step3 - Equations • Equations/Theory Where Energy is in kJ/day, efficiency is a decimal value, and heatingValue is in kJ/kg. DailyQuantity will be in kg/day. If that kgNeeded is greater than the reserves, an order of energy is required! • Step4: no assumptions necessary

  12. Step5 • Assume all givens: Energy = 500,000 kJ/day Energy source is 1, which is wood, which has 20,470 kJ/kg Kilograms left = 180kg Days until next delivery = 20days Efficiency = .75 (75%) DailyQuantity = 500,000/(.75*20,470) =32.57 kg/day kgNeededToLast = 32.57*20 = 651.36 kg Since homeowner has 180kg, that’s not good!!! Emergency!!! • Step6: all looks plausible

  13. Requirements • Prompt for all givens • For the energy source, use characters (w) for wood, (c) for coal…etc… • Error proof code. As soon as one input is invalid, tell user. • Determine/display the kilograms needed to survive • Tell the user whether s/he should re-order as an emergency!

  14. Algorithm • ALWAYS start with a basic plan It will of course get better as you go…

  15. The code… • The following code has: Syntax errors Runtime errors Logic errors • Find as many as you can!

More Related