CS315 - Midterm #1

advertisement
CS 111: Introduction to Computing in Engineering and Science
(Q1) (4*7.5=30 pts)
Assume that array arr is defined as shown below, and determine the contents of the
following arrays.
2
3
13
35
 16

5
11
10
8
23 
arr  

9
7
6
12
9


4
14
15
1
54 

(i) a = arr(2:end,:);
(ii) b = arr;
b(5,[4 6]) = 17;
(iii) c = arr;
c([1 4],:) = [];
(iv) d = arr(1:2:3,1:2:5) * eye(3,3);
(Q2)(4x7.5=30 pts)
Turn the following formulae into Matlab expressions and find
the results:
(i)
 b  b 2  4ab
2a
for a=1, b=3, c=4
(ii) 2r 2  rh
for r=[2.3 4.5], h=2
(iii) x  y  
for x=3, y=3.0000000000000001
(iv)
cos( x) sin( y )
sin( x)  cos( y )
for x=0:/18:, y=/4-x;
(Q3)(2x20=40 pts)
Express the following problems with Matlab statement(s). Remember that you can only use
 assignment statement,
 input statement, and
 if statement (with or without else; simple or nested)
in your answers.
(i)
Two integer numbers, say a and b, are to simulate the numbers drawn when you
throw two dice. Note that a and b must be between 1 and 6. Your answer must
decide if a and b denote a pair or not by assigning a proper value to a boolean
variable, say pair. For instance, if a is 3 and b is 3, then pair must be set to true (1).
If a is 2 and b is 3, then pair must be set to false (0). If a is 1 and b is 7, then
nothing will be set to pair!
1/2
(ii)
Given (already input) three positive floating point numbers, say a, b and c, decide
the type of the triangle formed by sides a, b and c by assigning the correct value to
an integer variable, say type by using the table below:
Condition
Two sides are equal
All sides are equal
All sides are different
Value of type
2
3
1
2/2
Meaning
Isosceles triangle
Equilateral triangle
Just a triangle
Download