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

Please explain each line of this code: This is the answer to the code, please ex

ID: 3833238 • Letter: P

Question

Please explain each line of this code:



This is the answer to the code, please explain each line of this code :

% the function that takes a word and
% a letter as input and returns the position
% of that letter in that word
function [x] = find_letter_positions(word,letter,case_sensitive)
x=[];
i=1;
j=1;
opt=false;
if nargin == 3
if(case_sensitive==true)
opt = true;
end
end

for l = word
if(opt==true)
if(l==letter)
x(j)=i;
j=j+1;
end
else
if(lower(l)==lower(letter))
x(j)=i;
j=j+1;
end
end
i=i+1;
end % end of loop
end % end of function
main
% the function that takes a word and
% a letter as input and returns the position
% of that letter in that word
find_letter_positions('bEtween','e',true)
find_letter_positions('bEtween','e',false)

Write code that tracks a city's snow removal budget during a simulated winter season of 120 days The code must implement the following requirements. a) The amount of snow is stored for each of N roads. You may assume that N exists and contains a valid value. For example, if N eguals 3, the data you must maintain for the amount of snow on the roads would look something like: [0, 10, 4 l. This would mean that currently there is n (0) snow on road 1. 10 inches of snow on road 2, and 4 inches of snow on road 3. b) For each day you simulate, use the function how much new snow0, which will return the number of inches of snow that fell that day. It returns 0 if no snow fell c) The amount of snow that falls should be added to the amount of snow already on each road. (For example, if 6 new inches of snow fell, then the road data listed in a) above would now be: 6, 16, 10 d) on days that it snows (and only on days that it snows), plows will be sent out. If the current day is an odd day, then only the odd roads (roads with odd indexes) will removed the even roads will be plowed. All snow on a plowed road is (unless you run out of budget the budget section below) e) If it does not snow, then one inch of snow will melt off every road. Remember, there can be a minimum of 0 inches on any road

Explanation / Answer

Answer:

function [x] = find_letter_positions(word,letter,case_sensitive)
x=[];
i=1; % initialization of i value to 1
j=1; % initialization of j value to 1
opt=false; % if case_sensitive is false then opt is set to false
if nargin == 3 % if nargin is equal to 3
if(case_sensitive==true) if case_sensitive is equal to true
opt = true; % if case_sensitive is true and nargin is equal to 3 then opt is set to true
end
end

for l = word % for a letter l in word
if(opt==true) % if option is true
if(l==letter) % if l is letter
x(j)=i; % then i is stored in x(j)
j=j+1; % j is incremented by one to iterate through the for loop
end
else % if case_sensitive is false then
if(lower(l)==lower(letter)) % then convert the letter and the current letter in the word to be checked into lower case % and then check
x(j)=i; % i is stored in x(j)
j=j+1; % j is incremented by one to iterate through out the loop
end
end
i=i+1; and then i is incremented by 1
end % end of loop
end % end of function
main
% the function that takes a word and
% a letter as input and returns the position
% of that letter in that word
find_letter_positions('bEtween','e',true) % invoking the function by giving required arguments in the function
find_letter_positions('bEtween','e',false) % invoking the function by giving required arguments in the function