This driver uses the double-bound array class, including
copying and assigning objects. When you use it with
the first implementation, it gives some wrong answers.
/*
* This is a driver for the bound array class.
*/
#include <cstdlib>
#include <string>
#include <iostream>
using std::string;
// This is a dirty trick that allows me to compile with different bounded
// array headers. Normally uses boundarr1.h, but add compile flag
// -DBAHDR=\"somethingelse.h\" to use another. Note that this procedure
// depends on the compiler used, but they will probably be pretty consistent
// for this. How you add the flag depends on how you are running the
// compiler. If an IDE, you'll have to much through its menus.
#ifndef BAHDR
#define BAHDR "boundarr1.h"
#endif
#include BAHDR
/*
* This function takes a BoundArr by value and increments each item by
* the given amount, and prints it before and after. It should, of course,
* leave the argument unchanged.
*/
void arrinc(string label, BoundArr<int> arr, int incr)
{
std::cout << " " << label << " before: " << arr << std::endl;
for(int i = arr.low(); i <= arr.high(); ++i)
arr.at(i) += incr;
std::cout << " " << label << " + " << incr << ": " << arr << std::endl;
}
int main()
{
std::cout << "Using the version in " << BAHDR << std::endl << std::endl;
// Make two arrays and fill them. The numbers have no
// particular meaning.
BoundArr<int> bai1(5,10);
for(int i = bai1.low(); i <= bai1.high(); ++i)
bai1.at(i) = 5 + i/6 + 2*i - i*i/5;
BoundArr<int> bai2(-4,8);
for(int i = bai2.low(); i <= bai2.high(); ++i)
bai2.at(i) = 4 +
17*bai1.at(bai1.low() +
(i - bai2.low()) % bai1.size())/7;
std::cout << "Two arrays with a bunch of useless numbers:" << std::endl;
std::cout << " bai1: " << bai1 << std::endl;
std::cout << " bai2: " << bai2 << std::endl << std::endl;
// Call the exciting function a couple of times.
std::cout << "Using a function to increment copies of each array:"
<< std::endl;
arrinc("bai1", bai1, 8);
arrinc("bai2", bai2, -5);
std::cout << std::endl;
// The arrays were passed to the incrementer by value, so are unchanged.
std::cout << "But since copies were used, the arrays are unchanged "
<< "in main:" << std::endl;
std::cout << " bai1: " << bai1 << std::endl;
std::cout << " bai2: " << bai2 << std::endl << std::endl;
bai1 = bai2;
// So now they're the same.
std::cout << "Now we've assigned bai2 to bai1 so they are the same:"
<< std::endl;
std::cout << " bai1: " << bai1 << std::endl;
std::cout << " bai2: " << bai2 << std::endl << std::endl;
// Make changes to bai1.
for(int i = bai1.low()+1; i <= bai1.high()-1; ++i)
bai1.at(i) = 0;
// And independent copies.
std::cout << "And they are two independent copies so changing one "
<< "leaves the other alone:" << std::endl;
std::cout << " bai1: " << bai1 << std::endl;
std::cout << " bai2: " << bai2 << std::endl;
}