Like many other computer languages, MATLAB has a type of container called a stru
ID: 3864879 • Letter: L
Question
Like many other computer languages, MATLAB has a type of container called a structure which allows for multiple types of data to be stored together. In MATLAB, structures are used when creating graphical user interfaces (GUIs), when writing information to an excel file, and with curve-fitting features. This problem requires you to explain sections of code and to finish writing a user-written function to provide you with exposure to structures. Please open the associated (.m) file. For this problem, complete the following two items: Respond to each of the 10 numbered prompts that appear in the comments of the .m file. Your responses should be typed. Complete the user-written function that calculates a student’s final grade. Additional information: Brief description of the code: (1) an array of assignment categories and their associated weights are stored (generally in response to user-input) (2) Then, points associated with each assignment within each category as stored. (3) The structure arrays created by step 1 and step 2 are passed to a user-defined function that calculates the final grade. For example, the code can be used to calculate a course grade given the following information: Six homework assignments, each out of ten points (altogether, 20% of the final grade) Three midterms, each out of a hundred points (altogether, 50% of the final grade) One final, out of 100 points (30% of the final grade). %% 9 fprintf(' 9) ') requestUserInput = ~isempty(input('Would you like this script to accept user input? (press return for no)')); % 9-Category input categories = {'Homework';'Midterm';'Final';'Done'}; weights = [20 50 30]; done = false; idx = 1; % 1) What is accomplished by this region? while ~done category = my_input(requestUserInput, categories{idx}, 'Enter an assignment category (or Done)', 's'); if strcmp(category, 'Done') % 2) What is accomplished by this region? done = true; else % 3) What is accomplished by this region? cats(idx).ID = idx; cats(idx).Type = category; cats(idx).Weight = my_input(requestUserInput, weights(idx), 'Enter a weight for this category (read as a %)'); idx = idx + 1; end end userInputs = [7 8 7 9 8 9 -1 65 85 100 -1 100 -1]; worths = [10 10 10 10 10 10 100 100 100 100]; done = false; idxCat = 1; idxInputs = 1; idxScore = 1; %% 9-Grade input % 4) What is accomplished by this region? while ~done fprintf('Enter a %s score (or -1 for done) (# pts earned) ', cats(idxCat).Type); earned = my_input(requestUserInput, userInputs(idxInputs),''); % input (); if earned == -1 && idxCat == length(cats) % 5) What is accomplished by this region? done = true; elseif earned == -1 % 6) What is accomplished by this region?] idxCat = idxCat + 1; else % 7) What is accomplished by this region? scores(idxScore).ID = cats(idxCat).ID; scores(idxScore).Type = cats(idxCat).Type; scores(idxScore).Earned = earned; scores(idxScore).Worth = worths(idxScore); idxScore = length(scores) + 1; end idxInputs = idxInputs + 1; end finalgrade = calculateGrade (cats, scores); fprintf('The final grade for this student is %.2f%% ',finalgrade); end %% 9 Functions: % 8) What is accomplished by this function? function res = my_input(doPrompt, fallback, promptStr, option) if doPrompt if nargin == 4 res = input(promptStr,option); else res = input(promptStr); end else res = fallback; end end %% Complete the function below (function should calculate final grade and include the lines below) function calculateGrade % Note: the square brackets used below group together all the fields of a % structure array into there own array. (e.g. if we created an array of % structures, 'persons', where there is one structure for each person in % the class, then [persons.Age] would be an array of ages) for i = 1:length(scores) % 9) Explain this line in your own words: tot = sum(scores(i).ID == [scores.ID]); % Number of points in an assignment category (e.g. 6 homework assignments) % 10) Explain this line in your own words: weight = cats(scores(i).ID == [cats.ID]).Weight; % Weight of that assignment category (e.g. homework is worth 20%) end end
10 numbered prompts
Explanation / Answer
function Q7
clear
close all
clc
%% 9
fprintf(' 9) ')
requestUserInput = ~isempty(input('Would you like this script to accept user input? (press return for no)'));
%% 9-Category input
categories = {'Homework';'Midterm';'Final';'Done'};
weights = [20 50 30];
done = false;
idx = 1;
% 1) What is accomplished by this region?
% Ans 1) It asks for the categories from the user and then assign the weightage
% to each category as the user wants.
while ~done
category = my_input(requestUserInput, categories{idx}, 'Enter an assignment category (or Done)', 's');
if strcmp(category, 'Done') % 2) What is accomplished by this region?
done = true; % Ans 2) It checks whether there is any category left or all done.
else % 3) What is accomplished by this region?
cats(idx).ID = idx; % Ans 3) If a new category, assigns a ID, and weight to it.
cats(idx).Type = category;
cats(idx).Weight = my_input(requestUserInput, weights(idx), 'Enter a weight for this category (read as a %)');
idx = idx + 1;
end
end
userInputs = [7 8 7 9 8 9 -1 65 85 100 -1 100 -1];
worths = [10 10 10 10 10 10 100 100 100 100];
done = false;
idxCat = 1;
idxInputs = 1;
idxScore = 1;
%% 9-Grade input
% 4) What is accomplished by this region?
while ~done
fprintf('Enter a %s score (or -1 for done) (# pts earned) ', cats(idxCat).Type);
earned = my_input(requestUserInput, userInputs(idxInputs),''); % input ();
if earned == -1 && idxCat == length(cats) % 5) What is accomplished by this region?
done = true; % Ans 5) It checks whether scores has been entered for all the exams in all categories
elseif earned == -1 % 6) What is accomplished by this region?]
idxCat = idxCat + 1; % Ans 6) It checks whether scores has been entered for all exams of a particular category, then move on to next
else % 7) What is accomplished by this region?
scores(idxScore).ID = cats(idxCat).ID; % Ans 7) It fills the array of scores structure with the required values for a particular exam.
scores(idxScore).Type = cats(idxCat).Type;
scores(idxScore).Earned = earned;
scores(idxScore).Worth = worths(idxScore);
idxScore = length(scores) + 1;
end
idxInputs = idxInputs + 1;
end
finalgrade = calculateGrade (cats, scores);
fprintf('The final grade for this student is %.2f%% ',finalgrade);
end
%% 9 Functions:
% 8) What is accomplished by this function?
% Ans 8) This functions first checks whether user wants to provide
% input or not. If yes, then it prompts the user with the string passed in the
% parameter 'promptStr'. If no, then it returns the default value passed in
% the parameter variable 'fallback'.
function res = my_input(doPrompt, fallback, promptStr, option)
if doPrompt
if nargin == 4
res = input(promptStr,option);
else
res = input(promptStr);
end
else
res = fallback;
end
end
%% Complete the function below (function should calculate final grade and include the lines below)
function finalgrade = calculateGrade(cats, scores)
% Note: the square brackets used below group together all the fields of a
% structure array into there own array. (e.g. if we created an array of
% structures, 'persons', where there is one structure for each person in
% the class, then [persons.Age] would be an array of ages)
finalgrade = 0;
temp1 = 0;
temp2 = 0;
j = 0;
for i = 1:length(scores)
% 9) Explain this line in your own words:
% Ans 9) It finds the number of exams of a particular category.
tot = sum(scores(i).ID == [scores.ID]); % Number of points in an assignment category (e.g. 6 homework assignments)
% 10) Explain this line in your own words:
% Ans 10) It finds out the weightage of the exam of a particular category
weight = cats(scores(i).ID == [cats.ID]).Weight; % Weight of that assignment category (e.g. homework is worth 20%)
temp1 = temp1 + scores(i).Earned;
temp2 = temp2 + scores(i).Worth;
j = j + 1;
if j == tot
finalgrade = finalgrade + (temp1/temp2) * weight;
temp1 = 0;
temp2 = 0;
j = 0;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.