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 might try to answer 100 points worth of questions in 100 minutes, leaving you 20 minutes to review your answers or work on any questions you skipped the first thru.
So....
!
#
@
>>
%
!
#
/*
//
%
(4 pts) Which of the following is true about the Unix command:
CC -c lab09.cc
lab09.cc
, and produces a file called a.out
lab09.cc
, and produces a file called lab09.cc~
lab09.cc
, and produces a file called lab09.o
lab09.cc
, and produces a file called lab09.exe
lab09.cc
, and produces a file called lab09.exe
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?
>> dataFile = 'project1.dat'; >> strrep(dataFile,'.dat','.jpg')
ans = project1.jpg >>
ans = dataFile.jpg >>
ans = dataFile.dat.jpg >>
ans = project1.dat.jpg >>
>>
prompt
(4 pts) What is the output?
>> dataFile = 'project1.dat';
>> [ dataFile '.jpg' ]
ans = project1.jpg >>
ans = dataFile.jpg >>
ans = dataFile.dat.jpg >>
ans = project1.dat.jpg >>
>>
prompt
The multiple choice questions in this section deal with the following problem.
You work for an engineering firm doing a study of three alternative routes for a new highway. The following table shows information about these three routes. The costs are in millions of dollars.
Each route has a cost associated with construction. Because each route will involve some impact on wetlands, there is also a cost associated with mitigating the environmental impact by constructing artificial wetlands nearby.
Code Name | Construction Cost | Environmental Mitigation Cost |
---|---|---|
red | 127.5 | 150.0 |
blue | 200.0 | 25.0 |
green | 250.0 | 0.0 |
yellow | 210.0 | 12.5 |
constructionCost = [ 127.5 200.0 250.0 210.0]; mitigationCost = [150.0 25.0 0.0 12.5];
Which of the following would be correct MATLAB code for create a vector to hold all of the code names for the project?
codeName = [ 'red' 'blue' 'green' 'yellow' ];
codeName = [ "red" "blue" "green" "yellow" ];
codeName{1} = "red"; codeName{2} = "blue"; codeName{3} = "green"; codeName{4} = "yellow";
codeName(1) = 'red'; codeName(2) = 'blue'; codeName(3) = 'green'; codeName(4) = 'yellow';
codeName{1} = 'red'; codeName{2} = 'blue'; codeName{3} = 'green'; codeName{4} = 'yellow';
bold
) then uses a MATLAB vector operation to add the two cost vectors together, and gives a new vector with the total cost of each project:
constructionCost = [ 127.5 200.0 250.0 210.0]; mitigationCost = [150.0 25.0 0.0 12.5]; totalCost = constructionCost + mitigationCost;
Which of the following sequences of MATLAB code is equivalent to the third line (the one in bold?)
for k=1:length(totalCost)
totalCost(k) = constructionCost(k) + mitigationCost(k); end; % for loop
for k=1:length(constructionCost)
totalCost(k) = constructionCost + mitigationCost; end; % for loop
for k=1:length(mitigationCost)
totalCost(k) = constructionCost(k) + mitigationCost(k); end; % for loop
for k=1:length(totalCost)
totalCost = constructionCost(k) + mitigationCost(k); end; % for loop
codeName
constructionCost = [ 127.5 200.0 250.0 210.0]; mitigationCost = [150.0 25.0 0.0 12.5]; totalCost = constructionCost + mitigationCost;
Which of the following answers would be correct for the "third part", so that the final script prints a message indicating which project has the lowest cost, e.g. The project with the lowest cost is yellow
?
minIndex = 1; for k=2:length(totalCost) if ( totalCost(k) < totalCost(minIndex) ) minIndex = k; end % if end %for fprintf('The project with the lowest cost is %s\n', ... codeName{minIndex});
minIndex = v(1); for k=2:length(v) if ( v(k) < v(minIndex) ) minIndex = k; end % if end %for fprintf('The project with the lowest cost is %s\n', ... codeName(k));
min= totalCost(1); for k=2:length(totalCost) if ( min < totalCost(minIndex) ) min = totalCost(k); end % if end %for fprintf('The project with the lowest cost is %s\n', ... codeName(min));
min = 0; for k=1:length(totalCost) if ( totalCost(k) < min ) min = k; end % if end %for fprintf('The project with the lowest cost is %s\n', ... codeName(min));
minIndex = 1; for k=1:length(totalCost) if ( totalCost(k) < totalCost(minIndex) ) minIndex = totalCost(k); end % if end %for fprintf('The project with the lowest cost is %s\n', ... codeName{minIndex});
% roadProject.m Compute road project with lowest total cost
% P. Conrad, 12/09/2006 for VeryFineEngineering, LLC.
function result = roadProject(v) %roadProject Compute road project with lowest total cost
function result = roadProject(v) %P. Conrad, 12/09/2006 for VeryFineEngineering, LLC.
Below you will find code listings for four M-files, each of which defines a version of the factorial function. Among these four are:
Your job is to identify which one is which. You will use each answer exactly once.
fact1
uses recursion, and is correctfact1
uses recursion, but is WRONG fact1
does NOT use recursion and is correctfact1
does NOT use recursion, and is WRONG 1 | function result = fact1(n) |
2 | %fact1 first version of factorial function |
3 | % |
4 | % Consumes: n, a scalar number |
5 | % Produces: result, a scalar number |
6 | % Examples: |
7 | % >> fact1(4) |
8 | % ans = 24 |
9 | % >> fact1(0) |
10 | % ans = 1 |
11 | % >> fact1(-1) |
12 | % Error: factorial of negatives is undefined |
13 | % >> |
14 | |
15 | if (n<0) |
16 | error('factorial of negatives is undefined'); |
17 | end; |
18 | |
19 | result = 1; |
20 | if (n<=1) |
21 | return; |
22 | end %if |
23 | |
24 | result = fact1(n-1) * n; |
25 | return |
26 | |
27 | end |
fact2
uses recursion, and is correctfact2
uses recursion, but is WRONG fact2
does NOT use recursion and is correctfact2
does NOT use recursion, and is WRONG 1 | function result = fact2(n) |
2 | %fact2 second version of factorial function |
3 | % |
4 | % Consumes: n, a scalar number |
5 | % Produces: result, a scalar number |
6 | % Examples: |
7 | % >> fact2(4) |
8 | % ans = 24 |
9 | % >> fact2(0) |
10 | % ans = 1 |
11 | % >> fact2(-1) |
12 | % Error: factorial of negatives is undefined |
13 | % >> |
14 | |
15 | if (n<0) |
16 | error('factorial of negatives is undefined'); |
17 | end; |
18 | |
19 | result = 1; |
20 | for k=1:n |
21 | result = result * n; |
22 | end % for |
23 | |
24 | return |
25 | |
26 | end |
fact3
uses recursion, and is correctfact3
uses recursion, but is WRONG fact3
does NOT use recursion and is correctfact3
does NOT use recursion, and is WRONG 1 | function result = fact3(n) |
2 | %fact3 third version of factorial function |
3 | % |
4 | % Consumes: n, a scalar number |
5 | % Produces: result, a scalar number |
6 | % Examples: |
7 | % >> fact3(4) |
8 | % ans = 24 |
9 | % >> fact3(0) |
10 | % ans = 1 |
11 | % >> fact3(-1) |
12 | % Error: factorial of negatives is undefined |
13 | % >> |
14 | |
15 | if (n<0) |
16 | error('factorial of negatives is undefined'); |
17 | end; |
18 | |
19 | result = 1; |
20 | for k=1:n |
21 | result = result * k; |
22 | end % for |
23 | |
24 | return |
25 | |
26 | end |
fact4
uses recursion, and is correctfact4
uses recursion, but is WRONG fact4
does NOT use recursion and is correctfact4
does NOT use recursion, and is WRONG 1 | function result = fact4(n) |
2 | %fact4 first version of factorial function |
3 | % |
4 | % Consumes: n, a scalar number |
5 | % Produces: result, a scalar number |
6 | % Examples: |
7 | % >> fact4(4) |
8 | % ans = 24 |
9 | % >> fact4(0) |
10 | % ans = 1 |
11 | % >> fact4(-1) |
12 | % Error: factorial of negatives is undefined |
13 | % >> |
14 | |
15 | if (n<0) |
16 | error('factorial of negatives is undefined'); |
17 | end; |
18 | |
19 | result = 0; |
20 | if (n<=1) |
21 | return; |
22 | end % if; |
23 | |
24 | result = fact4(n-1) * n; |
25 | return |
26 | |
27 | end |
1 | % testFact1.m Run regression/acceptance tests |
2 | % for fact1.m |
3 |
|
4 | fprintf('Test 1...'); |
5 | if (fact1(4)==24) |
6 | fprintf('passed\n'); |
7 | else |
8 | fprintf('failed\n'); |
9 | end; |
10 |
|
11 | fprintf('Test 2...'); |
12 | if (fact1(0)==1) |
13 | fprintf('passed\n'); |
14 | else |
15 | fprintf('failed\n'); |
16 | end; |
17 |
|
18 | fprintf('Test 3...'); |
19 | if (fact1(3)==6) |
20 | fprintf('passed\n'); |
21 | else |
22 | fprintf('failed\n'); |
23 | end; |
24 |
|
25 | %** (6 pts) Fill in the missing line of code below |
26 | %** so that Test 4 determines whether the |
27 | %** function correctly handles the error |
28 | %** condition |
29 |
|
30 | fprintf('Test 4...'); |
31 | try |
32 | |
33 | fprintf('failed\n'); |
34 | catch |
35 | fprintf('passed\n'); |
36 | end; |
Below you will find a code listing for an M-file that defines a function.
Some code has been removed after the
bold comment.
Fill in the missing code.
1 | function result = sumOfPositiveElements(vec) |
2 | %productOfOddElements return the sum of all the positive elements |
3 | % |
4 | % Consumes: vec, a vector of numbers |
5 | % Produces: a scalar number |
6 | % |
7 | % Example: |
8 | % |
9 | % >> sumOfPositiveElements([3 -2.5 6 -1]) |
10 | % ans = |
11 | % 9 |
12 | % >> sumOfPositiveElements([]) |
13 | % ans = |
14 | % 0 |
15 | % >> sumOfPositiveElements([-2 -4]) |
16 | % ans = |
17 | % 0 |
18 |
|
19 | result = 0; |
20 | |
21 |
|
22 | |
23 | for k = 1:length(vec) |
24 | |
25 | %** (10 pts) Fill in the missing code |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | end % for |
32 |
|
33 | return |
34 | end |
Below you will find complete listings of several files, except that at a few places in the files code has been removed.
Before this code was removed, these files contained complete correct source code for a C++ file to define a function to compute the area of a rectangle, and a driver program that uses that function to compute price per square inch for a rectangular pizza.
Each place where code was removed is marked with comments containing //**
and a point value.
Fill in the missing code.
1 | // areaOfRect.cc P. Conrad for CISC106 final exam, 12/10/2006 |
2 | // compute area of a rectangle |
3 | // |
4 | // Consumes: length ( a double), 345 ( a double) |
5 | // Produces: area ( a double) |
6 | // |
7 | // Examples: |
8 | // Function call: cout << areaOfRect(7,5) << endl; |
9 | // Output: 35 |
10 | // |
11 | // Function call: cout << areaOfRect(3,3.25) << endl; |
12 | // Output: 9.75 |
13 |
|
14 | double areaOfRect(double length, double width) |
15 | { |
16 | //** (4 pts) Fill in the line that computes and returns the result |
17 |
|
18 | |
19 | } |
1 | // rectPizza.cc |
2 | // P. Conrad for CISC106 Exam |
3 |
|
4 | #include <iostream> |
5 | using namespace std; |
6 |
|
7 | //** (3 pts) Something must go after this comment---otherwise when you |
8 | //** compile rectPizza.cc, you get this error: |
9 | //** error: `areaOfRect' undeclared (first use this function) |
10 | //** Fill in what is missing. |
11 |
|
12 | |
13 |
|
14 | //** (3 pts) Fill in the missing line that starts the main program in C++ |
15 |
|
16 | |
17 | { |
18 | //** (2 pts) Ask the user to input the length of the pizza |
19 | //** (One additional line of code is needed here) |
20 |
|
21 | double length; |
22 | cout << "Please enter length "; |
23 | |
24 |
|
25 | //** (6 pts) Fill in one or more lines of code as needed to |
26 | //** ask the user to input the width of the pizza |
27 |
|
28 | |
29 | |
30 | |
31 |
|
32 | //** (6 pts) Fill in one or more lines of code as needed to |
33 | //** ask the user to input the price of the pizza |
34 |
|
35 | |
36 | |
37 | |
38 |
|
39 | double ppsi; // price per square inch |
40 |
|
41 | //** (4 pts) Write a line of code that computes the price |
42 | //** per square inch and assigns it to ppsi. Use a function |
43 | //** call to areaOfRect() as part of your calculation |
44 |
|
45 | |
46 |
|
47 | //** (4 pts) Output the result with an appropriate message; that is, |
48 | //** tell the user what the price per square inch of the pizza is. |
49 |
|
50 | |
51 | |
52 | return 0; // normal successful completion of a C++ program |
53 | } |