Homework 5 Part 2 - Debugging
Instructions: 5 programs have been provided for you. For each program, identify all errors you
can find. Classify each error either as a syntax, runtime, or logical error. Explain what is causing
the error and describe what needs to be changed to fix the error (if there is more than one way to
fix the error, you just need to describe one way). Type your answers below each question in
sentence format.
1) Exam scores
% test scores
scores = [88 92 75 63 95 57 68 81 73 99 84 91 77 60 89 54 79];
% determines baseline for passing score
passed = scores > 60
% Calculate the percentage of students who passed
passPercentage = (passedSum / length(scores)) * 100;
fprintf('%d students passed the exam.\n', passedSum);
fprintf('Percentage of students who passed: %.2f%%\n',...
passPercentage);
% Check if each score lies within a certain range
for i == 1 to length(scores)
if 90 <= scores(i) <= 100
fprintf(“Great job!\n”)
elseif scores < 10
print(Let’s talk about some study strategies.\n)
end
runtime error = passedSum is an undefined variable, should use
Sum
syntax error = for a ‘for’ loop you should use = instead of ==
logic error = need to separate logical statements so it should be
if scores(i) >= 90 && scores(i) <= 100
runtime error = print instead of fprintf
syntax error = no quotations around the string in the print
function
2) 5k Personal Best Time
funtion [record, improvement] == runnerStats(time, personalBest)
% function that determines if a runner achieved a new record
if time <= personalBest
disp('thats fast! you beat your personal best.')
record = 1;
if tine = personalBest
disp('wow! you matched your personal best.')
record = 0;
else
disp('keep trying! you''ll get em next time.')
record = 0;
end
syntax error = function is misspelled in the first line
syntax error = the first line should use = instead of ==
syntax error = there should be two ‘end’ to close both of the if
statements
syntax error = time is misspelled in the second if statement
syntax error = it should be time == personalBest in the second if
statement not time = personalBest
3) Matrix Manipulations
mat = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
% Index each value across each row and column. If a value is
% equal to 9, change that value to 0. If a value is not equal
% to 9, add it to a vector named notNine
for i = 1:size(matrix,1)
for j = 1:sze(matrix,2)
if mat(i,j) = 9
mat(i,:) = 0;
else
notNine(end+1) = mat(i,j);
end
end
% Now, increase the size of the notNine vector until
% it contains 10 values by adding additional 9’s
while lengths(notNine) <= 10
notNine(end+1) = 9;
end
runtime error or syntax error (I’m not sure) = used wrong variable in
the for statement, should be i = size(mat,1) and j = 1:size(mat,2)
syntax error = misspelled the word size in the second for statement
runtime error = notNine hasn’t been clarified as a variable
syntax error = missing an ‘end’
syntax error = should be length(notNine) not lengths(notNine)
4) Trigonometry and Inputs
% Ask user to choose degrees or radians
userChoice = input('Type "d" for degree or "r" for radian: \n');
% Calculate sine of the angle based using either radians or
% degrees as specified by the user
if userChoice = 'd'
val = input('Input your degree: \n');
userAns = sind(val);
fprintf('The answer is %.2s\n', userAns);
elseif userchoice = 'r'
val = input('Input your radian: \n');
userAns = sine(val);
fprintf('The answer is %.2s\n', userAns);
elseif (userChoice ~ 'd' || userChoice ~ 'r')
fprintf('Please enter the correct input as required.\n');
end
syntax error = should be userChoice == ‘d’ not userChoice = ‘d’
runtime error = userchoice is used instead of userChoice
runtime error = sine(val) should be sin(val)
logic error = %.2s prints a string so it should be %.2f so a
number will be printed instead
logic error = elseif at the end should just be else
5) Dividing A Value
list = [5 0 4 6 8 10 11 43 23 0 9 81];
quotient = 10000;
i = 1;
% Repeatedly divide 10000 by each consecutive value in list
% and stop if quotient becomes less than 1.
% Print the final value of quotient and how many divisions were
% performed.
while quotient >= 1 && i <= length(list)
quotient = quotient / list{i};
end
if quotient < 1
fprintf(“Quotient equals %f and is less than 1.\n”, quotent)
elseif
fprintf(“Quotient greater than or equal to 1 and”, ...
“equals %f.\n”, quotient)
Runtime error = list{i} should use regular parenthesis and be
list(i)
Logic error = i = 1 creates an infinite loop so it should be i =
i + 1
Runtime error = there are zeros in the list which means quotient
might be divided by zero which wouldn’t be defined
Runtime error = misspelled quotient in the if statement
Syntax error = there is no condition after the elseif so it
should just be else
Syntax error = there is no ‘end’