hi, i am in a desperate plea for help with my matlab asignment.. a solution woul
ID: 3631742 • Letter: H
Question
hi, i am in a desperate plea for help with my matlab asignment.. a solution would be greatly appreciated.
I need to use one of my previous assignments to create a m-file which uses functions and subfunctions instead of operators to end up with the same result as before. This assignment has exactly the same specifications as my old assignment, with the exception that my code must now be written using one primary function (rescue_main) and multiple subfunctions in one .m file.
objective:
My main solution file to this assignment(named rescue_main.m) should contain a primary function called rescue_main which will then call the following 11 subfunctions named exactly as specified with the following functionality:
1. get_code: A subfunction that prompts the user to enter a code, gets the code, and returns it.
2. has_five_digits: A subfunction that determines if the code has exactly 5 digits.
3. split_digits: A subfunction that that extracts the 5 separate digits into an array
4. sum_is_even: A subfunction that determines if the sum of the digits is even.
5. get_rescue_day: A subfunction that calculates the rescue day number.
6. day_is_valid: A subfunction that determines if the rescue day number is valid.
7. get_rendezvous_pt: A subfunction that calculates the rendezvous point number.
8. point_is_valid: A subfunction that determines if the rendezvous point number is valid.
9. get_rescue_msg: A subfunction that prints out a rescue message using the rescue day and rendezvous point numbers. It calls the next two subfunctions.
10. get_day_name: A subfunction that determines a rescue day string based on a corresponding rescue day number.
11. get_point_name: A subfunction that determines a rendezvous point string based on a corresponding rendezvous point number.
- the primary function should be called rescue_main and should only contain:
o variable declarations
o assignment statements
o function calls
o an if-else chain
o output statements
- Aside from the logical not operator (~) and the assignment (=) statement, the primary function rescue_main must not contain:
o arithmetic operators ( + - * /)
o relational operators (< <= > >= == )
o logical operators (&& ||)
o switch statements
• All of the functionality of these operators and statements must be moved out to the subfunctions.
I have provided my assignment 3 (rescue.m) and my assignment which i started below(rescue_main.m) as well as my expected output.
Any help would be greatly appreciated. This is due soon so im sweating a little.
THIS IS MY ASSIGNMENT IN WHICH I NEED TO BUILD ON WITH FUNCTIONS AND SUB-FUNCTIONS AND BY REMOVING THE OPERATORS...
THIS IS THE START TO MY NEXT ASSIGNMENT...
THANK YOU AGAIN
Explanation / Answer
Dear,
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);
rendezvouspt
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.