Question

In: Computer Science

Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input...

Programming assignment 9
Write a function sortWords(array) that does the following:
1. Takes as an input a cell array of strings consisting of letters only. (1 pts)
2. Returns a cell array with all the strings in alphabetical order. (5 pts)
3. The function should ignore whether letters are lower case or upper case. (2 pts)
Test your function with the following: (2 pts)
>> sorted=sortWords({’Hello’,’hell’,’abc’,’aa’,’aza’,’aab’,’AaBb’,’a’}) sorted =
’a’ ’aa’ ’aab’ ’AaBb’ ’abc’ ’aza’ ’hell’ ’Hello’
Note: Your function may not contain any built-in MATLAB function(s) that deal with sorting or finding minimum values. If your function does use these built-in functions, up to 8 points may be deducted from your total.
Hint: Use the program sharkSort(vec) as a starting point and amend it so that it works for this programming assignment. For example your function could start as follows (fill in the blanks as necessary):
function sortedarray=sortWords(array) sortedarray={}; while ~isempty(array) [m,pos]=findSmallest(array); array(pos)=[]; sortedarray=[sortedarray m]; end
function [m,pos]=findSmallest(array) m = ...; pos=1; for i=2:length(array) if isLessWord(...,m) m = ...; pos= i; end end
function less=isLessWord(wordA,wordB) worda=lower(wordA); wordb=lower(wordB);

Solutions

Expert Solution


function sortedarray = sortWords(array)
sortedarray = {};
while ~isempty(array)
[m,pos] = findSmallest(array);
array(pos) = [];
sortedarray = [sortedarray m];
end
end

function [m,pos] = findSmallest(array)
m = array{1}; % set m to string at position 1 as the smallest string
pos = 1; % index of the smallest string position
% loop over the array and find the smallest string in the array
for i=2:length(array)
if isLessWord(array{i},m)
m = array{i}; % if array at ith position < m , then set m = string at ith position
pos = i;
end
end
end

function less = isLessWord(wordA,wordB)
% function to return 1 if wordA < wordB else 0
worda = lower(wordA);
wordb = lower(wordB);
less = -1;

if(length(worda) == length(wordb))
word_length = length(worda);
elseif(length(worda) < length(wordb))
word_length = length(worda);
else
word_length = length(wordb);
end
  
for i=1:word_length
if(worda(i) < wordb(i))
less = 1;
break;
elseif(worda(i) > wordb(i))
less = 0;
break;
end
end
  
if(less == -1)
if(length(worda) > word_length)
less = 0;
else
less = 1;
end
end
end

% end of script

Output:


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input...
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive integers. 2. Of the two integers a and b, the function returns the integer that has the most factors fact. 3. If both integers a and b have the same amount of factors fact, the function will return the larger integer. Test your function with the following: >> result=moreFactors(24,32,3) result = 24 (24 = 3^1 · 2^3 , 32 = 2^5 )...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT