Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What does this code do? Debugging required because the output calculated is inco

ID: 3861181 • Letter: W

Question

What does this code do? Debugging required because the output calculated is incorrct. (For example: Input of two numbers, 2 and 0 gives the output of 2400) How does this happen?

ORG 100
Input a       /first factor
Store a       /store the factor
Input Ctr   /second factor used to control loop
Store Ctr   /store second factor
Load Ctr   /reload second factor to assure it is not zero
Skipcond 800   /check if the second factor is greater than zero
Jump End   /jump to end if the second factor is zero, product will be 0
Loop,   Load Ctr   /reload second factor
Subt One   /Subtract the factor by 1 through each loop iteration
Store Ctr   /Store second factor
Load Sum   /Load the sum of addition operation; initially zero
Add a       /add first factor to sum
Store Sum    /store sum
Load Ctr   /load second factor to check its state
SKipcond 400   /stop the loop if the second factor is equal to zero
Jump Loop    /run the loop if the second factor is not equal to zero
Load Sum    /load sum for output
End,    Output    /output final "product"
Halt       /terminate program

a, Dec 0    /first factor
Ctr, Dec 0   /second factor
Sum, Dec 0    /sum of repeated addition
One, Dec 1    /used to decrement the second factor

Explanation / Answer

Understand the difference between testing and debugging

Know the three categories of program errors

Understand the importance and limitations of testing

Know general testing strategies for small programs

Be able to use MATLAB's debugger

Lesson:

I. Programming Concepts

A. Program Errors

Most large programs contain errors.

Creating error-free code is very difficult.

There are three types of program errors:

syntax errors

run-time errors

logic errors

A syntax error is a violation of the rules of the programming language.

The MATLAB interpreter catches syntax errors and displays a red error message beginning with ??? .

MATLAB programs are checked for syntax errors before execution of the program begins. A single syntax error will prevent your program from executing.

If you click on the syntax error "hyper-link," the cursor in the editor window will be placed in the exact spot where MATLAB recognized a syntax problem. The syntax error is on the line indicated by the cursor, or possibly on previous lines. The syntax error will never be below the indicated line.

Syntax errors are relatively easy to find and fix.

A run-time error is an error that results from using invalid operand values for an operation.

The following statement generates a run-time error if Count has the value zero, since division by zero is not mathematically defined.

Average = Total/Count;

The MATLAB interpreter usually displays a warning message when a run-time error occurs, but does not halt the running program.

In most cases, any calculations or processing after a run-time error will produce incorrect results. NEVER SIMPLY IGNORE run-time error messages.

A logic error in a program is any code that causes incorrect output/results even though the program runs to completion.

The following code contains a logic error because the > should be >=

if Age > 18
     fprintf('You are old enough to vote! ')
end

A program with a logic error may give the correct answer sometimes and the wrong answer other times.

Logic errors typically are the most difficult type of errors to find and correct.

Finding logic errors is the primary goal of testing.

Some common errors for novice MATLAB programmers include:

Using two different names for the same variable, e.g., Count and count .

Not providing the correct arguments to a function, e.g., fprintf('Age = %d ');

Forgetting semicolons at the end of an assignment statement.

Using = for equality testing instead of ==.

Putting a logical expression after an else .

Forgetting to initialize or modify a loop control variable.

Many more -- too numerous to list!

Error-free code is not necessarily good code. Good code also should be readable, maintainable and reasonably efficient.

B. Program Testing

Testing is the process of finding errors ("bugs") in a program.

Testing can increase your confidence that a program is error-free.

Testing can find the presence of errors, but, in general, cannot prove the absence of errors.

Testing small programs is much easier than testing large programs.

When testing code with branches, provide inputs that test all code sections of the if statement.

You should test your program with expected and unexpected (invalid) inputs:

You should know how your program will perform with good data as well as bad data.

Unexpected values may be negative, extremely large, or extremely small values.

You should test your program with boundary conditions values, i.e., values at and around a value for which the behavior of a program should change:

For example, good boundary condition test values for Age in the code above would be 17, 18, and 19.

For real valued boundaries like 98.6, test with close real values like 98.5999, 98.6, and 98.6001.

When testing loops, provide inputs that cause the loop to repeat zero, one, and many times.

C. Program Debugging

Debugging is the process of locating and correcting errors in a program.

The first case of computer program debugging involved removing a moth from the Navy's MARK II computer in 1947.

Debugging is problem-solving and often can be very challenging.

Thinking carefully about your program is often the best first step when debugging.

You can help minimize your time spent debugging by:

Starting with a good program design,

Coding carefully, and

Testing your program as you write.

The development of good debugging skills comes from experience. Therefore, make sure you learn from your mistakes.

It often helps to have someone else look at your code when debugging -- especially your instructor :-)

General rule for all debugging:

"When in doubt, print it out"

A symbolic debugger is a tool for debugging code that allows you to examine your program's behavior as it executes.

D. Incremental Code Development

Basic principle: It is much easier to test and debug small sections of code than large sections of code.

Test your code as you are writing it - every few lines.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote