Part I: Creating and Searching a Vertical String Array (10pts) Introduction Ofte
ID: 3771797 • Letter: P
Question
Part I: Creating and Searching a Vertical String Array (10pts)
Introduction
Often we want to gather some data, store it, and then later search for it. This data may be numeric or non-numeric. We’ve already looked at searching for numerical data in a vector. If our data is string data we can store it in a matrix using strvcat and then check if the desired string is any of the rows if this matrix.
Todo
First ask the user for how many strings they plan to enter. Keep asking them until they enter a positive number (floor it to make it an integer). Let’s call this number n
Then ask the user to enter n strings, each time adding it as a new row to the matrix storing the data. Use the Matlab function strvcat to do this.
After the user has entered n strings ask them for a string to look for in the string matrix. If you find the string in any of the rows return that row number. Otherwise return an error message.
Hints
Let us assume you are storing the data in a variable called data. For the examples that follow your data variable will grow as follows:
data=[]
data=[‘Look for me’];
data=[‘Look for me’; ‘CS105 ’ ];
data=[‘Look for me’;’CS105 ’; ‘Test today?’ ];
Remember how to compare strings?
When doing strvcat extra space is added as needed (see Note above). So when you compare you should probably first strip any extra space. You do this with the strtrim function
You have to check each row in data. Do you remember how to get the number of rows from a matrix?
Remember how to get all the data for a single row of a matrix?
Example 1 (user input in bold)
How many strings will you enter? Test
That is not a number.
How many strings will you enter? -10
You have to enter a positive number.
How many strings will you enter? 3
Enter string #1: Look for me
Enter string #2: CS105
Enter string #3: Test today?
What string would you like to search for? Test Today?
Test Today? was found in row 3
Example 2
How many strings will you enter? 3
Enter string #1: Look for me
Enter string #2: CS105
Enter string #3: Test today?
What string would you like to search for? me
me was not found
Part II: Processing a CSV line (10pts)
Introduction
String processing and manipulation is an important part of scientific computing. Many data sources store their information in plaintext which can be read in as strings. The data is separated by delimiters. For example a CSV (comma separated values) file may have entries like:
Name, Age, ID
where Age and ID are written into the file as text versions of numbers and the data is separated by commas.
Todo
Write a program that gets a CSV line from a user (via the command line). Compute the sum of the numbers and display that sum to the command line.
Hints
Find all of the commas and for each of them get a substring using that comma’s location
You need a start and stop location to get substrings
Remember how to get sub-matrices/sub-vectors?
You may need to treat the first value (before the first comma) and the last one (after the last comma) “specially”.
If the substring is a number, add it to your sum
Examples
Input: 4, 5, 6, 0
Output: 15
Input: 4, dkf, 8, fff.0
Output: 12
Input: a b c 5
Output: 0
Submission
Submit a single zip file consisting of
A PDF that contains:
A description of what you did for Part 1 and 2, including example tests and results
Your scripts for Part 1 and 2
As always in your report add any additional things you tried, discoveries you made, and/or issues you had. Also make sure to include your pledge.
Explanation / Answer
Complete Program:
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.