Vector
class from programming assignment 1.
This assignment is both about vector arithmetic and about C++. With regard to the former, you must implement various operations. With regard to the latter, you must use return by reference, operator overloading, exceptions, and initializer lists.
Multiplication by a Scalar: Given \(\alpha \in \mathbb{R}\) and \(\bs{p} \in \mathbb{R}^n\), \(\alpha \bs{p} = (\alpha p_1, \alpha p_2, \ldots, \alpha p_n)\).
Addition: Given \(\bs{q} \in \mathbb{R}^n\) and \(\bs{r} \in \mathbb{R}^n\), \(\bs{q} + \bs{r} = (q_1 + r_1, q_2 + r_2, \ldots, q_n + r_n)\).
Subtraction: Given \(\bs{q} \in \mathbb{R}^n\) and \(\bs{r} \in \mathbb{R}^n\), \(\bs{q} - \bs{r} = (q_1 - r_1, q_2 - r_2, \ldots, q_n - r_n)\).
Inner Product: Given \(\bs{q} \in \mathbb{R}^n\) and \(\bs{r} \in \mathbb{R}^n\), \(\bs{q} \cdot \bs{r} = q_1 r_1 + q_2 r_2 + \ldots + q_n r_n\) (or, using summation notation, \(\bs{q} \cdot \bs{r} = \sum_{i=1}^{n} q_i r_i\)).
Note that the setValues()
method is now protected
(rather than public) and has been overloaded with a version that is
passed a pointer to an array of double
values. Note
also that the setValue()
method has been eliminated.
begin()
method returns a pointer to the first
element of an initializer list. So, in the following example,
values
points to the first double
(in the array of double
values) that is associated with
v
.
void doSomething(std::initializer_list<double> v) { const double* values; values = v.begin(); // Work with the array }
Remember to use the -std=c++17
flag when compiling
(since initializer lists of this kind are a fairly recent feature of
C++).
There are several good unit testing frameworks/harnesses available for C++, and you should feel free to use one of them, but it may be easier to use a "home grown" system until you become more comfortable with C++.
The file UnitTest.hpp
contains a very simple
framework (that you will be able to expand on as you learn more
C++) that allows you to make calls to assertEquals()
functions as in JUnit, the file ExampleUnitTests.cpp
contains an example of how it can be used, and the file
ExampleMakefile
contains a makefile
that can be used to build the executable.
At this point, this framework won't let you test for exceptions or compare objects, but it should let you do most of what you need, and it should be easy to understand.
Though you will not be submitted your unit tests, hence will not be assessed on their coverage, you should probably use a test coverage tool to help ensure that your are covering your code adequately. See the course "Tools" and "Help" pages for more information.
==
operator
fairly early in the process.
As always, it is strongly recommended that you design, implement, debug, and test one method/function at a time.
pa2.zip
that contains
just Vector.h
and Vector.cpp
using Autolab.
Copyright 2020