how to Write a code that read student marks and assign grade to it. By reading the grades from a txt file.

9 ビュー (過去 30 日間)
Please can you help me i already upload the file in MATLAB , but a want to learn the code
  2 件のコメント
Anas Rozy
Anas Rozy 2020 年 11 月 6 日
I did it by While loop, But I need to use if or for statement

サインインしてコメントする。

回答 (1 件)

Pratyush Swain
Pratyush Swain 2022 年 6 月 29 日
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.

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by