MATLAB Answers

0

Entering a structure into a function to sort

Asked by Ariana Hall on 10 Nov 2019 at 20:38
Latest activity Commented on by Walter Roberson
on 11 Nov 2019 at 23:04
So I am struggling with a homework assignment for my course. I am supposed to write a function that consumes an array of student structures (name, semester GPA, cumulative GPA) and produces an array of the names of those with a semester GPA between 2.9 and 2.99. Here is the exact question: "Your university has added a new award for studets who were "almost there" last semester and just missed getting into the Dean's List. Write a function called "almost" that consumes an array of student structures, and produces an array of names of those who have a semester GPA between 2.9 and 2.99 (inclusive). The student structure has the following fields:
Name - string (e.g. 'George P. Burdell')
Semester_GPA - decimal number (e.g. 2.97)
Cumulative_GPA - decimal number (e.g. 3.01)
This is what I have, but I do not know how to get it to return an array of the specific names from the many that are given. Thank you in advance!
% The script that I have:
condition1 = true;
while condition1
choice = input('Do you want to enter student information? [0=no],[1=yes]: ');
if (choice==0)
disp(entry)
condition1 = false;
else (choice == 1);
condition2 = true;
while condition2
name = input('Enter the name of the student: ', 's');
semester_GPA = input('Enter the semester GPA of the student: ');
cumulative_GPA = input('Enter the cumulative GPA of the student: ');
almostAward = almost(name, semester_GPA, cumulative_GPA);
disp(entry.name)
condition2 = false;
end
end
end
% And the function
function almostAward = almost(name, semester_GPA, ~)
if (2.9<=semester_GPA && semester_GPA<=2.99)
entry.Name = name;
condition = false;
else
condition = false;
end
ans.name = name;
end

  3 Comments

Write a function called "almost" that consumes an array of student structures, and produces an array of names
Your function named almost expects a parameter name (of size and type unknown) and a scalar numeric value semester_GPA . This is contrary to the instructions that the function must accept an array of student structures -- you have the wrong number of input arguments, and the wrong types.
Name - string (e.g. 'George P. Burdell')
The example is not a string, it is a character vector. This affects what it means for there to be an array of names on output. An array of string objects is one possibility and that would be natural if the input Name really are string rather than the character vector given in the example. Another possibility is that the output should be a cell array of character vectors, as was common in earlier releases, but the assignment does not mention anything about cell arrays.
Thank you for the answer. I understand what you are saying, but unfortunately I have no idea how to fix it. This is a beginning MATLAB course, and my professor is extremely unhelpful. I mean, I don't even know how to write a function that consumes a structure in general. Can you give me any ideas on how to fix it?
function almostAward = almost(student_info_struct)
almostAwardMask = false(size(student_info_struct));
for K = 1 : numel(student_info_struct)
this_student_GPA = student_info_struct(K).student_GPA
now under some conditions set almostAwardMask(K) to true
end
Now use almostAwardMask to figure out what list of strings to create in almostAward
end

Sign in to comment.

Tags

0 Answers