for-loop from an if-statement
4 ビュー (過去 30 日間)
古いコメントを表示
Given: Use previous code to make a for-loop
Find: Prompt the user to enter the number of grades (N) that they are trying to classify; use a for-loop to allow the user to enter and classify a total of N grades, (I will need a loop variable and will need to provide it with row vector of values, such that the loop will run N times); when run your code should ask the user (N times) to enter a percentage; after entering each grade, the letter grade should be displayed to the user. After getting this to run I will have to find a way to save each percentage in a vector and each letter grade returned in a string array, (strings function).
Issue: How do I turn my code into a for loop while keeping all architecture of my if-statement intact? I have a flowchart but I am under the impression I wont be able to use if, elseif, or else in the for-loop.
My solution: I have this code from the if-statement function I made:
grade=input('Enter a grade between 0 & 100: ');
if grade<=100 && grade>90
disp('You got an A!!!')
elseif grade<90 && grade>=80
disp('You got a B!')
elseif grade<80 && grade>=70
disp('You got a C.')
elseif grade<70 && grade>=60
disp('Got a D')
elseif grade<60 && grade>=0
disp('You fail, F.')
else
disp('You entered an invalid percentage, try again')
end
2 件のコメント
採用された回答
Dinesh
2024 年 3 月 21 日
Hello,
You can put all the if, else-if and else conditions within the for loop itself.
The following code runs for "N" times, once for each grade prompting the user to enter the grade in each iteration of the loop. The letter grades and percentages are all stored in vectors.
N = input('Enter the number of grades you are trying to classify: ');
grades = zeros(1, N);
letterGrades = strings(1, N);
for i = 1:N
grade = input('Enter a grade between 0 & 100: ');
grades(i) = grade;
if grade <= 100 && grade > 90
disp('You got an A!!!')
letterGrades(i) = "A";
elseif grade < 90 && grade >= 80
disp('You got a B!')
letterGrades(i) = "B";
elseif grade < 80 && grade >= 70
disp('You got a C.')
letterGrades(i) = "C";
elseif grade < 70 && grade >= 60
disp('Got a D')
letterGrades(i) = "D";
elseif grade < 60 && grade >= 0
disp('You fail, F.')
letterGrades(i) = "F";
else
disp('You entered an invalid percentage, try again')
letterGrades(i) = "Invalid";
end
end
% Here, grades and letterGrades have the percentages and letter grades for
% all the entered N grades
If this is not what you are looking for, please let me know and I will look into this further.
その他の回答 (1 件)
Hassaan
2024 年 3 月 21 日
編集済み: Hassaan
2024 年 3 月 21 日
% Prompt the user to enter the number of grades
N = input('Enter the number of grades you want to classify: ');
% Initialize a vector to save the percentages
percentages = zeros(1, N);
% Initialize a string array to save the letter grades
letterGrades = strings(1, N);
% Use a for-loop to prompt for grades and classify them
for i = 1:N
grade = input('Enter a grade between <0 & 100>: ');
percentages(i) = grade; % Save the percentage
if grade <= 100 && grade > 90
disp('You got an A!!!');
letterGrades(i) = "A";
elseif grade <= 90 && grade > 80
disp('You got a B!');
letterGrades(i) = "B";
elseif grade <= 80 && grade > 70
disp('You got a C.');
letterGrades(i) = "C";
elseif grade <= 70 && grade > 60
disp('Got a D');
letterGrades(i) = "D";
elseif grade <= 60 && grade >= 0
disp('You fail, F.');
letterGrades(i) = "F";
else
disp('You entered an invalid percentage, please try again');
letterGrades(i) = "Invalid";
percentages(i) = NaN; % Indicate invalid entry
% Optional: Decrement i to retry entering a valid grade
% i = i - 1; % Uncommenting this line may require changing loop structure
end
end
% Display the percentages and their corresponding letter grades
disp('Percentages:');
disp(percentages);
disp('Letter Grades:');
disp(letterGrades);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
参考
カテゴリ
Help Center および File Exchange で Downloads についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!