1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
This is a tracing problem.
The functions alpha() and beta() are defined and saved in the Current Folder.
The indicated lines of code are run in the Command Window.
Fill in the blanks on each line with the value stored in the indicated variable(s) after that line has been executed.
Notes:
Include single quotes at the beginning and end of vectors of characters. (e.g. 'ABCDE' )
Include brackets at the beginning and end of vectors of numbers. (e.g. [1 2 3 4] )
Indicate logical true as true and logical false as false.
Include brackets at the beginning and end of vectors of logicals. (e.g. Don't type [1 0 1 0], Do type [true false true false] )
z = 'Ali$Ola#May'
a = 'Ali Ola May'
c = [4 8]
d = [8 4]
a = 'Ali$Ola#May'
e = 'Ali$Ola#May'
a = [4 8]
b = [2 1]
c = [8 4]
The solution for the following coding problem is given below. However, there are blanks in the code.
Fill in the blanks so that the function runs correctly.
Function Name: middleLastFirst
Inputs:
[1xN Double] A vector
Output:
[1xN Double] Modified vector
Description:
Write a function called middleLastFirst() that takes in a vector and rearranges its thirds so that the middle third is first, the last third is in the middle, and the first third is last. It is guaranteed that the input vector will have a length that is divisible by 3.
p = length(v)
t = p/3
t2 = t* 2
w = v(1: t)
h = v( t+1:t2)
b = v( t2+1 :end)
v = [h b w]
function [base10] = base2ToBase10(base2)
a = base2(8);
b = base2(7)*2;
c = base2(6)*4;
d = base2(5)*8;
e = base2(4)*16;
f = base2(3)*32;
g = base2(2)*64;
h = base2(1)*128;
base10 = a+b+c+d+e+f+g+h;
end
function pair = farApart(voc)
diffs = diff(voc)
[val, ind] = max(diffs)
pair = voc([ind, ind+1])
end
function [score, name] = topName(data)
d = double(data)
d(:,4) = sum(d,2)
[vals, ord] = sort(d(:,4),'d')
score = vals(2)
name = data(ord(2),:)
end
z = 'The winner is Jerry'
a = 'Jerry'
c = ['Jerry' ; 'Darci']
d = 'Jerry'
e = 'The winner is Jerry'
a = ['Jerry' ; 'Darci']
c = 1
c = 'Jerry'
Function: phraseScore()
Input:
(char): A vector of characters representing an phrase
Output:
(double): A score for the phrase
Description:
Write a function named phraseScore() that calculates an phrase's score via the following algorithm.
Add 4 points for every lowercase consonant.
Subtract 3 points for every uppercase vowel.
Add 5 points for every word.
Notes:
There are only letters and spaces in the input.
The words are separated by single spaces.
Example:
>> phrase = 'I do not like Green Eggs and Ham'
>> score = phraseScore(phrase)
lower
q=='u'
p<='z'
m
p=='U'
sum
-3
32
1
5
Function Name: percentages
Input:
(char) A string of any length containing only lowercase 'r's, 'g's or 'b's
Output:
(double) The percentage of 'r's in the input string
(double) The percentage of 'g's in the input string
(double) The percentage of 'b's in the input string
Note:
The find() function is banned
Round percentages to two decimal places
Examples:
>> [pr1, pg1, pb1] = percentages('rgb')
>> pr1 = 33.3300
>> pg1 = 33.3300
>> pb1 = 33.3300
>> [pr2, pg2, pb2] = percentages('rgrgr')
>> pr2 = 60
>> pg2 = 40
>> pb2 = 0
>> [pr3, pg3, pb3] = percentages('rggrbrgbgg')
>> pr3 = 30
>> pg3 = 50
>> pb3 = 20
function [pr, pg, pb] = percentages(str)
total = length(str);
nr = sum( str=='r' );
ng = sum( str=='g' );
nb = sum( str=='b' );
pr = round(100*nr/total, 2);
pg = round(100*ng/total, 2);
pb = round(100*nb/total, 2);
end
Function: swapNext2BigNext2Small
Input:
(1xN double) A Vector of numbers (N>=4)
Output:
(1xN double) A Vector
Function Description:
Given a 1XN vector where N>=4, return a new vector with the next to largest and next to smallest values swapped
Note(s):
All values that are not being swapped should stay in the same location.
There will be no duplicate values in the vector
Hint(s):
sort() may be useful
Use of a variable to hold a value may be helpful
Examples:
>> ans1 = swapNext2BigNext2Small([3 6 4 1 5 8 9 7 0])
ans1 = [3 6 4 8 5 1 9 7 0]
>> ans2 = swapNext2BigNext2Small([3 6 2 5])
ans2 = [5 6 2 3]
>> ans3 = swapNext2BigNext2Small([30 40 10 20 50 80])
ans3 = [30 40 10 50 20 80]
function [vec] = swapNext2BigNext2Small(vec)
[~, order] = sort(vec);
temp = vec(order(2));
vec(order(2)) = vec(order(end-1));
vec(order(end-1)) = temp;
end
Function: topSalesQuarter
Input:
(double) Nx4 array representing sales data
Output:
(double) The number of the quarter with the highest sales
(double) (N+1)x4 array containing augmented sales data
Function Description:
You are given an array of sales data. Each of the four columns represent each region's sales from the four quarters of last year ('Qtr1, Qtr2, Qtr3, Qtr4, respectively)
Your first task is to add a row to the bottom of the sales array that contains the total sales for each quarter
Your second task is to return the number of the quarter (1, 2, 3, or 4) that had the highest total sales last year
Your third task is to return the sales array with the additional row of totals.
Note(s):
No conditional (e.g. if or switch) or iteration (e.g. for or while) statements can be used to solve this problem. If they are used, you will receive zero credit for this problem.
You are guaranteed that the sales data array will have four columns of data
The quarterly totals with be unique
There may be more than one quarter that has sales equal to the maximum. Your output should contain a row for each of these regions
Hint(s):
Use of the colon ":" and "end" to index can be helpful in accessing the last row or column or appending a row or column.
Use of the sum(), max() and char() functions may be useful
Masking may be useful
Examples:
>> sales = [[2 5 3 7];[6 2 1 4];[0 9 7 3]; [9 5 9 5]];
>> [topQtr, salesSummary] = topSalesQuarter(sales)
topQtr = 2
salesSummary = 5×4 2 5 3 7
6 2 1 4
0 9 7 3
9 5 9 5
17 21 20 19
>> sales = [[2 5 4 9];[6 2 1 4];[4 5 17 3]; [3 5 4 9]; [13 1 2 6]; [3 2 4 6]];
>> [topQtr, salesSummary] = topSalesQuarter(sales)
topQtr = 4
salesSummary = 7×4 2 5 4 9
6 2 1 4
4 5 17 3
3 5 4 9
13 1 2 6
3 2 4 6
31 20 32 37
>> sales = [[2 5 3 1];[6 2 1 4]];
>> [topQtr, salesSummary] = topSalesQuarter(sales)
topQtr = 1
salesSummary = 3×4
2 5 3 1
6 2 1 4
8 7 4 5
function [maxQtr, sales] = topSalesQuarter(sales)
sales(end+1,:) = sum(sales,1);
[maxVal, maxQtr] = max(sales(end,:));
end
This is a tracing problem.
The functions alpha() and beta() are defined and saved in the Current Folder.
The indicated lines of code are run in the Command Window.
Fill in the blanks on each line with the value stored in the indicated variable(s) after that line has been executed.
Notes:
Include single quotes at the beginning and end of vectors of characters. (e.g. 'ABCDE' )
Include brackets at the beginning and end of vectors of numbers. (e.g. [1 2 3 4] )
Indicate logical true as true and logical false as false.
Include brackets at the beginning and end of vectors of logicals. (e.g. Don't type [1 0 1 0], Do type [true false true false] )
y = 19
a = 'ABABA'
b = [true false true false true]
c = [false true false true false]
d = 13
e = 6
f = 19
a = [true false true false true]
c = [3 2 3 2 3]
d = 13
a = [false true false true false]
c = 6
Function: digitTotal()
Input:
(char): A vector of characters
Output:
(double): The sum of the digits in the input
Description:
Write a function named digitTotal() that calculates the input vector's score via the following algorithm.
Add 2 points for every even digit, except zero.
Add 3 point for every odd digit
Subtract 4 points for every digit that is a zero.
Note(s):
The example input below was generated using AI.
Example:
>> str = 'In 1963, the school was built with 24 classrooms, and 58 years later, they added 10 more for a total of 34'
>> total = digitTotal(str)
49
'1'
m
0
2
1
3
'0'
48
sum
4
sum
Function: mixedInfo
Input:
(double) A 1x3N Vector of numbers (The length of the vector will be a multiple of 3)
Output:
(double) The height of the shortest person
(double) How much shorter they are than the average height of all the people in the vector
(double) The weight of the heaviest person
(double) How much heavier they are than the average weight of all the people in each vector
(double) The difference in age between the youngest and oldest person
Function Description:
The data in the input vector is arranged as concatenated groups of three (triples). Each triple represents three pieces of information about a different person.
The first number in each triple represents a person's height.
The second number in each triple represents that same person's weight.
The third number in each triple represents that same person's age.
Write a function named mixedInfo() that will take in the input vector of information as described above and output the six values as described in the Output section, in the order that they are described.
Note(s):
All differences should be positive values
All final values should be rounded to whole numbers. (Don't round until you have performed all other calculations.)
Hint(s):
The mean() , round(), sort(), max(), min() , abs() functions may be useful
Examples:
>> [shortHt1, shortDelta1, heavyWt1, heavyDelta1, ageDelta1] = mixedInfo([72 180 24 64 130 20 74 200 19 70 120 22])
shortHt1 = 64
shortDelta1 = 6
heavyWt1 = 200
heavyDelta1 = 43
ageDelta1 = 5
[shortHt2, shortDelta2, heavyWt2, heavyDelta2, ageDelta2] = mixedInfo([60 100 27 61 120 18 69 115 26 62 110 37 68 125 25])
shortHt2 = 60
shortDelta2 = 4
heavyWt2 = 125
heavyDelta2 = 11
ageDelta2 = 19
[shortHt3, shortDelta3, heavyWt3, heavyDelta3, ageDelta3] = mixedInfo([76 220 30 73 201 31 77 245 21])
shortHt3 = 73
shortDelta3 = 2
heavyWt3 = 245
heavyDelta3 = 23
ageDelta3 = 10
function [shortHt, shortDelta, heavyWt, heavyDelta, ageDelta] = mixedInfo(data)
heightData = data(1:3:end);
weightData = data(2:3:end);
ageData = data(3:3:end);
shortHt = min(heightData);
shortDelta = round(abs(mean(heightData)-shortHt));
heavyWt = max(weightData);
heavyDelta = round(abs(mean(weightData)-heavyWt));
ageDelta = max(ageData)-min(ageData);
end
Function Name: camelCase
Input:
(char) A string containing words separated by spaces
Output:
(char) A string in camelCase
Function Description:
When you are creating variable names that consist of multiple words, one way to make it easier to read it to use a technique called camel case. Camel casing is when you take combine together multiple words and have the first letter of the first word lower case, but the first letter of every other word capitalized. It's supposed to look like the ups and downs of a camel's humps.
Examples of camel case are: matlabIsTheBest, sortedVec, maxInd
Given an input that is a string, change it to be camel case. The string is guaranteed to be all lowercase, and words are guaranteed to be separated by only one space.
Note:
The find() function is banned
This can be done with masking or the strfind() function. Figure out how to do it both ways!
Examples:
>> out = camelCase('matlab is the best')
out =
'matlabIsTheBest'
>> out = camelCase('thisisoneword')
out =
'thisisoneword'
>> out = camelCase('georgia institute of technology')
out =
'georgiaInstituteOfTechnology'
function in = camelCase(in)
spaceInds = strfind(in,' ');
firstLetInds = spaceInds+1;
in(firstLetInds) = upper(in(firstLetInds));
in(spaceInds) = [];
end
Function: medalTotals
Input:
(Nx3 double) Array representing the number of gold, silver and bronze medals won by each country
Output:
((N+1)x4double) Sorted array with a totals row and a totals column appended to the bottom and the right respectively
Function Description:
You are given an array containing the number of gold, silver and bronze medals of the countries participating in an event. Your function should change this table in three ways:
append a fourth column that contains the total number of medals in each row
sort all of the rows based on the new fourth column in descending order
append a new row to the bottom of the table containing the total number of medals in each column (including the new fourth one)
Hint(s):
Use of the colon ":" and "end" to index can be helpful in accessing the last row or column or appending a row or column.
Use of the transpose operator (') may be helpful
The sum() and sort() functions may be helpful
Examples:
>> medals = [6 5 6; 5 2 6; 0 9 5; 8 5 4]
>> [medals] = medalTotals(medals)
medals =
6 5 6 17
8 5 4 17
0 9 5 14
5 2 6 13
19 21 21 61
>> medals = [8 5 3; 6 2 4; 0 1 1; 8 5 4]
>> [medals] = medalTotals(medals)
medals =
8 5 4 17
8 5 3 16
6 2 4 12
0 1 1 2
22 13 12 47
>> medals = [2 4 3; 5 3 2; 5 1 3; 6 1 2]
>> [medals] = medalTotals(medals)
medals =
5 3 2 10
2 4 3 9
5 1 3 9
6 1 2 9
18 9 10 37
function [medals] = medalTotals(medals)
medals(:,end+1) = sum(medals');
[~, order] = sort(medals(:,4), 'd');
medals = medals(order,:);
medals(end+1,:) = sum(medals);
end