In matlab Write a function that validates whether its inputs meet the following
ID: 3807947 • Letter: I
Question
In matlab Write a function that validates whether its inputs meet the following criteria:
a. It must take exactly three parameters as input.
b. The first parameter must be a string that starts with a capital letter.
c. The second parameter must be a structure, and the structure must contain a field named milage.
d. The third parameter must be a non-empty array where all numbers in the array must alternate even then odd and each value must be larger than the previous value. Eg: [ 2 9 14 23 66 101 102 ]
Explanation / Answer
function isValid = validate_input(par1, par2, par3)
if (upper(par1(1)) == par1(1)) & (isstruct(par2)) & (isfiled(par2, 'milage'))
firsttwo = 1
end
condition3 = 1 - isempty(par3)
isValid = firsttwo & condition3
prev = 0
current = 0
for idx = numel(par3)
current = par3(idx)
if (idx % 2) == 0
if current % 2 == 0
isValid = isValid & 1
else
isValid = isValid & 0
end
else
if current % 2 != 0
isValid = isValid & 1
else
isValid = isValid & 0
end
end
isValid = isValid & (current > prev)
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.