The LineSpace Algorithm generates linearly spaced vector. You are asked to write a C++ function that has the following syntax:
1 | vector<double> linespace(double start, double ed, int num); |
vector<double> linespace(double start, double ed, int num);
For example,
1 | vector<double> pts = linespace(-5, 5, 7); |
vector<double> pts = linespace(-5, 5, 7);
should return a vector of doubles [-5.0, -3.3, -1.6, 0, 1.6, 3.3, 5]. Please note that the LineSpace algorithm can be applied to 2D, 3D and even higher dimensions. Also, it can be applied to complex numbers as well. However, for simple educational purpose, we’ll cover the 1-D which is the scalar double values.
C++ Implementation of LineSpace Algorithm
The first and the last point should not be changed so that both equal to exactly the inputs. The number of total points (num) return divide the [start, end] to (num – 1) linearly spaced segments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | vector<double> linespace(double start, double ed, int num) { // catch rarely, throw often if (num < 2) { throw new exception(); } int partitions = num - 1; vector<double> pts; // length of each segment double length = (ed - start) / partitions; // first, not to change pts.push_back(start); for (int i = 1; i < num - 1; i ++) { pts.push_back(start + i * length); } // last, not to change pts.push_back(ed); return pts; } |
vector<double> linespace(double start, double ed, int num) { // catch rarely, throw often if (num < 2) { throw new exception(); } int partitions = num - 1; vector<double> pts; // length of each segment double length = (ed - start) / partitions; // first, not to change pts.push_back(start); for (int i = 1; i < num - 1; i ++) { pts.push_back(start + i * length); } // last, not to change pts.push_back(ed); return pts; }
Unit Test
How do we know the above implementation is correct? We can sure unit test it. For example, you could use CppUnit, the popular unit test framework in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | void testLineSpace(double start, double ed, int num, const vector<double> &pts) { // test output vector size assertEquals(num, pts.size()); // test first element assertEquals(start, pts[0]); // test last element assertEquals(end, pts[num - 1]); // expected distance between neighbour points double expDist = (ed - start) / (num - 1); for (int i = 1; i < num; i ++) { double dist = pts[i] - pts[i - 1]; // test length of each segment assertEquals(expDist, dist, 1e-2); } } |
void testLineSpace(double start, double ed, int num, const vector<double> &pts) { // test output vector size assertEquals(num, pts.size()); // test first element assertEquals(start, pts[0]); // test last element assertEquals(end, pts[num - 1]); // expected distance between neighbour points double expDist = (ed - start) / (num - 1); for (int i = 1; i < num; i ++) { double dist = pts[i] - pts[i - 1]; // test length of each segment assertEquals(expDist, dist, 1e-2); } }
In above test function, we test the number of vectors which should be equal to the input parameter num and we also test the first and the last points in the vector should equal to the input parameters start and ed. Then most importantly, each segment should equal to the size that can be calculated from the total length divided by the number of partitions.
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Display Text in Browser using the Echo API ?
Next Post: C/C++ Coding Exercise - Convert a Number to Hexadecimal?