MATLAB Answers

+ +
0

I'm a noob. Can anyone make this simpler or more efficient?

Asked by + +
on 1 Feb 2018
Latest activity Answered by Sean de Wolski
on 1 Feb 2018
name=input('Enter your name: ', 's');
num_grade=input('Enter your exam grade (out of 100): ');
check=0;
while check==0
if num_grade<=100 & num_grade>=90
    let_grade=('A');
    check=1;
elseif num_grade<=89 & num_grade>=80
    let_grade=('B');
    check=1;
elseif num_grade<=79 & num_grade>=70
    let_grade=('C');
    check=1;
elseif num_grade<=69 & num_grade>=60
    let_grade=('D');
    check=1;
elseif num_grade<60 & num_grade>=0 
    let_grade=('F');
    check=1;
else
    disp('Please enter a grade between 0 and 100');
    num_grade=input('Enter your exam grade (out of 100): ');
end
end
fprintf('%s, your grade is %s.\n',name,let_grade)

  2 Comments

It seems already pretty simple to me, what are the changes you would want?

The only remark I have is that you forgot a '\n' at the end of your formatspec in fprintf (although that may be a conscious decision).

Yeah I was just asking if there were even simpler methods.

but i don't really care about the new line or not lol

Sign in to comment.

3 Answers

Answer by John D'Errico
on 1 Feb 2018
Edited by John D'Errico
on 1 Feb 2018

Um, yes, I could make it "simpler" and perhaps more efficient. Who cares? Don't pre-optimize code without some reason to do so. The code you have written is already reasonably efficient.

Now, COULD I have written it more elegantly, perhaps using a tool like histcounts to bin the grades? The virtue of histcounts is it will work for a vector of student grades. So, yes, I could have done so. For a "noob" the result might not have made a lot of sense, using what appears to be a histogram tool. Remember that code that does something in a way that you can't read to debug later is a bad idea. So until you get to the point where that sort of approach looks easy to you, avoid it.

Finally, COULD I have written your code in a way that is somewhat simpler? Well, yes. Swap the way you are doing things.

if num_grade >= 90
  let_grade=('A');
  check=1;
elseif num_grade >= 80
  let_grade=('B');
  check=1;
elseif num_grade >= 70
  let_grade=('C');
  check=1;
elseif num_grade >= 60
  let_grade=('D');
  check=1;
elseif num_grade>=0
  let_grade=('F');
    check=1;
else
    disp('The sky is falling!!! (said Chicken Little)');
    check=0;
end

The point is, you don't need to test if numgrade is less than 90 to test for a B, since you have already excluded the case where it is greater than 90 in the first branch of the if. So the set of tests that I wrote are simpler and more efficient, since they use only 1 test at each step.

One last point. You should learn about the difference between the & and && operators.

In an if statement where you have two tests A and B,

if A & B

versus

if A && B

what is the difference? The && operator is a short-circuited test. Suppose that A is false? Can false & anything EVER result in true? Look at your truth tables for and.

So if A is false, the if statement never needs to evaluate the second test when using &&. The secondary test if only checked when A is true, thus when it is possible that a true result could happen. That saves some CPU cycles. But in the code as I wrote it, you only ever needed ONE test at a time anyway.

Similarly, || is a short-circuited version of the | operator.

  0 Comments

Sign in to comment.


Answer by Sean de Wolski
on 1 Feb 2018

numgrade = 77;
graderanges = [0 60:10:100];
grades = ['F' 'D':-1:'A'];
let_grade = grades(discretize(numgrade, graderanges))

  0 Comments

Sign in to comment.


Answer by Jan
on 1 Feb 2018
Edited by Jan
on 1 Feb 2018

There is no need for parentheses in

let_grade=('A');

Better:

let_grade = 'A';

Repeated code is a source of bugs, if you edit one line but forget to adjust the other one. Therefore it is a good programming practice to avoid repeating code. Here the input command is concerned.

You could use the contents of let_grade directly instead of using the additional variable check as a flag.

name      = input('Enter your name: ', 's');
let_grade = [];
while isempty(let_grade)
  num_grade = input('Enter your exam grade (out of 100): ');
  if num_grade < 0 || num_grade > 100
    disp('Please enter a grade between 0 and 100');
  elseif num_grade >= 90  % num_grade > 100 was excluded before
    let_grade = 'A';
  elseif num_grade >= 80
    let_grade = 'B';
  elseif num_grade >= 70
    let_grade = 'C';
  elseif num_grade >= 60
    let_grade = 'D';
  else
    let_grade = 'F';
  end
end
fprintf('%s, your grade is %s.\n', name, let_grade);

  0 Comments

Sign in to comment.