1 if statements

Begin by creating a script named q1.m and copy the following Matlab code into the script:

% q1.m 

% (a) Answer part (a) below this comment



% (b) Answer part (b) below this comment



% (c) Answer part (c) below this comment 


(a) [5 marks]

An integer value is even if it is evenly divisible by 2.

Assume that you have an integer value stored in a variable named x (you may create such a variable but you are not required to do so). Write an if or if-else statement that sets the value of a variable named isEven to 1 if the value of x is even and sets isEven to 0 if the value of x is odd.

(b) [5 marks]

As a quality control engineer at a food packaging plant you need to program your packaging equipment to accept packages that weigh between 745 grams and 755 grams (packages weighing exactly 745 or 755 grams are also accepted).

Assume that the weight of the package is stored in a variable named wt (you may create such a variable but you are not required to do so). Write an if or if-else statement that sets the value of a variable named accepted to 1 if the package should be accepted and sets accepted to 0 if the package should not be accepted.

(c) [5 marks]

Consider the problem described in part (b).

When rejecting a package it is useful to know why the package was rejected. From part (b) you know that packages are rejected if their weight is too low or if their weight is too high.

Assume that the weight of the package is stored in a variable named wt (you may create such a variable but you are not required to do so). Write an if-elseif statement that sets the value of a variable named reason to:

  • -1 if the weight of the package is too low
  • 0 if the weight of the package is acceptable
  • 1 if the weight of the package is too high