Вы находитесь на странице: 1из 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.

html

3/6/11 3:30 PM

CS1371 Fall 2010 - Test 2

WHITTINGSLOW, DANIEL CORBETT dwhittingslow3


1. (QID 1138) 16/20 points Given the following code: arr= {'Wall Street', [4 7 10], {'Shia', [300 400 100], 'yes'}, 'no'}; A = arr{3}{1}(2); arr{1} = []; B = class(arr{2}(1)); arr2 = {arr arr(1)}; C = length(arr2); D = arr2{1}{3}{1}; E = 4 + arr(2); After the code is run in MATLAB what would be the value of the following variables? Enter the values as you would enter them in MATLAB, strings need to be in single quotes, logicals need to be true or false, and vectors need to be in square brackets (you do not need to type A= or B= etc.). If the code runs an error type 'error'. Assume the code continues to run after any errors occur. Do not comment your code. A
'h'

(4 points)

Correct Answer: 'h'

B
'double'

(4 points)

Correct Answer: 'double'

C
2

(4 points)

Correct Answer: 2

D
'error'

(4 points)

Correct Answer: 'Shia'

E
'error'

(4 points)

Correct Answer: 'error'

2.

(QID 1148)

12/20 points

There is a file in the current directory called 'yogi.txt'. It contains the following FIVE lines: Baseball is 90% mental, and the other half is physical.
Page 1 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

- Yogi Berra Now consider the following code. Trace the code to determine the values of the variables below. name = 'yogi.txt'; M = fopen(name,'r'); f = [fgetl(M) fgetl(M)]; y = find(f==' '); g = fgetl(M); fgetl(M); h = fgetl(M); k = fgetl(M); A B C D = = = = f(y(4)+1:y(5)-1) length(k) class(g) f(f=='b'|f==',')

fclose(name); After the code is run in MATLAB what would be the value of the following variables? Enter the values as you would enter them in MATLAB, strings need to be in single quotes, logicals need to be true or false, and vectors need to be in square brackets (you do not need to type A= or B= etc.). If the code runs an error type 'error'. Assume the code continues to run after any errors occur. Do not comment your code. A: B: C: D: Is the file closed correctly? Type 'yes' or 'no' (with the single quotes). A
'the other'

(4 points)

Correct Answer: 'the'

B
1

(4 points)

Correct Answer: 1

C
'char'

(4 points)

Correct Answer: 'char'

D
','

(4 points)

Correct Answer: 'b,'

Is the file closed correctly? Type 'yes' or 'no' (with the single quotes).
'no'

(4 points)

Correct Answer: 'no'

3.

(QID 1099)

9/10 points
Page 2 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

Function Name: pickAString Inputs (2): (char) A string (char) Another string Outputs (1): (char) the string that has more numbers in it Function Description: This function takes in two strings. The output is the string that has more numbers (digits '0' through '9'). If both strings have the same amount of numbers, the output is an empty string. * You may NOT use iteration to solve this problem. Test Cases A = wordChooser('MATLAB','CS1371'); A => 'CS1371' B = wordChooser('Beverly Hills 90210','Atlanta, GA 30332'); B => '' function more_nums=pickAString(str1,str2) ndx_no1= find(str1 >= '0' & str1 <= '9'); numbers_1= str1(ndx_no1); ndx_no2= find(str2 >= '0' & str2 <= '9'); numbers_2= str2(ndx_no2); length1= length(numbers_1); length2=length(numbers_2); if length1 > length2 more_nums= str1; elseif length2> length1 more_nums= str2; else more_nums=[]; end end TA Comments -1 | output is empty string if there are same number of digits | Needs Work
Graded By: JS

4.

(QID 1155)

12.5/15 points
Page 3 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

Function Name: priceLookup Inputs (3): (cell) a cell array of items in inventory (cell) a cell array of the items' prices (char) an item to search for Output (1): (double or char) the price of the item, or the string 'Item Does Not Exist' if no item is found Function Description: This function takes in two cell arrays of equal length. The first contains strings, which are the names of items in inventory. The second contains doubles, which are the prices of those items. The third input is a string which is the item to search for. If the item exists in inventory, then the output is the items price. If the item does not exist in inventory, then the output is the string 'Item Does Not Exist'. See the test cases below for clarification. When performing comparisons, case is important. There will be no duplicate item names. Test Cases: items = {'Milk', 'Eggs', 'Jackhammer', 'Dump Truck'}; prices = {2.49, 1.89, 45.95, 30000}; A = priceLookup(items, prices, 'Jackhammer'); A => 45.95 B = priceLookup(items, prices, 'Flux Capacitor'); B => 'Item Does Not Exist' function out= priceLookup(items,prices, want) out='Item Does Not Exist'; things= {items} for i=1:length(things) if strcmpi(items{i}, want) out= prices{i}; end end end TA Comments -2.5 | Locates the item name in the cell array of item names | Needs Work
Graded By: CJ

Page 4 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

5.

(QID 759)

17/20 points

Function Name: findHouses Inputs (2): - (struct) a structure array of house listings - (double) a minimum price Outputs (1): - (struct) a structure array of the house listings that meet the search criteria Function Description: The input structure array contains information on house listings with two fields, 'price' and 'zipcode'. The output is a structure array containing only the listings located in Zip Code '08873' AND that are more expensive (greater) than the minimum price given as an input to the function. Test Case: list = struct('price', {85000, 90000, 75000}, 'zipcode', {'08873' '08844' '08873'}); A = findHouses(list, 80000); A -> 1x1 struct (A.price -> 85000 and A.zipcode -> '08873') function str_final= findHouses(struct, min_price) str_final= for i=1:length(struct) if struct.zipcode(i)=='08873' if struct.price(i) > min_price str_final= [str_final struct(i)] end end end

end TA Comments
Graded By: KC

-3 | Uses strcmp on a cell array of the zipcodes or uses iteration to logically ignore those that aren't '08873' | Needs Work

6.

(QID 1158)

15/15 points

Function Name: countNames Inputs (2):


Page 5 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

(char) the filename of a .txt file containing a list of names (char) a last name to search for Output (1): (double) the number of names in the file that have the specified last name Function Description: This function takes in the name of a text file. The text file contains a list of names (firstname lastname). The second input to the function is a last name to search for. The output of the function is the number of names in the file where the last name matches the second input. Comparisons should be made case-sensitive. If no matches are found, the output is 0. See the example file and test case below for clarification. Note that the first line of the file is NOT part of the list of names. Contents of 'names.txt': FirstName LastName Matt Ryan Ryan Bennett Nolan Ryan Sammy Sosa Test Case: A = countNames('names.txt','Ryan') A => 2 function out= countNames(file,last_name) fh= fopen(file,'r'); line=''; last_name= [' ',last_name]; out=0; while ischar(line) line=fgetl(fh); if ischar(line) [first last]= strtok(line, ' '); if strcmp(last,last_name) out=out+1; end end end end TA Comments
Graded By: SS

Page 6 of 7

https://t-square.gatech.edu/access/content/attachment/25e84ee8-1178-48ba-aff1-090348188318/feedback.html

3/6/11 3:30 PM

Good Job!

Overall Exam Scores

Section
Multiple Choice Fill In The Blank Coding Grand Total

Your Score Possible Score


0.00 28.00 53.50 81.50 0.00 40.00 60.00 100.00

Page 7 of 7

Вам также может понравиться