If Else if statement problem
조회 수: 4 (최근 30일)
이전 댓글 표시
Write a program that takes the grades of several students as a vector and Do the following(the grade should be between 0to 20): Use the “for”and conditional commands end-else-if to check each grade and change them as follows: Change scores less than 5 to 9 Change scores between 5 and 8 to 9.5. Change scores between 8 and 10 to 10. Increase scores between 10 and 15 by 1 score To increase scores more than 15 and less than 20 by 0.5 points.
채택된 답변
Anurag Ojha
2024년 4월 29일
Hello Maya
Kindly go through following code
% Define the vector of grades
grades = [4, 7, 9, 12, 16, 19];
% Iterate over each grade in the vector
for i = 1:length(grades)
% Check if the grade is less than 5
if grades(i) < 5
grades(i) = 9; % Change the grade to 9
% Check if the grade is between 5 and 8 (inclusive)
elseif grades(i) >= 5 && grades(i) <= 8
grades(i) = 9.5; % Change the grade to 9.5
% Check if the grade is between 8 and 10 (inclusive)
elseif grades(i) > 8 && grades(i) <= 10
grades(i) = 10; % Change the grade to 10
% Check if the grade is between 10 and 15 (inclusive)
elseif grades(i) > 10 && grades(i) <= 15
grades(i) = grades(i) + 1; % Increase the grade by 1
% Check if the grade is between 15 and 20 (exclusive)
elseif grades(i) > 15 && grades(i) < 20
grades(i) = grades(i) + 0.5; % Increase the grade by 0.5
end
end
% Display the modified grades
disp(grades);
댓글 수: 2
Dyuman Joshi
2024년 4월 29일
@Anurag, Please don't do obvious homework questions on Answers, when no effort has been shown. This does not help the student, who learns nothing more than to post all of their homwork problems on the site, hoping they will find someone willing to do their thinking and work for them. Worse that that, it convinces the next student who comes along to do the same.
If you want to help, then find a way to push the student in the right direction. But posting a complete solution does far more harm than good.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Downloads에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!