150 likes | 260 Views
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
E N D
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
Syntax ERRORS – easy fix Where are the syntax errors? Red Underlined Error messages!
Runtime ERRORS Where are the runtime errors? • R-E-A-D the error message!
Logic errors • Still the programmer’s fault… but… MATLAB will NEVER give error messages for those. • What is wrong here?
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)
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?
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?
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?
Step2: Diagram • “A picture is worth a thousand words”
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
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
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!
Algorithm • ALWAYS start with a basic plan It will of course get better as you go…
The code… • The following code has: Syntax errors Runtime errors Logic errors • Find as many as you can!