Name: ________________________________________________________________
UD Email: _____________________________________________@ udel.edu
Circle your section number:
010 011 012 013
014 015 016 017
Do not write your name on any page except this one.
Answer all questions on this paper directly (including the multiple choice)
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) Suppose you have the following M file in your current working directory.
function result = mysteryFunction(a,b)Then, suppose you type the following at the MATLAB prompt. What is the output?
%mysteryFunction does something mysterious
%Examples:
% ... (omitted to make you think harder)
%D. Duck for CISC106 sect 99, 11/10/2006
if (a < b)
result = a:b;
else
result = b:a;
return;
end
>> mysteryFunction(6,3)
ans =
6:3
>>
ans = 3:6
>>
>>
prompt
ans = 3 4 5 6
>>
ans = 6 5 4 3
>>
(4 pts) Suppose you have the following M file in your current working directory.
function result = yetAnotherFunction(a,b) %yetAnotherFunction does something with a and b %Examples: % ... (omitted to make you think harder) %D. Duck for CISC106 sect 99, 11/10/2006 if (a > b) result = [ a b ]; else result = [ b a ]; end return; endThen, suppose you type the following at the MATLAB prompt. What is the output?
>> yetAnotherFunction(2,4)
ans =
8
>>
ans = 24
>>
>>
prompt
ans = 2 4
>>
ans = 4 2
>>
(4 pts) Suppose you have the following M file in your current working directory.
function result = stillAnotherFunction(v) %stillAnotherFunction does something with a vector v %Examples: % ... (omitted to make you think harder) %D. Duck for CISC106 sect 99, 11/10/2006 result = 0; for k=1:length(v) result = result + ( v(k)^2 ); end return end
>> stillAnotherFunction([3 4]);
ans =
3 4
>>
ans = 7
>>
>>
prompt
ans = 9 16
>>
ans = 25
>>
>> stillAnotherFunction([6 3])
ans =
6 3
>>
ans = 9
>>
>>
prompt
ans = 36 9
>>
ans = 45
>>
lab08.cc
? lab08.cc
diameter = input('What is the diameter?');
fprintf('result = %d\n',result);
For each of the questions in this section, you are given
In each problem, there is one or more piece missing that you have to fill in. Fill in the missing piece.
In the file countNegativeNumbers.m
, fill in
if
statement in the body of the functionIn the file testCountNegativeNumbers.m
, fill in
if
tests (i.e. the conditions). Use the examples given in the M file as the test cases. function result = countNegativeNumbers(vec) %countNegativeNumbers counts how many even numbers are in a vector % %consumes: vec, vector of numbers %produces: result, a scalar number % %Examples: % >> countNegativeNumbers([1 -3 0 -5 6]) % ans = 2 % >> countNegativeNumbers(-3:1:3) % ans = 3 % >> countNegativeNumbers([1 3 5]) % ans = 0 count = 0; for k=1:length(vec) if ( vec(k) < 0 ) % <== fill in something here count = count + 1; end % end of if end % end of for result = count; % <== fill in something here on % right hand side end
% testCountNegativeNumbers.m % regression/acceptance test for countNegativeNumbers.m % S. J. Cat, CISC106, sect 99, 11/06/2006 fprintf('Test 1: '); if ( ) % <== fill in something here fprintf('passed\n'); else fprintf('failed\n'); end fprintf('Test 2: '); if ( ) % <== fill in something here fprintf('passed\n'); else fprintf('failed\n'); end fprintf('Test 3: '); if ( ) % <== fill in something here fprintf('passed\n'); else fprintf('failed\n'); end
>> testCountNegativeNumbers Test 1: passed Test 2: passed Test 3: passed >>
In the file slope.m
, fill in
In the file testSlope.m
, fill in
% <== fill in %slope return slope of line passing through two points % %consumes: x1, y1, x2, y2: scalar numbers %produces: theSlope, a scalar number % %Examples: % >> slope(2,2,4,4) % ans = 1 % >> slope(3,1,5,5) % ans = 2 % >> slope(1,4,1,5) % Error: slope is undefined % >> denom = x1 - x2; num = y1 - y2; if ( ) % <== fill in this line of code error('slope is undefined'); else theSlope = num/denom; return; end
% testSlope.m Peggy Hill for CISC106, sect 99, Arlen Comm. College % regression/acceptance testing for slope.m fprintf('Test 1: '); if (slope(2,2,4,4)==1) fprintf('passed\n'); else fprintf('failed\n'); end fprintf('Test 2: '); if (slope(3,1,5,5)==2) fprintf('passed\n'); else fprintf('failed\n'); end fprintf('Test 3: '); try x = slope(1,4,1,5); fprintf( ); % <== fill in some code here catch fprintf( ); % <== fill in some code here end
>> testSlope Test 1: passed Test 2: passed Test 3: passed >>
In the space below, write a complete M-file to define a function that computes the product of all the elements of a one-dimensional vector of numbers.
Important notes (read these carefully!)
product.m
. >> product([3 4 5]) ans = 60 >> product([-1 -5 -0.5 -1]) ans = 2.5000 >>
MATLAB | C++ | |
---|---|---|
comments | % comment |
// comment |
string literals | 'delimited with single quotes' |
"delimited with double quotes" |
variables | don't have to be declared before they are used |
have to be declared before they are used (e.g. int x; )choices: double , float , int |
input | price = input('please input the price: '); |
double price; |
output |
fprintf('The price is %d\n',price); or disp(['The price is ' num2str(price)]);
|
cout << "The price is " << price << endl; |
if/else statements |
|
|
function definition |
start with the word |
The word |