L2: Functions, Conditionals, Problem-Solving, and Debugging

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/17

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

18 Terms

1
New cards
  1. state the problem clearly

  2. describe the input and output information

  3. work the problem (by hand or calculator) with a simple data set

  4. develop an algorithmic solution

    → use flow chart or pseudocode

  5. convert the solution to a computer program

  6. test the solution with a variety data sets

what are the six steps of problem solving

2
New cards
  1. something is known or given

  2. something is to be found, estimated or decided

  3. there is some condition to be satisfied or objective to achieve

how do we make sure we know we are solving the correct problem

3
New cards
  • syntax error → issues with syntax (i.e., the structure of our code)

  • semantic/logic errors → issues that may arise from misunderstanding the specific, or logical mistakes in our coding

    • these errors can occur at compile-time or run-time

what are the two categories of errors that we may encounter

4
New cards
  1. read the error message

  2. interpret the error message

  3. find the line where the error is occuring

  4. attempt to fix the error

  5. rerun the program to see if we have solved the error

how do we debug syntax errors

5
New cards
  • misreading or misunderstanding the specification

  • using the incorrect variable or mistyping variable names

  • forgetting about operator precedence

  • mixing up the order of a function’s input or output arguments

  • hard-coding values

  • forgetting to remove or comment out old code

what are common causes of algorithmic errors

6
New cards
  1. reread the specification

  2. identify the correct output and some intermediate values

  3. find where the values start diverging from what we expect

    • begin by sparsely looking through the program to find general vicinity of the error with one or more of the following:

      • print out some intermediate values using the disp function

      • remove the semi-colon ; operators that suppress output

      • add breakpoints and use the debugger

  4. attempt to fix the error

  5. rerun the program to see if we have resolved the error

how do we debug algorithmic errors

7
New cards
  1. Read and interpret the error message.

  2. Debug up to the line of the error with breakpoints.

  3. Identify what values (in the Workspace) might cause the error.

  4. Find where we calculated the incorrect values and place breakpoints at these lines.

  5. Debug again while stepping through breakpoints to pinpoint the error-causing line.

  6. Attempt to fix the error.

  7. Rerun the program to see if we have resolved the error.

how do we debug run-time errors

8
New cards

when your code doesn’t throw an error but isn’t producing the output you’d expect

→ these can be found by testing your code after it is written

what is a silent bug

9
New cards

create adequate tests that consider edge cases

how do we debug a code

10
New cards
  • negative values

  • very large values

  • very small values

  • special values (e.g. 0,1 and -1)

  • identical inputs

what are examples of edge cases

11
New cards
  • allows you to set breakpoints in our code which will stop the execution of a script or function at that particular point of the program

    • this allows you to see the state of the script or function at that point i.e., what variables exist and what are the values stored in them

how do the built-in debugging tools in MATLAB work

12
New cards

using the whos function

how can we see the type of variables we have in MATLAB

13
New cards
  • MATLAB has a data type known as logical

    • only has two possible values: 1 or 0

  • we can cast a variable to a logical type by using the logical function

  • a zero value will become a logical 0

  • logical 1 represents a true state

  • logical 0 represents a false state

what is a logical data type

14
New cards
  • == → equal to

    • e.g. A == B

  • ~= → not equal to

    • e.g. A ~= B

  • > → greater than

    • e.g. A > B

  • < → less than

    • e.g. A < B

  • >= → greater than or equal to

    • e.g. A >=B

  • <= → less than or equal to

    • e.g. A <= B

what relation operators are used to represent the outcomes of comparison

15
New cards
  • ~ → Find logical NOT

    • e.g. ~A

  • & → Find logical AND

    • e.g. A & B

  • | → Find logical OR

    • e.g. A | B

how do logical operators work

16
New cards
  • logical AND with short-circuit operators → A && B && C && D && E

  • logical OR with short-circuit operators → A || B || C || D || E

what are examples of short circuit logical operators

17
New cards
  • 1) Parentheses ()

  • 2) Power ^

  • 4) logical NOT ~

  • 5) Multiplication → *, division → /

  • 6) Addition → +, subtraction → -

  • 8) Equal to → ==, Not equal to → ~=, Greater than → >, Less than → <, Greater than or equal to → >=, Less than or equal to → <=

  • 9) Element-wise AND → &

  • 10) Element-wise OR → |

  • 11) Short-circuit AND → &&

  • 12) Short-circuit OR → ||

what is the operator precedence of MATLAB operators

18
New cards

if <expression>

<statements>

elseif <expression>

<statements>

else

<statements>

end

how do conditional statements work