CS 1371 Exam 1 Prep

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/46

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

47 Terms

1
New cards
term image

function answer = isGreater (n1, n2)

answer = n1 > n2;

end

2
New cards
term image

function area = recArea (width, length)

area = round(width * length, 2);

end

3
New cards
term image

function answer = withTip(bill,tip)

answer = round(bill + (tip * bill),2);

end

4
New cards
term image

function [sumAns, prodAns, meanAns] = allMath(n1,n2,n3)

sumAns = round(n1 + n2 + n3, 2);

prodAns = round(n1 * n2 * n3, 2);

meanAns = round(sumAns/3, 2);

end

5
New cards
term image

function [numMats, costMats] = matMath(w,l, c)

areaMat = w * l;

numMats = round(areaMat, 2);

costMats = round(numMats * c,2);

end

6
New cards
term image

function [needed, leftOver] = theSandbox(w,l,hFt,n)

wFt = w / 12;

lFt = l * 3;

vFt3 = wFt lFt hFt;

total = ceil(vFt3/3);

needed = max(0, total - n);

have = needed + n;

leftOver = round(have * 3 - vFt3, 2);

end

7
New cards
term image

function [score, logical] = raiseMyGrade(Test1, Test2, Test3)

Test4 = 90 * 4 - (Test1 + Test2 + Test3);

score = Test4;

logical = (score <= 100);

end

8
New cards
term image

function answer = isFirstLast (vec)

answer = vec(1) == vec(end);

end

9
New cards
term image

function answer = getLetter(letter)

answer = char('a' + (letter - 1));

end

10
New cards
term image

function answer = calculateTotal(cost,tip)

answer = cost + (cost.*tip/100);

answer = round(answer, 2);

end

11
New cards
term image

function answer = swapBigSmall(vec)

answer = vec;

maxNum = max(vec);

minNum = min(vec);

answer(vec == maxNum) = minNum;

answer(vec == minNum) = maxNum;

end

12
New cards
term image

function answer = deleteMiddleThird(vec)

l = length(vec);

answer = [vec(1:(l/3)), vec(1+(2*l/3):end)];

answer = answer(end: -1 : 1);

end

13
New cards
term image

function answer = hasRepeats(vec)

answer = any(diff(sort(vec)) == 0);

end

14
New cards
term image

A = 'char'

B = 'Whos thE GoAt BUzz'

15
New cards
term image

D = 'Whos the Goat Buzz'

16
New cards
<p><span style="color: rgb(0, 0, 0);">A = Given the string str1 that contains one or more instances of str2, produce a vector of all the starting indices of str2 in str1.</span></p>

A = Given the string str1 that contains one or more instances of str2, produce a vector of all the starting indices of str2 in str1.

A = strfind(str1, str2)

17
New cards
<p><span style="color: rgb(0, 0, 0);">B = Given the string str3, replace all of the occurrences of str2 in str1 with the string str3.</span></p>

B = Given the string str3, replace all of the occurrences of str2 in str1 with the string str3.

B = strrep(str1, str2, str3)

18
New cards
<p><span style="color: rgb(0, 0, 0);">C = Capitalize the string str1.</span></p>

C = Capitalize the string str1.

C = upper(str1)

19
New cards
<p><span style="color: rgb(0, 0, 0);">D = Split the string str1 into two parts at the first space. The first part should be everything before the space. Store the first part in D.</span></p><p><span style="color: rgb(0, 0, 0);">E = Store the second part of the string from part D in E, which contains the space and everything afterwards.</span></p>

D = Split the string str1 into two parts at the first space. The first part should be everything before the space. Store the first part in D.

E = Store the second part of the string from part D in E, which contains the space and everything afterwards.

[D, E] = strtok(str1, ' ')

20
New cards
<p><span style="color: rgb(0, 0, 0);">F = Find the character that has an ASCII value corresponding to the double num.</span></p>

F = Find the character that has an ASCII value corresponding to the double num.

F = char(num)

21
New cards
<p><span style="color: rgb(0, 0, 0);">G = Given the double num, create the character vector version of this number.</span></p>

G = Given the double num, create the character vector version of this number.

G = num2str(num)

22
New cards
<p><span style="color: rgb(0, 0, 0);">H = Given the variables name and age, use the sprintf() function to create the sentence: </span></p><p><span style="color: rgb(0, 0, 0);">‘My name is &lt;insert name&gt; and I am &lt;insert age&gt; years old.’ </span></p><p><span style="color: rgb(0, 0, 0);">For example, if name is ‘Kantwon’ and age is 31, then the sentence should be: ‘My name is Kantwon and I am 31 years old.’</span></p>

H = Given the variables name and age, use the sprintf() function to create the sentence:

‘My name is <insert name> and I am <insert age> years old.’

For example, if name is ‘Kantwon’ and age is 31, then the sentence should be: ‘My name is Kantwon and I am 31 years old.’

H = sprintf('My name is %s and I am %i years old.', name, age)

23
New cards
<p><span style="color: rgb(0, 0, 0);">I </span><span style="color: rgb(0, 0, 0);">= Given str1 and str2, produce a logical of whether or not they are the same string (case sensitive).</span></p>

I = Given str1 and str2, produce a logical of whether or not they are the same string (case sensitive).

I = strcmp(str1, str2)

24
New cards
<p><span style="color: rgb(0, 0, 0);">J = Produce a single logical of if str2 is contained within str1</span></p>

J = Produce a single logical of if str2 is contained within str1

J = contains(str1, str2)

25
New cards
<p><span style="color: rgb(0, 0, 0);">A = Create a mask that is true at all positions of vec1 that contain a number greater than 4</span></p>

A = Create a mask that is true at all positions of vec1 that contain a number greater than 4

A = vec1 > 4

26
New cards
<p><span style="color: rgb(0, 0, 0);">B = Create a mask that is true at all positions of vec1 that contain an even number</span></p>

B = Create a mask that is true at all positions of vec1 that contain an even number

B = mod(vec1, 2) == 0

27
New cards
<p><span style="color: rgb(0, 0, 0);">C = Create a mask that is true at all positions of vec1 that contain an even number that is greater than 4</span></p>

C = Create a mask that is true at all positions of vec1 that contain an even number that is greater than 4

C = (mod(vec1, 2) == 0) & (vec1 > 4)

28
New cards
<p><span style="color: rgb(0, 0, 0);">D = Produce the quantity of 4’s that are in vec1</span></p>

D = Produce the quantity of 4’s that are in vec1

D = sum(vec1==4)

29
New cards
<p><span style="color: rgb(0, 0, 0);">E = Create a mask that is true at all positions in str1 that contain a space</span></p>

E = Create a mask that is true at all positions in str1 that contain a space

E = (str1 == ' ')

30
New cards
<p><span style="color: rgb(0, 0, 0);">F = Create a mask that is true at all positions in str1 that are NOT a space</span></p>

F = Create a mask that is true at all positions in str1 that are NOT a space

F = (str1 ~= ' ')

31
New cards
<p><span style="color: rgb(0, 0, 0);">G = Delete all of the spaces in str1 and save this newly modified string in G (it is ok if you do this on multiple lines. Your final string just needs to be saved in the variable G)</span></p>

G = Delete all of the spaces in str1 and save this newly modified string in G (it is ok if you do this on multiple lines. Your final string just needs to be saved in the variable G)

G = str1(str1 ~= ' ')

32
New cards
<p><span style="color: rgb(0, 0, 0);">H = Using masking, create a vector of indices of all the locations of capital E’s in str2</span></p>

H = Using masking, create a vector of indices of all the locations of capital E’s in str2

H = (1:length(str2)) .* (str2 == 'E')

H = H(H~=0)

mask = double(str2) >= 97 & double(str2) <= 122

33
New cards
<p><span style="color: rgb(0, 0, 0);">I = Using masking, create a vector of all of the lowercase letters in str2</span></p>

I = Using masking, create a vector of all of the lowercase letters in str2

I = str2(mask)

34
New cards
term image

function [n, s] = endCap(vec)

n = length(vec);

s = vec;

s(end) = upper(vec(end));

end

35
New cards
term image

function out = rearrange(vec1, vec2)

[newOrder, originalVec] = sort(vec1);

out = vec2(originalVec);

end

36
New cards
term image

function out = inInterval(lBound, uBound, vec)

out = vec(vec >= lBound & vec <= uBound);

end

37
New cards
term image

function ratio = vowelsToWordRatio(str)

str = lower(str);

vowelsInStr = sum(str == 'a' | str == 'e' | str == 'i' | str == 'o' | str == 'u');

wordsInStr = sum(str == ' ') + 1;

ratio = round(vowelsInStr/wordsInStr, 2);

end

38
New cards
term image

function out = uncensor(str1, str2)

out = str1;

mask = (str1 == '*');

out(mask) = str2(mask);

end

39
New cards
term image

function score = stringScore(str)

len = length(str);

spaces = sum(str == ' ');

score1 = 10 * (spaces > 0 & mod(len,spaces)==0);

capitalized = sum(double(str) >= 65 & double(str)<= 90);

totalLetters = sum(str ~= ' ');

score2 = 7 * (capitalized / totalLetters >= 0.05);

notSpaces = str(str ~= ' ');

capLetters = double(notSpaces) >= 'A' & double(notSpaces) <= 'Z';

lowLetters = double(notSpaces) >= 'a' & double(notSpaces) <= 'z';

negPoint = capLetters(1: end-1) & lowLetters(2:end);

score3 = -sum(negPoint);

low = lower(notSpaces);

revString = low(end: -1: 1);

palindrome = isequal(low, revString);

score4 = 50 * palindrome;

score = score1 + score2 + score3 + score4;

end

40
New cards
term image

function [word, len] = longestWord(str)

spaces = [0, strfind(str,' '), length(str)+1];

lenofWord = diff(spaces) - 1;

[len, indices] = max(lenofWord);

word = str(spaces(indices) + 1 : spaces(indices + 1) - 1);

end

41
New cards
term image

function out = howManyPs(data)

ascii = double(data);

mask = ascii == 80;

out = sum(mask(:));

end

42
New cards
term image

function out = crossoutEvenColumns(data)

[r,c] = size(data);

out = data;

even = 2:2:c;

out(:, even) = 'X';

end

43
New cards
term image

function out = deleteOddRows(data)

[r, ~] = size(data)

odd = 1:2:r;

data(odd,:) = [];

out = data;

end

44
New cards
term image

function [avg, count] = countSalesAboveAvg(sales)

avg = floor(mean(sales(:)));

count = sum(sales(:) > avg);

end

45
New cards
term image

function [totals, sorted] = topGolf(scores, names)

totals = sum(scores, 2);

[totals, idx] = sort(totals, 'ascend');

sorted = names(idx,:);

end

46
New cards
term image

function Outarr = sortTheRows(arr, criteria)

col = str2num(criteria(1));

order = criteria(2);

[~, idx] = sort(arr(:,col));

revIdx = idx(end: -1: 1);

Descending = (order == 'D');

finIdx = idx.*(~Descending)+revIdx.*Descending;

Outarr = arr(finIdx, :);

end

47
New cards
<p><span style="color: rgb(0, 0, 0);">Examples:</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">temps = [89 80 27 68 73 66 78</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">92 10 34 14 11 50 72]</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">[avg, map] = warmerDays(temps)</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">&gt;&gt; avg = 54.57</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">&gt;&gt; map = ['HH.HHHH'</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">'H.....H']</span></p>

Examples:
temps = [89 80 27 68 73 66 78
92 10 34 14 11 50 72]
[avg, map] = warmerDays(temps)
>> avg = 54.57
>> map = ['HH.HHHH'
'H.....H']

function [avg, map] = warmerDays(temps)

avg = round(mean(temps(:)), 2);

hot = temps > avg;

[r,c] = size(temps);

map = char(46 * ones(r,c));

map(hot) = 'H';

end