Each program (the source code .cpp file) should be submitted through Blackboard (Course Materials > Lab).
You can submit all your programs at the end of the lab session in one submission. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.
Basically, submit when you are sure that it will be your final version.
Each program should start with a comment that contains your name and a short program description, for example:
/*
Author: Your Name
Description: Lab 1. Task 1. Hello world
*/
You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.
vector<T>
is a template class that works essentially like an array, but with many improvements.
Constructors:
vector<int> v;
makes an empty vector of integers (contains no elements),vector<double> v(10);
makes a vector of double-precision floating point numbers with 10 elements.Accessing the elements of a vector
The elements of a vector are accessed the same way as the elements of a simple array:
vector<char> v(5);
v[0] = 'A';
v[4] = 'E';
Some member functions:
v.push_back(x)
adds element x
at the end of the vector (since vectors are essentailly extensible arrays,
it’s common to create an empty vector, and sequetially add elements to the vector using this function).v.size()
returns the number of elements in the vector v
,To read more about the class vector<T>
, please refer to this article
(in particular, read until the section Iterators).
A box is a container that may contain other boxes. And there may be many levels of nested boxes inside …
There is only one condition: all boxes placed inside another box must be either all arranged horizontally, or all arranged vertically. Schematically, it is shown in the figure below.
How can we implement such a class Box
?
Box
is a very simple class that has two private member variables:
children
, anddir
.enum Direction { HORIZONTAL, VERTICAL };
class Box {
private:
Direction dir;
std::vector<Box> children;
public:
...
};
In your opinion, why do you think we could not use an array for storing internal boxes?
In this lab, we will be writing constructors for this class that make particular arrangements of boxes.
You are provided with a function print
for printing a box and all other boxes it contains.
An empty box (with no children) would look like:
+-+
| |
+-+
A box with two empty boxes arranged horizontally would be
+------+
|+-++-+|
|| || ||
|+-++-+|
+------+
and so on.
>> Download the source code <<
This is not a part of the assignment, but it is worth mentioning that the printing function works using the public methods of the
class Box
that compute the width and the height of a box. This computation is done recursively:
The base case is the empty box, it has width = 3 (in characters), and height = 3.
To compute the width of a bigger box, assuming that the child boxes are placed horizontally,
the total width is the sum the widths of the children +2 for margins. Similarly, the height
is the max of the heights of the children +2 for margins.
(And of course, if the child boxes are arranged in the vertical direction,
then the total width is the max, and the total height is the sum).
Another example of a complex arrangement of nested boxes:
Take the source code (boxes.cpp). Run the program, make a few boxes using two available constructors and print them out.
Box(Direction d, int num)
.Add a constructor Box(Direction d, int num)
that creates a box with num
empty boxes
arranged in the direction specified by the parameter d
.
To test your implementation, write a main
function similar to
int main() {
Box b1(HORIZONTAL, 0);
Box b2(HORIZONTAL, 1);
Box b3(VERTICAL, 3);
Box b4(HORIZONTAL, 5);
print(b1);
print(b2);
print(b3);
print(b4);
}
Box(int width, int height)
.Add a constructor Box(int width, int height)
that creates a box with
height
-many narrow boxes arranged vertically. And each of those child boxes
should contain width
-many empty boxes arranged horizontally.
For example, Box b(4,3)
should create a box like this:
Write a main
function to test the program. Test that the constructor works for various values of width
and height
greater or equal to 1.
(You may assume that these two parameters cannot be smaller than 1.)
Box(vector<int>)
.Add another constructor Box(vector<int> v)
that creates a box where the internal boxes are arranged horizontally, and
each internal box itself contains v[i]
-many children. As a result, we obtain something similar to a bar chart with
the bars going down.
Example:
When the vector contains { 1, 4, 2, 0, 3, 1 }, the box should look like:
Write a main
function to verify that the constructor works correctly.
Box(int)
.As we know, Fibonacci numbers are defined recursively as
F(0) = 1,
F(1) = 1,
F(n) = F(n-1) + F(n-2) for all n > 1.
Similarly, we can define Fibonacci boxes, FB(n):
When the paramenter n is equal to 0 or 1, it is an empty box.
Otherwise, when n > 1, a Fibonacci box FB(n) is a made of two smaller boxes FB(n-1) and FB(n-2) composed horizontally:
Add (or edit) a constructor Box(int n)
that generates such Fibonacci boxes. Also write a main
function that
prints out the Fibonacci boxes for n = 0, 1, 2, 3, 4, and 5.