Question

Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that semester Enter yo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for a:

%Here the grades are read from the user,
% which are separated by spaces,
% the second parameter 's' is used to avoid text evalulation and read the input as it is

grade_str = input('Enter your grades(letters):','s');
% This statement here splits the above string where whitespace is the delimiter,
%and converts it into cell array containing characters

grades = strsplit(grade_str);

%gpa variable which is used to store the average gpa
gpa = 0.0;

% Iterating over the cell array
for k=1:length(grades)
% If the character at grades(1,k) is 'A'
if char(grades(1,k))=='A'
gpa = gpa + 4.0
% Else If the character at grades(1,k) is 'B'
elseif char(grades(1,k))=='B'
gpa = gpa + 3.0
% Else If the character at grades(1,k) is 'C'
elseif char(grades(1,k))=='C'
gpa = gpa + 2.0
% Else If the character at grades(1,k) is 'D'
elseif char(grades(1,k))=='D'
gpa = gpa + 1.0
% Else If the character at grades(1,k) is 'F'
elseif char(grades(1,k))=='F'
gpa = gpa + 0.0
% If it is none of these
else
gpa = gpa + 0.0
end

end

% Calculating the average gpa
gpa = gpa / length(grades)

% Printing the average GPA
disp(sprintf('Your GPA is %.2f', gpa))

OUTPUT:

Enter your grades (letters) : gpa = 4 gpa8 gpa= 11 gpa16 = 3.2000 gpa Your GPA is 3.20

Code for b:

function num_grade = Letter2Grade(letter_grade)

% This function converts a letter grade into a numeric grade.
% If the entered grade is invalid, it returns Invalid grade.

% If the letter_grade is 'A'
if letter_grade=='A'
   num_grade = 4
% If the letter_grade is 'B'
elseif letter_grade=='B'
   num_grade = 3
% If the letter_grade is 'C'
elseif letter_grade=='C'
num_grade = 2
% If the letter_grade is 'D'
elseif letter_grade=='D'
   num_grade = 1
% If the letter_grade is 'F'
elseif letter_grade=='F'
   num_grade = 0
% If it is none of these
else
error('Invalid Grade')
end
end

% Testing the function

Letter2Grade('A')
Letter2Grade('B')
Letter2Grade('C')
Letter2Grade('D')
Letter2Grade('F')
Letter2Grade('Q')

OUTPUT:

octave : 2> function num-grade Letter2G rade(letter-grade) = % This function converts a letter grade into a numeric grade % I

Add a comment
Know the answer?
Add Answer to:
Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that seme...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • b) (2.5 marks) Write a Matlab user defined function that takes the letter grades (i.e. AABBC) as input and returns...

    b) (2.5 marks) Write a Matlab user defined function that takes the letter grades (i.e. AABBC) as input and returns if the student entitled to the honor list or not. The student is considered in the honor s f he or she satisfies the following three conditions: a. An average GPA of 3.5 and above and b. At least two classes with A mark and e. no class with a mark less than C (i.e. no D or F) The...

  • Write a C++ program that will provide a user with a grade range if they enter...

    Write a C++ program that will provide a user with a grade range if they enter a letter grade. Your program should contain one function. Your function will accept one argument of type char and will not return anything (the return type will be void), but rather print statements to the console. Your main function will contain code to prompt the user to enter a letter grade. It will pass the letter grade entered by the user to your function....

  • MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print...

    MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print its equivalence in numerical scale. For example if the user enters A, then your function should print a message stating that the grade must be between 90 and 100. B is between 80 and 89, etc. Everything under 60 is F. Use the switch-case function. Use fprintf to display the results on the screen.

  • In Matlab. Use loops. 2. Create an algorithm to allow the user to enter a list...

    In Matlab. Use loops. 2. Create an algorithm to allow the user to enter a list of courses they have taken along with the grades received and the credit hours for each course, and then calculate the user's GPA. The program should first ask the user to type either Yes or No in response to the question, "Do you want to enter a grade?" If the answer is Yes, the user should be asked to enter the name of the...

  • Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...

    Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be...

  • Part I Create a Java program that inputs a grade from the user. The grade input...

    Part I Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled...

  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • This code should be written in php. Write a program that allows the user to input...

    This code should be written in php. Write a program that allows the user to input a student's last name, along with that student's grades in English, History, Math, Science, and Geography. When the user submits this information, store it in an associative array, like this: Smith=61 67 75 80 72 where the key is the student's last name, and the grades are combined into a single space-delimited string. Return the user back to the data entry screen, to allow...

  • write a c++ code be sure the user only enter A or a for packageA, B...

    write a c++ code be sure the user only enter A or a for packageA, B or b for packageB, C or c for packageC. If the user inputs some other value display an error message saying " invalid out". When you ask the user to input the number of minute and the user enters a negative value you should display the same error message "invalid input" in both cases, after you display the error message the program should stop...

  • Write python code on computer, No handwritten code You will implement a program which asks the...

    Write python code on computer, No handwritten code You will implement a program which asks the user to enter scores of different courses in different semesters. The program should read the input and output some simple statistics 1. An example input string by the user is given below. Fal12016-coursel:82, course2:80,course3:85;Spring2018- course4:82;Fall2018-course5:85, course6:50, course7:78 Info from different semesters are separated by a ";", courses in each semester are separated by a,and score and corresponding course is separated by a, information of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT