how to Write a code that read student marks and assign grade to it. By reading the grades from a txt file.
2 views (last 30 days)
Show older comments
Please can you help me i already upload the file in MATLAB , but a want to learn the code
Answers (1)
Pratyush Swain
on 29 Jun 2022
Hi,
From my understanding,you want to read the grades and range of marks it corresponds to from a txt file and use this information to specify grades to the marks entered by the user.
To simulate this scenario I have taken a txt file which contains grades and thier ranges,Ex-(1 91 100,2 81 90,...)
I have assigned the highest grade as 1(for simplicity) ranging from 91-100,and similarly other grades follow.
You can refer to the following implementation:
%display content in grades file%
type grades.txt
%file handling to store the grades and their range%
fileID = fopen('grades.txt','r');
sizeA=[3 7];
A = fscanf(fileID,'%d %d %d',sizeA);
A=A';
ch='y';
%continue operations until user specifies%
while strcmp(ch,'y')==1
%taking input of marks%
prompt='Enter your mark';
x=input(prompt);
%exit if marks lie in invalid range%
if x>100 | x<0
fprintf("invalid marks-limit range btw 0-100...terminating");
break;
end
%find grade corresponding to entered mark%
grade=find_grade(A,x);
fprintf('your grade is %d \n',grade);
%ask if user wants to continue%
prompt='Do you want to continue (y/n)';
ch=input(prompt,'s');
end
%function to map marks to grades%
function g=find_grade(A,x)
[nr,~]=size(A);
for i=1:nr
if x>=A(i,2) && x<=A(i,3)
g=A(i,1);
return;
end
end
end
Hope this helps.
0 Comments
See Also
Categories
Find more on Language Fundamentals 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!