Entering a structure into a function to sort
4 views (last 30 days)
Show older comments
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
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
Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!