Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment Instructions The purpose of this assignment is to show that you can... « Compare two character arrays. « Analyze provided function(s). « Implement an insertion sort using student and...

1 answer below »
Assignment Instructions
The purpose of this assignment is to show that you can...
« Compare two character a
ays.
« Analyze provided function(s).
« Implement an insertion sort using student and provided functions.
We have used the insertion sort algorithm to sort an unsorted vector of numbers. This homework has you apply the
same algorithm on words in a sentence. For example...Sort...
"the quick
own fox"
aS...
"
own fox quick the"
For this homework assignment you will...
« Create a function "wordBeforeWord" which takes two arguments: a character a
ay "word1", and another character a
ay "word2

in that order. You can assume that "word1" and
"word2" are lowercase and contain no spaces. "word1" or "word2" may be empty. Return a logical true if "word1" is alphabetically before "word2". Otherwise return false.
= For example...
wordBeforeWord ('goodbye', 'hello')
ans =
logical
1
wordBeforeWord('hello', 'goodbye')
ans —
logical
0
wordBeforeWord ('goodbye', 'goodbye')
ans —
logical
0
wordBeforeWord('good' , ‘goodbye ')
ans —
logical
1
wordBeforeWord ('goodbye', 'good')
ans —
logical
0
= Here's some other test cases
wordBeforeWord('a’,
wordBeforeWord ('b',
wordBeforeWord('a’,
wordBeforeWord ('aa’,
wordBeforeWord ('ab',
wordBeforeWord ('aa’,
wordBeforeWord ('aaa’
wordBeforeWord ('aba’
wordBeforeWord ('aaa’
wordBeforeWord ('aab'
wordBeforeWord ('a
'
wordBeforeWord ('aab'
wordBeforeWord ('aac'
wordBeforeWord ('abc'
wordBeforeWord ('aac'
wordBeforeWord ('aaa’
wordBeforeWord ('aba’
wordBeforeWord ('aaa’
wordBeforeWord ('aab'
wordBeforeWord ('a
'
wordBeforeWord ('aab'
wordBeforeWord('a’,
wordBeforeWord ('ab',
wordBeforeWord('a’,
wordBeforeWord ('ab',
'b') % return 1
'a') % return 0
'a') % return 0
'ab') % return 1
'aa') % return 0
'aa') % return 0
, taba’)
, 'aaa’)
, 'aaa’)
’ ra
')
, 'aab')
, 'aab')
, rabo')
, rac’)
, rac’)
’ ra
')
, 'aab')
, 'aab')
, taba’)
, 'aaa’)
, 'aaa’)
w
®
®
®
®
®
®
®
®
®
®
®
®
®
®
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
eturn
'ab') % return 1
'a') % return 0
'ab') % return 1
'a') % return 0
wordBeforeWord('', 'a') % return 1
wordBeforeWord('a', '') % return 0
wordBeforeWord('', '') % return 0
wordBeforeWord('', 'ab') % return 1
wordBeforeWord ('ab',

1") % return 0
wordBeforeWord('', '') % return 0
CcomHOHOOMOOHKOOHR
= DO THIS ONLY USING methods and practices demonstrated in class. These include...
= Sequences or Vectors.
= The functions "strcmp", "length"'strfind", etc...
for", "whil
= Arithmetic, relational and logical operations.
= DO NOT USE other built-in matlab functions.
= Such as.."regexp", "return", "string" or other similar functions.
= If unsure, ask the instructor.
= HINT: Suggest checking one character at a time iteratively.
= HINT: "a" <'b'is true.

"
eak", "continue" statements
« Use the provided function (see attached) called "insertWordInOrderToSentence" which takes two arguments: word character a
ay, "newWord", and an already word sorted sentence
character a
ay "ss"...in that order. Assume "ss" has no beginning or trailing spaces and each word within it is single space separated. "ss" should already be an alphabetically word sorted
sentence. "ss" may be empty. "newWord" is the additional word that will be inserted into "ss" in a sorted manner. Assume "newWord" is all lower case and has no spaces. "newWord"
may be empty. Return the sentence "ss" with "newWord" inserted into it at the proper location. Use your function "wordBeforeWord()" to accomplish this.
= For example...
insertWordInOrderToSentence ('banana', 'avocado grape pear')
ans —
'avocado banana grape pear’
insertWordInOrderToSentence ('watermelon', 'avocado grape pear’)
ans —
avocado grape pear watermelon’
insertWordInOrderToSentence ('apple', 'avocado grape pear')
ans —
'apple avocado grape pear’
insertWordInOrderToSentence ('new', '')
ans —
"new!
DO THIS ONLY USING methods and practices demonstrated in class. These include...
= Sequences or Vectors.
= The functions "strcmp", "length"'strfind", etc...
"for", "while", "
eak", "continue" statements
= Arithmetic, relational and logical operations.
DO NOT USE other built-in matlab functions.
= Such as.."split", "st
ep”,'replaceBetween","'regexprep", "return", "string" or other similar functions.

= If unsure, ask the instructor.
HINT: Handle the empty cases separately from the non-empty cases.
HINT: See examples from back in Lecture Slide 4 about finding first or last word in sentence...apply new knowledge of loops.
HINT: While in the Lecture Slides there is an example of insertlnOrder() for numbers, it might NOT be productive to use that as a template for your code here. Instead, search the
sentence for the first word which is not alphabetically before "newWord". Insert "newWord" within "ss" there. If all words in ss are alphabetically before "newWord", insert
"newWord" at the end of "ss".

« Create a function called "sentencelnsertionSort" which takes an argument: an unsorted sentence character a
ay that order. Assume "s" has no beginning or trailing spaces and
each word within it is single space separated. "s" may be empty. Using an insertion sort algorithm, return the character a
ay sentence with the words in "s" alphabetically sorted. Use
your function "insertWordInOrderToSentence()" to accomplish this.
= For example...

sentenceInsertionSort('the quick
own fox')
ans —
own fox quick the’
sentenceInsertionSort ('the')
ans =
the!
sentenceInsertionSort('')
ans —
0x0 empty char a
ay
= DO THIS ONLY USING methods and practices demonstrated in class. These include...
= Sequences or Vectors.
= The functions "strcmp", "length"'strfind", etc...
= "if", "for", "while", "
eak", "continue" statements
= Arithmetic, relational and logical operations.
= DO NOT USE other built-in matlab functions.
= Such as.."st
ep"'replaceBetween","regexprep"”, "return", "string" or other similar functions.
» If unsure, ask the instructor.
= HINT: See insertSortSimple() in Lecture Slide 14.
1. Start Matlab, you should see a bunch of windows all touching each other, with the biggest one labeled “Command Window”, as was demonstrated in lecture. Click in Matlab's Command
Window. Hit the return key and you should see a prompt printed by the computer:
After the prompt, type
diary homework5.txt
This will make everything that will be typed in the command window (by either you or Matlab) also be copied into the file homeworkS5.txt. This file will be on the computer where
Matlab is really running,
2. In the command window, type
clea
This will remove any previously assigned variables from the cu
ent workspace
3. In the command window, type
help wordBeforeWord
You should see the help for your custom function.
4. In the command window, type
help insertWordInOrderToSentence
You should see the help for your custom function.
5. In the command window, type
help sentenceInsertionSort
You should see the help for your custom function.
6. In the command window, type
wordBeforeWord ('down', 'up')
You should see either true or false.
7. In the command window, type
wordBeforeWord (' programming’ , 'program')
You should see either true or false.
8. In the command window, type
wordBeforeWord ('rain', 'rainfall')
You should see either true or false.
Answered 2 days After Nov 22, 2022

Solution

Baljit answered on Nov 25 2022
52 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here