/** **************************************************************************** * Driver program to run and count operations for loop-based analysis exercises* * * * @author Henry M. Walker * * @file TestAnalysisCount.c * * @date November 13, 2024 * * * *******************************************************************************/ #include #include #include "loopCount.hpp" using namespace std; int main(int argc, char * argv []) { int NUM_EXERCISES = 6; long (* funcArray [ ]) (int, int*, int*, int*, int*, int*) = {run0Count, run1Count, run2Count, run3Count, run4Count, run5Count}; // preliminaries cout << "Program to run loop-based code segments\n"; int exer; // loop exercise to run int n; // size of test try { // error checking of command-line arguments // check for exactly 2 numbers supplied if (argc != 3) { throw 1; // error 1: wrong number o arguments } istringstream iss1 (argv[1]); if (!(iss1 >> exer) || !iss1.eof() || (exer < 0) || (exer >= NUM_EXERCISES) ) { throw 2; // error 2: first argument not a number; } istringstream iss2 (argv[2]); if (!(iss2 >> n) || !iss2.eof() || (n <= 0) ) { throw 3; // error 2: second argument not a number; } } // end try block catch (int except) { cout << "command-line error\n"; switch (except) { case 1: cout << " 2 arguments required\n"; case 2: cout << " arg. 1: Loop variations to use --- a number between 1 and " << NUM_EXERCISES << "(inclusie)\n"; case 3: cout << " arg. 2: number of iterations to time --- a positive number\n"; default: cout << " integer error identified\n"; } ; cout << "exiting program\n"; exit(1); } catch (...) { cout << "exception of another type\n"; exit (1); } cout << "Running Exercise " << exer << " with time iterations = " << n << "\n";; int ACt, BCt, SCt, MCt, PCt; // variables to count assignments, Boolean ops, etc. // now test desired method long res = funcArray[exer] (n, &ACt, &BCt, &SCt, &MCt, &PCt); cout << "operation couts\n"; cout << " assignments: " << ACt << "\n"; cout << " Bool.ops: " << BCt << "\n"; cout << " + or - ops: " << SCt << "\n"; cout << " *, /, % ops: " << MCt << "\n"; cout << " increments: " << PCt << "\n"; cout << "sum returned: " << res << "\n"; return 0; }