Name: ________________________________________________________________
UD Email: _____________________________________________@ udel.edu
Are you classified as a freshman (circle one): YES NO
Circle your section number:
010 011 012 013
014 015 016 017
Do not write your name on any page except this one.
Answer the multiple choice questions on the scantron sheet.
Answer the remaining questions on this paper directly.
Total Points: ???
A hint about allocating your time:
You need to answer 100 points worth of questions in 50 minutes, so....
Each of the problems in this section gives you some input that is typed into MATLAB inside the box with the heavy border.
Which of the boxes afterwards with the light border represents the output you will get back?
Consider each problem as a new problem—that is, suppose that you do a clear
command in between each problem.
Feel free to use the blank space on the exam sheet for scratch work.
(4 pts) What is the output?
>> x = 1:4
x = 1 2 3 4 >>
x = 1 2 3 4 >>
x = 4 3 2 1 >>
>>
prompt
x = 1 4 >>
x =
part, and the new MATLAB prompt (>>
).
>> x = 1:2:7; >> x'
1 2 7
1 2 7
1 3 5 7
>>
prompt1 3 5 7
(4 pts) What is the output?
>> x = 5:1
5 4 3 2 1
5 4 3 2 1
5 1
5
(4 pts) What is the output?
>> x = [ 1 2 3; 4 5 6]
1 2 3
4 5 6
1 2 3 4 5 6
29
1 4
2 5 3 6
(4 pts) What is the output?
>> x = [1 2 3];
>> x + 1
1 2 3 1
1
2
3
1
7
2 3 4
(4 pts) What is the output?
>> x = 5:-1:1;
5 4 3 2 1
5 4 3 2 1
5 -1 1
5
(4 pts) What is the output?
>> x = [1 2 3]; >> y = [4 5 6]';
>> x * y
32
4 10 18
4
10
18
1 2 3
4
5
6
(4 pts) What is the output?
>> x = [1 2; 3 4]; >> y = [1 0; 0 1]; >> x * y
1 0 0 4
1 2
3 4
1 0 0 4
1
0
0
4
>> x = [1 2; 3 4]; >> y = [1 0; 0 1]; >> x .* y
1 0 0 4
1 2
3 4
1 0 0 4
1
0
0
4
(4 pts) What is the output?
>> x = 5:-1:1
5 4 3 2 1
5 4 3 2 1
5 1
For each of the questions in this section, you are given the text on a M-file named foo.m. Indicate in the blank box what the output of that M-file would be. In each case, the MATLAB prompt and the
command foo
to run the M-file is shown—you put in both the output, and show where the MATLAB prompt would appear.
% foo.m Donald Duck, 10/03/06 for x=1:5; fprintf('o\n'); end;
>> foo
% foo.m Donald Duck, 10/03/06 for x=1:3:5; fprintf('+'); end; fprintf('\n');
>> foo
% foo.m Donald Duck, 10/03/06 for k=0:2:5; fprintf('k is %d\n',k); end;
>> foo
% nums.dat 3 14 5 9 4 4 9 6
% foo.m Donald Duck, 10/03/06 load nums.dat; a = nums(:,1); b = nums(:,2); for k=1:length(a); if ( a(k) < b(k) ) fprintf('w'); elseif ( a(k) == b(k) ) fprintf('d'); else fprintf('l'); end end; fprintf('\n');
>> foo
01010000 01001001 01001110 01000101
A 65 J 74 S 83 B 66 K 75 T 84 C 67 L 76 U 85 D 68 M 77 V 86 E 69 N 78 W 87 F 70 O 79 X 88 G 71 P 80 Y 89 H 72 Q 81 Z 90 I 73 R 82
In the space below, write a complete M-file to solve the following problem.
The Pizza Hoot sells two kinds of pizza: round pies, and rectangular pies. A user wants to know which one is the better value, so he wants to calculate the "price per square inch" of a round pizza.
Write an M-file that prompts the user to enter the diameter of a pizza in inches, and the price, and then outputs the price per square inch.
Keep in mind the following:
pi
, which represents π in MATLAB