If Else if statement problem
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
Risposta accettata
Anurag Ojha
il 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 Commenti
Dyuman Joshi
il 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Downloads in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!