Cs341 Practice Final
Cs341 Practice Final
Cs341 Practice Final
2) Suppose V is a vector of ints. Sort V into ascending order by calling the built-in C++ sort algorithm. Recall
that sort is defined as sort(first, lastp1, compare) where first is an iterator denoting the first
element, lastp1 is an iterator denoting one beyond the last element, and compare is a lambda expression
returning a boolean denoting whether one element should come before another (true if so, false if not).
5) Consider the program above. It has no type declarations, yet it is strongly-typed type errors are caught
& reported (vs. the all-too-common silent crashes you get from incorrect C programs). Discuss how type
errors are detected in F#. In particular, discuss whether they are caught at compile-time or run-time, and
how.
map is a powerful, higher-order function, and an important tool for functional programming. In F# map
is available as List.map F L. In C++ its available as std::transform(first, lastp1, firstout, F), where F is applied
to each element in the iterator range [first, lastp1) starting with first and up to but not including the
element denoted by lastp1. The results are stored in memory starting at the location denoted by the iterator
firstout; it is assumed that firstout can be advanced without error to store the results.
7) Youre programming in C++, and have an integer vector V along with an integer C. You need to multiply
every element of V by C. You could write a loop to do that, but today youre feeling functional. Show below
how you would call std::transform to perform this computation, using a lambda expression for F. [ Hint: store
the results back into V. ]
vector<int> V = ;
int C = ;
std::transform(
8) The operation you performed above is scalar multiplication. Write this function in F# using List.map.
let ScalarMultiply C L =
List.map
9) Given the usefulness of map, its only natural that there are versions that operate on 2 lists, 3 lists, and
more. For example, in F#, map2 is available as List.map2 F L1 L2. The function F takes 2 arguments one
element from each list and returns a single value; map2 applies F across the corresponding elements of L1
and L2, and collects the return values into a list that is ultimately returned.
An example is vector multiplication: [1; 2; 3] * [4; 5; 6] = [4; 10; 18]. Write the function VectorMultiply in
F# using List.map2. Assume L1 and L2 are the same length.
let VectorMultiply L1 L2 =
List.map2
10) Now implement the F# function map2. You must write this function in a tail-recursive manner, using only
the most basic F# features: if, match, .Head, .Tail, :: and @. No loops, no mutable variables, no other
functions from F# or .NET. Assume the two lists are the same length, though possibly empty.
[ Note: define any helper functions you may need below map2; well assume they were defined above map2 as
required by the F# compiler. ]
let map2 F L1 L2 =
11) In the Northwestern Hospital database, there is a table named Doctors. Every doctor has a unique
DoctorID, a PagerNumber, a FirstName, a LastName, and a Specialty (Heart, OB/GYN, Brain, etc.).
Answer the following questions using an SQL query:
a) How many doctors are there in each specialty? Retrieve the specialty and the # of doctors; the output
should be in descending order by the # of doctors. If 2 specialties have the same # of doctors, output
in ascending order by specialty.
b) Theres another table named NightCall that lists the doctors able to work at night. The NightCall table
contains a NightCallID and a DoctorID; the DoctorID is a foreign key. Retrieve the first and last name of
every doctor that is able to work at night. Output should be in ascending order by last name; if 2
doctors have the same last name, then output in ascending order by first name.
12) [ This question spans 2 pages ] Suppose the Netflix database contains an additional table, Users:
Users
UserID LastName FirstName Email
822109 Parker Robert [email protected]
837488 Bashi Pooja [email protected]
. . . .
. . . .
Add a GetUser(int userid) method to the Business Tier to lookup a user by their user ID, returning the users
info in the form of a User object. The first step is to define the User class do that below. Wait to define the
GetUser( ) method on the next page.
class User
{
}
// question continued on next page:
class BusinessTier
{
private DataAccessTier datatier; // data tier to execute SQL:
.
.
.
public User GetUser(int userid)
{