Student Cell Array help?
9 views (last 30 days)
Show older comments
I am trying to finish my matlab homework and am currently stuck. Currently my program asks how many students there are in a class. Then asks for each of their name's and five letter grades and inputs them into two cells called Names{a} and Grades{a}. Now, our professor provided us with a fuction called gradeStatus. Here it is.
function status = gradeStatus(grades)
%Input: Grades- a string array of 5 characters
%Output: GOOD if the the grades array has more A's, B's or C's than other grades.
numGood = 0;
for i = 1:5
curGrade = upper(grades(i));
if(strcmp(curGrade,'A') || strcmp(curGrade,'B') ||strcmp(curGrade,'C'))
numGood = numGood + 1;
end
end
if(numGood > 2)
status = 'GOOD';
else
status = 'BAD';
end
The last part of our program needs to say the students in Good standing and Bad standing. As well as list the students that have bad grades.
Like this There are x student in GOOD standing and x student in BAD standing The students in bad standing:
Here is my code so far.
clear;
clc;
numStudents = input('Enter the number of students: '); % or however many
Names = cell(numStudents,1);
Grades = cell(numStudents,1);
for a = 1:numStudents
fprintf('Information for student %g\n',a)
Names{a} = input('Enter the name of the student: ', 's');
Grades{a} = input('Enter the students grades: ', 's');
Status{a} = gradeStatus(Grades{a});
end
Any help I can get would be great!
Accepted Answer
Image Analyst
on 10 Nov 2014
Edited: Image Analyst
on 10 Nov 2014
Like this (untested):
for a = 1 : numStudents
fprintf('Information for student %g\n',a)
Names{a} = input('Enter the name of the student: ', 's');
Grades{a} = input('Enter the students grades: ', 's');
Status{a} = gradeStatus(Grades{a});
% Keep track of whether this student is good or bad.
inGoodStanding(a) = strcmpi(Status{a}, 'GOOD')
end
% Print out required stuff.
numGood = sum(inGoodStanding);
numBad = numStudents - numGood;
fprintf('There are %d students in GOOD standing, and %d students in BAD standing\n', numGood, numBad);
fprintf('The students in bad standing:\n');
for a = 1:numStudents
if ~inGoodStanding(a)
% They're in bad standing.
fprintf('%s\n', Names{a});
end
end
More Answers (0)
See Also
Categories
Find more on Logical 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!