My Grading program is not printing the answers onto the text file

1 回表示 (過去 30 日間)
Braeden McKeown
Braeden McKeown 2021 年 3 月 10 日
回答済み: Walter Roberson 2023 年 2 月 26 日
I have an assignment in which it is asking this
The file testscores.dat (testscores.txt) has on each line a student ID# and a test score. Write a program to read in the scores and use built-in MATLAB functions to determine the high, low, average (Ave), and the standard deviation (SD) of the scores. For each student, determine the grade assuming that the following table is used:
Print out in a new file (FinalGrades.txt or FinalGrades.dat) the student ID and the corresponding letter grade only. Your program must work for any properly formatted file named testscores.dat/txt. The program will also provide a summary of the scores: Low, High, Average, Standard Deviation, and the number of each letter grade to the command window
However my program that I have set up is not printing out anything onto the finalgrades file.
data=load('testscores.txt','r');
StudentID=data(:,1);
Scores=data(:,2);
SD=std(Scores);
Ave=mean(Scores);
A=0;
B=0;
C=0;
D=0;
F=0;
High=0;
Average=0;
Low=0;
FID=fopen('FinalGrades.txt','w');
if Scores>=Ave+1.5*SD
A=A+1;
High=High+1;
fprintf(FID,'%d A\n',StudentID);
elseif Ave+1.5*SD>Scores>=Ave+0.5*SD
B=B+1;
High=High+1;
fprintf(FID,'%d B\n',StudentID);
elseif Ave+0.5*SD>Scores>=Ave-0.5*SD
C=C+1;
Average=Average+1;
fprintf(FID,'%d C\n',StudentID);
elseif Ave-0.5*SD>Scores>=Ave-1.5*SD
D=D+1;
Low=Low+1;
fprintf(FID,'%d D\n',StudentID);
elseif Ave-1.5*SD>Scores
F=F+1;
Low=Low+1;
fprintf(FID,'%d F\n',StudentID);
end
fprintf('The number of low scores was %d\n',Low);
fprintf('The number of average scores was %d\n',Average);
fprintf('The number of high scores was %d\n',High);
fprintf('The number of As was %d\n',A);
fprintf('The number of Bs was %d\n',B);
fprintf('The number of Cs was %d\n',C);
fprintf('The number of Ds was %d\n',D);
fprintf('The number of Fs was %d\n',F);
fprintf('The Standard Deviation was %0.4f\n',SD);
fclose(FID);
The text file it is to read from is below in a two column set up
100014 88.3
100109 96.9
100205 69.9
100263 90.5
100269 86.9
100293 76.2
100329 81.9
100412 87.1
100414 108.4
100419 103.1
100436 75.9
100501 104.8
100575 89.6
100640 84.4
100686 89.5
100741 83.4
100771 84.0
100846 94.6
100865 94.1
100934 94.2
100953 89.2
100990 76.8
101053 89.5
101132 95.6
101141 88.0
101234 91.6
101312 89.6
101361 82.8
101405 86.7
101450 79.6
101481 90.7
101532 77.2
101584 77.7
101666 79.5
101746 65.4
101811 94.3
101849 86.9
101931 79.8
101985 93.8
102021 73.5
102115 84.1
102203 83.2
102259 86.9
102322 86.9
102381 79.1
102402 84.6
102433 83.7
102481 88.9
102505 92.0
102590 92.1
102610 79.1
102633 85.3
102651 76.8
102674 77.5
102718 84.8
102750 94.9
102843 79.7
102887 87.3
102906 83.3
102997 92.2
103095 77.6
103139 85.0
103151 88.4
103177 92.1
103218 95.0
103278 85.4
103305 75.0
103366 79.9
103438 77.8
103461 100.3
103473 80.7
103503 89.7
103535 83.5
103578 90.7
103629 79.8
103638 75.5
103665 75.4
103746 88.0
103749 83.6
103842 83.5
103916 94.2
103965 86.7
104023 86.1
104047 95.3
104093 79.5
104190 62.7
104245 63.6
104298 56.8
104322 59.7
104371 51.0
104434 51.1
104502 59.0
104542 62.9
104579 74.6
104678 54.1
104682 59.5
104771 57.8
104863 46.1
104943 55.5
104953 47.0
104980 63.6
105014 52.7
105082 58.9
105096 54.9
105169 60.2
105180 54.5
105246 61.4
105296 63.0
105374 69.1
105446 57.1
105537 44.8
105627 53.0
105661 66.8
105731 51.5
105751 64.4
105755 59.1
105830 67.4
105881 45.9
105929 57.1
106020 50.7
106081 76.6
106143 63.5
106229 67.0
106310 51.6
106368 55.3
106387 56.6
106411 65.2
106500 56.5
106503 62.7
106552 45.4
106569 56.1
106667 53.1
106739 48.4
106790 61.5
106838 60.1
106844 58.5
106913 49.9
106918 65.4
106926 60.5
106979 56.4
106989 58.4
107071 56.6
107153 47.3
107226 56.5
107241 53.1
107307 52.1
107359 51.0
107457 54.9
107522 45.7
107603 64.4
107649 61.6
107693 58.2
107776 58.1
107785 53.3
107799 64.7
107817 57.5
107857 53.8
107941 66.8
108022 56.9
108029 54.6
108069 56.4
108122 53.0
Any help would be much appreciated!!!

採用された回答

Star Strider
Star Strider 2021 年 3 月 10 日
There are different ways to do this, however the easiest may be to put it in a loop:
for k = 1:numel(Scores)
if Scores(k)>=Ave+1.5*SD
A=A+1;
High=High+1;
fprintf(FID,'%d A\n',StudentID(k));
... CODE ...
end
I leave the rest to you.
  3 件のコメント
Holden
Holden 2023 年 2 月 26 日
In fact, im not even getting any results for the Number of High scores, Average Scores, Low Scores, or Letter Grades. All ive got is the standard deviation.
Star Strider
Star Strider 2023 年 2 月 26 日
Post this as a new question, and with significantly more information.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 2 月 26 日
if Ave+1.5*SD>Scores>=Ave+0.5*SD
In MATLAB that is parsed as
if all((Ave+1.5*SD>Scores)>=Ave+0.5*SD)
That is, Ave+1.5*SD>Scores is evaluated to a logical array, and the results in the logical array are compared to Ave+0.5*SD . false is converted to numeric 0, true is converted to numeric 1, and those 0 and 1 are compared >= Ave+0.5*SD. Probably the results will all be false. But unless all of the results are non-zero (true) then MATLAB will consider the test to be false.
You should fix your conditions. You might also wish to consider using logical indexing instead of looping.

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by