for-loop from an if-statement

4 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 3 月 21 日
コメント済み: Spaceman 2024 年 3 月 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 件のコメント
Stephen23
Stephen23 2024 年 3 月 22 日
編集済み: Stephen23 2024 年 3 月 22 日
There are many ways to approach this:
grade = 67;
C = {'You fail, F.', 'Got a D', 'You got a C.', 'You got a B!', 'You got an A!!!'};
N = discretize(grade,[0,60,70,80,90,100]);
C{N}
ans = 'Got a D'
Spaceman
Spaceman 2024 年 3 月 30 日
Genius. This is a beautiful way to do this as well. However, I have never dealt with the discretize function before.

サインインしてコメントする。

採用された回答

Dinesh
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 件のコメント
Spaceman
Spaceman 2024 年 3 月 21 日
Eureka! This is beautiful. Thank you so much. I have been up until 5am every night racking my brain with these practice problems. I am hoping eventually it will get easier and I will intuitively just know what to do based on what the prompt is asking. :3

サインインしてコメントする。

その他の回答 (1 件)

Hassaan
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.
  1 件のコメント
Spaceman
Spaceman 2024 年 3 月 22 日
Genius. This is such a beautiful answer as well. I liked the displaying of ALL the results at the end in a digestible manner. How would the loop need to be restructured to include the line of code:
i=i-1
such that the user would have to re-enter a correct input if they had accidently typed the wrong number and hit enter initially?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by