Entering a structure into a function to sort

4 views (last 30 days)
AH
AH on 10 Nov 2019
Commented: Walter Roberson on 11 Nov 2019
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
AH
AH on 11 Nov 2019
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?
Walter Roberson
Walter Roberson on 11 Nov 2019
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.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!