Question

In: Computer Science

Correct all errors in the provided script. Remember that some errors are fatal (result in an...

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

Solutions

Expert Solution

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


Related Solutions

1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
Prepare necessary adjusting entries to correct the errors on all issues (ii) to (v) in the...
Prepare necessary adjusting entries to correct the errors on all issues (ii) to (v) in the financial statements of GHL for the year ended 31 March 2020 in accordance with relevant HKFRSs.   (ii) At 1 April 2019 there was a deferred tax liability of $6.6 million in the statement of financial position and no adjustments have been made to this figure at 31 March 2020. This deferred tax liability was solely in relation to the differences between the carrying amount...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
Correct mistakes in the following program: /* BUG ZONE!!! Example: some common pointer errors */ #include...
Correct mistakes in the following program: /* BUG ZONE!!! Example: some common pointer errors */ #include <stdio.h> main() { int i = 57; float ztran4; int track[] = {1, 2, 3, 4, 5, 6}, stick[2][2]; int *nsave; /* Let's try using *nsave as an int variable, and set it to 38 */ *nsave = 38; /* BUG */ nsave = NULL; *nsave = 38; /* BUG */ nsave = 38; /* BUG */ &nsave = 38; /* BUG */ nsave...
**Review the following passages for sentence fragments, comma splices, and run on sentences. **Correct all errors...
**Review the following passages for sentence fragments, comma splices, and run on sentences. **Correct all errors where necessary, and bold each correction. • “It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the...
Review the following passages for sentence fragments, comma splices, and run on sentences. Correct all errors...
Review the following passages for sentence fragments, comma splices, and run on sentences. Correct all errors where necessary, and bold each correction. • “It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the...
Entries to Correct Errors The following errors took place in journalizing and posting transactions: Cash of...
Entries to Correct Errors The following errors took place in journalizing and posting transactions: Cash of $3,460 received on account was recorded as a debit to Fees Earned and a credit to Cash. A $1,680 purchase of supplies for cash was recorded as a debit to Supplies Expense and a credit to Accounts Payable. Note: Prepare the entry to reverse the original entry first. Journalize the entries to correct the errors. If an amount box does not require an entry,...
Entries to Correct Errors The following errors took place in journalizing and posting transactions: Insurance of...
Entries to Correct Errors The following errors took place in journalizing and posting transactions: Insurance of $18,000 paid for the current year was recorded as a debit to Insurance Expense and a credit to Prepaid Insurance. A withdrawal of $10,000 by Brian Phillips, owner of the business, was recorded as a debit to Wages Expense and a credit to Cash. Journalize the entries to correct the errors. For part a, first reverse the original entry and then make the correct...
The debit and credit totals are not equal as a result of the following errors: A....
The debit and credit totals are not equal as a result of the following errors: A. The cash entered on the trial balance was understated by $5,500. B. A cash receipt of $5,400 was posted as a debit to Cash of $6,300. C. A debit of $10,400 to Accounts Receivable was not posted. D. A return of $147 of defective supplies was erroneously posted as a $1,470 credit to Supplies. E. An insurance policy acquired at a cost of $1,250...
In this python script find any bugs and errors which would cause the code to crash....
In this python script find any bugs and errors which would cause the code to crash. The code must be re-written correctly. After debugging make a separate list of all the errors you found in the script. contacts_list=[] # global variable, list of contacts_list, one string per contact def pause()     """ pauses program e.g. to view data or message """     input("press enter to continue") def load():     """ populate list with data """          contacts_list.append(('Milo ', '063847489373'))...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT