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

A parking lot charges $5/hour for each of the first 6 hours and $4/hour for each

ID: 3563891 • Letter: A

Question

A parking lot charges $5/hour for each of the first 6 hours and $4/hour for each additional hour Write a function, named ParkingCost. which takes as an argument the number of hours a car has been parked and computes the overall parking cost. For example. ParkingCost(9) should compute 42 (since the first 6 hours arc charged at $5/hour and the remaining 3 hours are charged at $4/hour). A magic square is a two dimensional matrix such that the sum of the elements along each row, column, or diagonal is the same. Write a function, named IsMagicSquare, which takes an argument a 3x3 matrix and determines whether or not it is a magic square. Write a function, named Three, which takes an argument a 3x3 matrix of integers and returns 1 iff the matrix has a row. a column, or a diagonal that has the same integer value in its three slots. For example. Three!11 0 2: 5 5 5: 1 2 4)) should return 1 since each entry in the second row is 5. As another example. Three

Explanation / Answer

3.

function [cost]= ParkingCost(x)
% x represent no. of hours

cost=0;
if x>0 && x<=6
  
cost=5*x;
elseif x>0 && x>6
cost=5*6 + 4*(x-6);
else
error('no. of hours can not be negative');
end

end

4.

function [magic]= IsMagicSquare(A)
% A represent 3x3 matrix
magic='MagicSquare';
for i=1:8
  
if i<4
Msum(i)=sum(A(i,:));
elseif i>3 && i<7
Msum(i)=sum(A(:,i-3));
elseif i==7
Msum(i)=sum(diag(A))
else
Msum(i)=A(3,1) +A(2,2) +A(1,3)
end
end
check =Msum - Msum(1)
if sum(check)==0
display(magic)
else
magic='Not a MagicSquare';
display(magic)
end

end

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