CS 372 Spring 2016 > Assignment 2
CS 372 Spring 2016
Assignment 2
Assignment 2 is due at 5 pm Tuesday, February 16. It is worth 20 points.
Procedures
This assignment is to be done individually.
Turn in answers to the exercise below on the UAF Blackboard Learn site, under Assignment 2 for this class.
- Your answers should consist of one file:
fa2.h
, from Exercises A, B, and C. This file should be attached to your submission. - Send only the above! I do not want things I already have, like the test program.
- I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (20 pts total)
General
In each of the following exercises,
you are to write a C++ function or function template.
All functions & function templates are to be
in the file fa2.h
.
Code must be readable and of high quality.
You may include any other functions or classes that you wish
in file fa2.h
.
These will not be tested.
Test Program
A single test program for all of the exercises
is available:
fa2_test.cpp
.
If you compile and run this program (unmodified!) with your code
and the catch.hpp
header,
then it will test
whether your code works properly.
The catch.hpp
header holds the Catch C++ unit-test framework.
A link to the GitHub page for Catch
is on the main CS 372 webpage.
Do not turn in the test program.
Note:
The test program cannot check all the requirements of this
assignment.
For example, it cannot check whether you use the C++11
random-number generation functionality in Exercise A
or whether you use std::async
calls
in Exercise B.
I will check these by looking at your code.
Exercise A — Generating Random Numbers
Purpose
In this exercise you will write code to use C++11 pseudorandom-number generation functionality.
Instructions
Write a C++ function normRand
,
prototyped as follows.
[C++]
inline vector<double> normRand(size_t n);
Calling normRand(n)
returns a vector
of size n
,
containing random numbers generated according to a
normal distribution with mean \(100.0\) and standard deviation \(15.0\).
The random numbers must be generated by an appropriate
C++11/14 Standard Library random-number generator
and distribution,
from header <random>
.
The numbers generated must come from the same sequence of values
each time the function is called.
For example, suppose that
normRand(4)
returns a vector
containing
120.5
, 89.3
, 101.2
, 91.7
.
Then
normRand(2)
must return a vector
containing
120.5
, 89.3
.
You may assume that parameter n
is nonnegative.
But your code must place no arbitrary limits on the value
of parameter n
.
Exercise B — Asynchronous Computation
Purpose
In this exercise,
your will write code to do asynchronous computation
using std::async
calls.
Instructions
Write a C++ function asyncSquares
,
prototyped as follows.
[C++]
inline vector<int> asyncSquares(size_t n);
Calling asyncSquares(n)
returns a vector
of size n
,
in which item \(i\) has the value \(i^2\).
So the items should be \(0\), \(1\), \(4\), \(9\), \(16\), \(25\), etc.
The values in the vector
much be computed
asynchronously.
Each item must be computed using a separate
std::async
call
with launch policy std::launch::async
,
and retrieved using a get
on its return value.
The std::async
calls should all be done
before any get
calls are made.
You may assume that parameter n
is nonnegative.
But your code must place no arbitrary limits on the value
of parameter n
.
Exercise C — Iterating a Function
Purpose
In this exercise you will write code to deal with a function or function-like object using C++11 features.
Below, the term function object
refers to a function or function-like object:
a function (pointer),
a lambda function,
a bind expression,
a std::function
object,
or anything that is callable using the same syntax as a C++ function.
Instructions
Write a C++ function or function template
repeatFunction
.
This should take two parameters:
a one-parameter function object f
and an int
n
.
It returns a function object whose effect is the same as
calling f
, and then calling f
again
on the return value, etc., n
times.
You may assume that the parameter type and the return type
of f
are both int
,
and that n
is positive
(in particular, it will not be zero).
Appropriate C++11/14 function objects must be used.
For example, consider the following code.
[C++]
int f(int k) { return k+5; } auto g = repeatFunction(f, 3);
Now, doing g(k)
is the same as doing
f(f(f(k)))
.
So g(k)
should return ((k+5)+5)+5
,
that is, k+15
.
Here is another example.
[C++]
int ff(int x) { return x*2; } auto gg = repeatFunction(ff, 4);
Now, doing gg(x)
is the same as doing
ff(ff(ff(ff(x))))
.
So gg(k)
should return (((x*2)*2)*2)*2
,
that is, x*16
.