/* Example programs from the book Scientific and Engineering Programming in C++: An Introduction with Advanced Techniques and Examples, Addison-Wesley, 1994. (c) COPYRIGHT INTERNATIONAL BUSINESS MACHINES CORPORATION 1994. ALL RIGHTS RESERVED. See README file for further details. */ #include #include "examples/ch11/CheckedSimpleArray.h" template float average(SimpleArray& a) { double sum = 0.0; for (int i = 0; i < a.numElts(); i++) sum += a[i]; return sum / a.numElts(); } void f() { CheckedSimpleArray a(10); // ... set values in a cout << average(a) << endl; } int main() { // Check operation as array CheckedSimpleArray a(10); for (int i = 0; i < a.numElts(); i++) a[i] = 2*i; CheckedSimpleArray b(a); if (a.numElts() != b.numElts()) { cerr << "Bad copy: numElts wrong" << endl; return 1; } for (i = 0; i < b.numElts(); i++) { if (b[i] != 2*i) { cerr << "Bad copy: elements wrong" << endl; return 1; } } b = 10; for (i = 0; i < b.numElts(); i++) { if (b[i] != 10) { cerr << "Bad assignment of scalar" << endl; return 1; } } b = a; for (i = 0; i < b.numElts(); i++) { if (b[i] != 2*i) { cerr << "Bad array assignment" << endl; return 1; } } if (average(a) != 9) { cerr << "Bad average: " << average(a) << endl; return 1; } // Check that errors are caught try { b[100]; cerr << "Bad subscript not caught" << endl; return 1; } catch(CheckedSimpleArray::SubscriptRangeError) { } { CheckedSimpleArray ia(4); // ok CheckedSimpleArray ipa(4); // ok typedef float Coord; CheckedSimpleArray ca(4); // ok CheckedSimpleArray fa(4); // ok, same as previous. CheckedSimpleArray< CheckedSimpleArray > iaa(4); // ok } return 0; }