C++ Preprocessing Directives in Debugging
#include <iostream> #include <cstdlib> // For random number generator #include <ctime> // For time function using std::cout;//from ww w . ja v a 2s . co m using std::endl; #define TESTINDEX // Function to generate a random integer 0 to count-1 int random(int count) { return static_cast<int>( count*static_cast<unsigned long>(std::rand())/(RAND_MAX+1UL)); } int main() { int a {10}, b {5}; int result {}; int (*pfun[])(int, int) {fun::sum, fun::product, fun::difference}; int fcount {sizeof pfun/sizeof pfun[0]}; int select {}; srand(static_cast<unsigned>(time(0))); for(int i {} ; i < 10 ; ++i) { select = random(fcount); // Generate random index 0 to fcount-1 #ifdef TESTINDEX std::cout << "Random number = " << select << std::endl; if((select >= fcount) || (select < 0)) { std::cout << "Invalid array index = " << select << std::endl; return 1; } #endif result = pfun[select](a, b); // Call random function cout << "result = " << result << endl; } result = pfun[1](pfun[0](a, b), pfun[2](a, b)); std::cout <<"The product of the sum and the difference = " << result << std::endl; }