C++ assignment question

The overall goal of the coursework is to investigate the use C++ templates to perform simple program generation tasks.

Part 1: Expression Templates

Write C++ templates which allow you to use types to represent integer arithmetic expressions over the four basic operations (addition, subtraction, multiplication, integer division), a single variable, and integer literals, for instance

x + (x-2) * (x-3)

For the purposes of Parts 1 and 2 we refer to the single variable as x.

The types representing the expressions should contain a function named ‘eval’ that provides code for evaluating the expression. This function should accept an integer parameter that will be used as the value for the single variable x.

Write a main() method that builds an expression as a type and evaluate it at some integer input value.

Print the results to standard out.

Part 2: Bounds

Extend your expression templates from Part 1 to allow each Expression type to carry with it a lower and upper bound for the possible values the expression could evaluate to. Of course, this depends on the range of values for the single variable x.

We will assume that the bounds on the input are statically known, and therefore you should parameterise the template type for representing the variable expressions on a type BOUNDS. The type BOUNDS itself should be a template parameterised on two integer values: the lower and upper values of the range for x.

Modify the eval function to throw an exception in case the input for x is not in the specified input range. Now modify your example in main() to build the expression based on a known range for the input values for x.

Print to standard out the calculated bounds for the output from the expression before printing the result of the evaluation.

For example, suppose that the bounds on variable x 0 <= x <= 5. Let us write this concisely as x_{`0,5`}.

Consider a sample expression with a variable that carries these bounds:

(x_{`0,5`} + 3) * ( x_{`0,5`} + 5)

Then we can calculate the bounds on the whole expression above as follows:

3 <= x_{`0,5`} + 3 <= 8 and 5 <= x_{`0,5`} + 5 <= 10

Consequently we have that

15 <= ( x_{`0,5`} + 3 ) * ( x_{`0,5`} + 5 ) <= 80

That is, the bounds for the above expression would be (15,80) if the bounds on x were (0,5).

Part 3: Multiple Variables

Modify your expression templates from Part 2 to allow for multiple variables in the expression. Each occurrence of a variable expression can be considered as the unique variable named x, y, z,

... For example

x + (x-2) * (x-3) would be considered as x+ (y - 2) * (z - 3).

Modify the eval function to accept an array of integer values and evaluate each variable according to the corresponding position in the input array. For instance, in the above example variable z should evaluate to the 3rd value of the input array.

Note that each variable expression should be parameterised on a BOUNDS type so that there may be different bounds for each variable.

Modify your example in main() to provide a number of example input arrays and store the results of evaluation in an array called outputs.


Please submit well-documented C++ source code for each Part. That is, submit a zip archive with subdirectories called Part 1, Part 2, and Part 3. In each subdirectory have the relevant versions of the C++ templates, the example program requested and any other test programs you wish to show.