this is an update to my original question post \'Matlab assignment help\' and de
ID: 3631788 • Letter: T
Question
this is an update to my original question post 'Matlab assignment help' and dealt with the "rescue.m" file in which I must change to incorporate functions and sub-functions as described previously 'see last question post'. I have split my question up since this is quite long and felt i needed to offer extra karma pts. thanks
this was hard to format so there are some lines that look kind of out of place, however this code is working 100% and is needed to create the rescue_main.m file also explained in my previous question.
Explanation / Answer
function rescue_main() clc; code_str=get_code(); if ~has_five_digits(code_str) disp(['Decoy message: Not a five-digit number.']); else digits = split_digits(code_str); iseven = sum_is_even(digits); if ~iseven disp(['Decoy message: Sum is odd.']); else rescueday=get_rescue_day(digits); isdayvalid=day_is_valid(rescueday); if ~isdayvalid disp(['Decoy message: Invalid rescue day.']); else rendezvouspt = get_rendezvous_pt(digits); point_validity = point_is_valid(rendezvouspt) if ~point_validity disp(['Decoy message: Invalid rendezvous point.']) else get_rescue_msg end end end end end function code=get_code() code=input('Please enter a code to break:','s'); end function is5digit = has_five_digits(code_str) is5digit = false; if length(code_str)==5 is5digit = true; end end function digit=split_digits(code_str) digit=code_str-'0'; end function iseven = sum_is_even(digits) sum1 = sum(digits); iseven = false; if mod(sum1,2)==0 iseven = true; end end function rescue=get_rescue_day(digits) rescue=digits(1)*digits(2)-digits(3); end function isvalid=day_is_valid(rescueday) isvalid = true; if rescueday < 1 || rescueday > 7 isvalid=false; end end function rendezvousPt = get_rendezvous_pt(digits) if digits(4)==digits(5) rendezvousPt=0; elseif digits(4) > digits(5) rendezvousPt=digits(5)+1; else rendezvousPt=digits(5); end end function point_validity = point_is_valid(rendezvousPt) if rendezvousPt == 2 || rendezvousPt == 7 || rendezvousPt == 8 point_validity = true; else point_validity = false; end end function nil = get_rescue_msg() disp(['Rescue on ' get_day_name(rescueday) ' at the ' get_point_name(rendezvouspt) '.']); end function day = get_day_name(rescueDay) if rescueDay == 1 day = 'Monday'; elseif rescueDay == 2 day = 'Tuesday'; elseif rescueDay == 3 day = 'Wednesday'; elseif rescueDay == 4 day = 'Thursday'; elseif rescueDay == 5 day = 'Friday'; elseif rescueDay == 6 day = 'Saturday'; elseif rescueDay == 7 day = 'Sunday'; else disp('Error in Rescue day!!!'); end end function point = get_point_name(rendezvousPt) if rendezvousPt == 2 point = 'fountain'; elseif rendezvousPt == 7 point = 'large tree'; elseif rendezvousPt == 8 point = 'bridge'; else disp('Error in rendezvous point!!!'); end end %%NOTE!! this function will give an error as the line 'rescueday < 1 || rescueday > 7' always gives an error. i merely copied from ur initial code so u might want to take a look at it. other than that the code is good
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.