In: Computer Science
Correct all errors in the provided script. Remember that some errors are fatal (result in an error message) while others simply cause the program to produce incorrect results.
You can assume that you have fixed all errors when the checker passes all tests.
%***************************************************************************************
%It is recommended that you debug this program offline and submit only once you have corrected the errors
%***************************************************************************************
%%
%These 3 loops all calculate the sum of the integers from 1 through 1000
for number = 1:1000
total = total + number;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n', total);
k=0;
theTotal = 0;
while k <1000
theTotal = theTotal + k;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n', theTotal);
theTotal_2 = 0;
while k >1000
theTotal_2 = theTotal_2 + k;
k = k-1;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n', theTotal);
%%
%These structures all do the same thing - find the roots of a quadratic,
%and print out the real roots, if any
a = 1;
b = 3;
c = 2;
discriminant = b^2 - 4ac;
if discriminant > 0
root1 = (-b+sqrt(b^2 - 4*a*c))/(2*a);
root2 = (-b-sqrt(b^2 - 4*a*c))/(2*a);
fprintf('The roots are %f and %f\n', root1, root2);
elseif discriminant == 0
root1 = -b/(2*a);
root2 = NaN;
fprintf('The root is %f and %f\n', root1);
else
fprintf('There are no real roots\n');
end
aa = 1;
bb = -8;
cc = 16;
discriminant = bb^2 - 4*aa*cc;
switch discriminant
case discriminant>0
root3 = (-bb+sqrt(bb^2 - 4*aa*cc))/(2*aa);
root4 = (-bb-sqrt(bb^2 - 4*aa*cc))/(2*aa);
fprintf('The roots are %f and %f\n', root3, root4);
case discriminant == 0
root3 = -b/(2*a);
root4 = NaN;
fprintf('The root is %f\n', root3);
otherwise
fprintf('There are no real roots\n');
end
%%
%This program calculates and plots the Fibonacci numbers that are less than
%100
fibonacciNumber=[1, 1];
%first time through the loop, calculating the 3rd Fibonacci number
index = 3;
while fibonacciNumber < 100
fibonacciNumber(end+1) = fibonacciNumber(end) + fibonacciNumber(end-1);
index = index + 1;
end
try
assert(sum(fibonacciNumber>=100)==0)
%The following lines will only be executed if the assertion passes
fprintf('There are %i Fibonacci numbers that are less than 100;', length(fibonacciNumber));
fprintf('They are: \n')
fprintf('%i\n', fibonacciNumber);
plot(1:index, fibonacciNumber)
catch ME
switch ME.identifier
case 'MATLAB:assertion:failed'
disp('Incorrect set of Fibonnaci numbers.')
case 'MATLAB:samelen'
disp('error in plot - vectors not the same length')
otherwise
disp('Something is wrong with the Fibonacci number code')
end
end
%%
%This program calls the user-defined function, GPA() to calculate a
%student's grade point average
Grades = 'ABACAABBDAC';
Credits = [43454453324];
Name = "Joe";
GPAValue = gradePointAverage(grades, credits);
fprintf('%s''s GPA is %d\n', GPAValue);
function [ gradePointAverage] = GPA(letterGrades, numCredits )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
qualityPoints = (letterGrades=='A')*4 + (letterGrades=='B')*3 + (letterGrades=='C')*2 + (letterGrades=='D')*1;
totalPoints = sum(qualityPoints.*credits);
gradePointAverage = totalPoints/sum(credits);
end
Question 1
%%
%These 3 loops all calculate the sum of the integers from 1 through
1000
total=0;
for number = 1:1000
total = total + number;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n',
total);
k=0;
theTotal = 0;
while k <=1000
theTotal = theTotal + k;
k+=1;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n',
theTotal);
theTotal_2 = 0;
k=k-1;
while k >0
theTotal_2 = theTotal_2 + k;
k = k-1;
end
fprintf('The sum of the numbers from 1 through 1000 is %i\n',
theTotal_2);
%%
Output
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
--------------------------------------------------------------------------------------
Question 2
%%
%These structures all do the same thing - find the roots of a
quadratic,
%and print out the real roots, if any
a = 1;
b = 3;
c = 2;
discriminant = b^2 - 4*a*c;
if discriminant > 0
root1 = (-b+sqrt(b^2 - 4*a*c))/(2*a);
root2 = (-b-sqrt(b^2 - 4*a*c))/(2*a);
fprintf('The roots are %f and %f\n', root1,
root2);
elseif discriminant == 0
root1 = -b/(2*a);
root2 = NaN;
fprintf('The root is %f and %f\n', root1);
else
fprintf('There are no real roots\n');
end
aa = 1;
bb = -8;
cc = 16;
discriminant = bb^2 - 4*aa*cc;
switch true
case discriminant>0
root3 = (-bb+sqrt(bb^2 -
4*aa*cc))/(2*aa);
root4 = (-bb-sqrt(bb^2 -
4*aa*cc))/(2*aa);
fprintf('The roots are
%f and %f\n', root3, root4);
case discriminant == 0
root3 =
-bb/(2*aa);
root4 = NaN;
fprintf('The root
is %f\n', root3);
otherwise
fprintf('There are
no real roots\n');
end
%%
Output
The roots are -1.000000 and -2.000000
The root is 4.000000
------------------------------------------------------------------------------------
Question 3
%%
%This program calculates and plots the Fibonacci numbers that are
less than
%100
fibonacciNumber=[1, 1];
%first time through the loop, calculating the 3rd Fibonacci
number
index = 2;
while true
val = fibonacciNumber(end) +
fibonacciNumber(end-1);
if(val<100)
fibonacciNumber(end+1)
=val;
else
break;
end
index = index + 1;
end
try
assert(fibonacciNumber(end)<100)
%The following lines will only be executed if
the assertion passes
fprintf('There are %i Fibonacci numbers that are
less than 100;', length(fibonacciNumber));
fprintf('They are: \n')
fprintf('%i\n', fibonacciNumber);
plot(1:index, fibonacciNumber)
catch ME
switch ME.identifier
case
'MATLAB:assertion:failed'
disp('Incorrect set of Fibonnaci numbers.')
case 'MATLAB:samelen'
disp('error in plot - vectors not the same length')
otherwise
disp('Something is wrong with the Fibonacci number code')
end
end
%%
Output
There are 11 Fibonacci numbers that are less than 100;They are: 1 1 2 3 5 8 13 21 34 55 89
-------------------------------------------------------------------------------------------------------------------
Question 4
%%
%This program calls the user-defined function, GPA() to calculate
a
%student's grade point average
Grades = 'ABACAABBDAC';
Credits = [43454453324];
function [ gradePointAverage] = GPA(letterGrades, numCredits
)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
qualityPoints = (letterGrades=='A')*4 + (letterGrades=='B')*3 +
(letterGrades=='C')*2 + (letterGrades=='D')*1;
totalPoints=0;
for i=1:size(qualityPoints)
totalPoints=totalPoints+(qualityPoints(i)*numCredits(i));
end
gradePointAverage = totalPoints/sum(numCredits);
end
Name = "Joe";
GPAValue = GPA(Grades, Credits);
fprintf('%s''s GPA is %d\n',Name, GPAValue);
%%
Output
Joe's GPA is 4