Filter löschen
Filter löschen

If Else if statement problem

1 Ansicht (letzte 30 Tage)
Maya
Maya am 29 Apr. 2024
Kommentiert: Dyuman Joshi am 29 Apr. 2024
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.
  1 Kommentar
Maya
Maya am 29 Apr. 2024
Can someone help me to solve this problem🙏🙏

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Anurag Ojha
Anurag Ojha am 29 Apr. 2024
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 Kommentare
Maya
Maya am 29 Apr. 2024
That’s great Thank you so much
Do you know how to solve this problem without if or for
Dyuman Joshi
Dyuman Joshi am 29 Apr. 2024
@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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by