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
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 个评论
Maya
Maya 2024-4-29
That’s great Thank you so much
Do you know how to solve this problem without if or for
Dyuman Joshi
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 CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by