please solve the problem by using Matlab Procedure array-contains-1-to-n Input:
ID: 3777144 • Letter: P
Question
please solve the problem by using Matlab
Procedure array-contains-1-to-n Input: A (1-dimensional array), n (an integer) output: true if A contains the values 1 to n exactly once each, false otherwise 1. If the length of A is not n, then return false 2. Create an array C of zeros of length n. Each element of the array will be the count of the number of times a value equal to the index of the element is seen. That is, Cv the number of time v occurs in the array A. 3. Traverse the array (visit each element exactly once) and do the following for each element of the array: a. If the element is less than 1, retum false because A is only supposed to contain the values from 1 to n b. If the element is greater than n, return false because A is only supposed to contain the values from 1 ton c. Let v the value of the element d. Increment the value of C by one. e. If C 1, then return false because v has now been seen more than once 4. Return true because the only way to reach this step is to have an array of length m (by line l) that contains only numbers between 1 and n (by lines 3.a and 3.b) and does not contain any duplicates (by line 3.e) Now it's your turn. Study the algorithm above and use what you learn it to patch any holes that may exist in your algorithm. Then, implement your algorithm for verifying that a given 1- dimensional array contains all and only the values from 1 to n. To test your algorithm, you can generate valid arrays by using the randperm function: help randperm rand perm Random permutation P rand perm (N) returns a vector containing a random permutation of the integers 1:N. For example randperm (6) might be 12 4 5 6 1 3] Now it's my turn again.Explanation / Answer
function words = int2words(number)
nameone = {'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'};
nameten = {'', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};
%% convert to words
hundreds = mod(number, 100);
if hundreds < 20
words = nameone{hundreds+1};
number = (number - hundreds) / 100;
else
tens = mod(number, 10);
words = nameone{tens+1};
if ~isempty(words)
words = ['-' words];
end
number = (number - tens) / 10;
tens = mod(number, 10);
words = [nameten{tens} words];
number = (number - tens) / 10;
end
if number > 0
if ~isempty(words)
words = [' ', words];
end
words = [nameone{number+1}, ' hundred', words];
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.