{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Quiz2_Solution

# Quiz2_Solution - clever comes about by considering the...

This preview shows pages 1–2. Sign up to view the full content.

Quiz 2: Simple Programs Name: ASE 201 Write a MATLAB script (BE SURE TO USE MATLAB NOTATION!) that 1. Reads-in two variables a and b from the user. Assume the user will never enter zero as a value. 2. Adds the two numbers if they have the same sign (both positive or both negative). 3. Subtracts b from a if they have diﬀerent signs (one is positive and one is negative) 4. Displays the result including the operation performed (+ or -). You may use disp() or fprintf(). The output of the program should look something like this if you were to run it on MATLAB: >> quiz2 Enter value for a: 1 Enter value for b: 2 (1) + (2) = 3 >> quiz2 Enter value for a: 3 Enter value for b: -2 (3) - (-2) = 5 Hint: There are several correct ways to check for the conditions. The simplest and most

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: clever comes about by considering the product of a and b . Solution: %quiz2.m clear a = input(’Enter value for a: ’); b = input(’Enter value for b: ’); if (a*b) > 0 result = a+b; fprintf(’\n (%g) + (%g) = %g \n’,a,b,result) end if (a*b) < 0 result = a-b; fprintf(’\n (%g) - (%g) = %g \n’,a,b,result) end Or if you did not get the hint could also do if (a > 0 && b > 0) || (a < 0 && b < 0) result = a+b; fprintf(’\n (%g) + (%g) = %g \n’,a,b,result) end if (a < 0 && b > 0) || (a > 0 && b < 0) result = a-b; fprintf(’\n (%g) - (%g) = %g \n’,a,b,result) end...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

Quiz2_Solution - clever comes about by considering the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online