/****************************************************************************** Datalogi 2P: Introduction to Algorithmics Exercise O1 Spring 1999 Topic: Integer multiplication Copyright: Jyrki Katajainen Department of Computing University of Copenhagen DK-2100 Copenhagen East, Denmark e-mail: jyrki@di.ku.dk Version: 1.3 January - February 1999 To compile and run the programs, use the makefile provided in the connection with this file. However, at first you should write the missing routines in the integer class. To see how to use my word class and the pair class from the Standard Template Library look at the addition routine in the integer class. For the further documentation of the Standard Template Library, consult the book by Bjarne Stroustrup or Standard Template Library Programmer's Guide available via the Worldwide Web at http://www.sgi.com/Technology/STL/ Changes made to version 1.0: 9 February 1999 Jarl Friis and "Søren Chr. Skov" digit a[n]; ... generate(a, a + n); ==> word a[n]; ... generate >(a, a + n); Changes made to version 1.1: 10 February 1999 word a[n]; ... generate >(a, a + n); ==> digit a[n]; ... generate(a, a + n); Changes made to version 1.2 15 February 1999 Person X at the lecture theatre #include was added. ******************************************************************************/ /****************************************************************************** test driver ******************************************************************************/ #if DEBUG typedef unsigned char digit; #else typedef unsigned long int digit; #endif #include // defines size_t #include // defines random and srandom #include // defines clock_t and clock() #include // defines str...() functions #include // defines pair #include // defines vector #include // defines standard streams #include // defines file streams #include "word.cc" // defines word #include "integer.cc" // defines integer template void generate(Ran s_begin, Ran s_end) { for (Ran p = s_begin; p != s_end; ++p) *p = T(random()); } void usage(int argc, char **argv) { cerr << "Usage: " << argv[0] << "[] [] []" << endl; exit(1); } void error(const char* s, const char* t = "") { cerr << s << ' ' << t << endl; exit(1); } bool files_equal(fstream& s, fstream& t) { char a; char b; while (s.get(a) && t.get(b)) { if (a != b) return false; } t.get(b); if (!s.eof() || !t.eof()) return false; return true; } int main(int argc, char **argv ){ // read the command line arguments, if any size_t n = 100; size_t repetitions = 3; unsigned long int seed = 1; switch (argc) { case 4: seed = atoi(argv[3]); if (seed == 0) usage(argc, argv); case 3: repetitions = atoi(argv[2]); if (repetitions == 0) usage(argc, argv); case 2: n = atoi(argv[1]); if (n == 0) usage(argc, argv); case 1: break; default: usage(argc, argv); } #if DEBUG // open check files char result_file [128]; char companion_file [128]; *result_file = '\0'; *companion_file = '\0'; #if SCHOOL strcat(result_file, "school"); strcat(companion_file, "divide_and_conquer"); #else strcat(result_file, "divide_and_conquer"); strcat(companion_file, "school"); #endif if (argc > 1) { strcat(result_file, argv[1]); strcat(companion_file, argv[1]); } else { strcat(result_file, "100"); strcat(companion_file, "100"); } strcat(result_file, "_"); strcat(companion_file, "_"); if (argc > 2) { strcat(companion_file, argv[2]); strcat(result_file, argv[2]); } else { strcat(companion_file, "3"); strcat(result_file, "3"); } strcat(result_file, "_"); strcat(companion_file, "_"); if (argc > 3) { strcat(companion_file, argv[3]); strcat(result_file, argv[3]); } else { strcat(companion_file, "1"); strcat(result_file, "1"); } strcat(result_file, ".check"); strcat(companion_file, ".check"); fstream result(result_file, ios::out); if (!result) error("Cannot open file", result_file); fstream companion(companion_file, ios::in); bool check_on = true; if (!companion) check_on = false; #endif // calibrate the timer clock_t c_start, c_stop, e_time, d_time; digit a[n]; integer x; integer y; integer z; srandom(seed); c_start = clock(); for (unsigned long int i = 0; i < repetitions; ++i) { generate(a, a + n); x = integer(a, a + n); y = integer(a, a + n); } c_stop = clock(); d_time = c_stop - c_start; // timing loop #if DEBUG cout << "Squaring an integer of " << n << " words " << repetitions << " times..." << endl; #else cout << n << '\t'; #endif srandom(seed); c_start = clock(); for (unsigned long int i = 0; i < repetitions; ++i) { generate(a, a + n); x = integer(a, a + n); y = integer(a, a + n); z = x * y; #if DEBUG result << "x = " << x << endl; result << "y = " << y << endl; result << "x * y = " << z << endl; #endif } c_stop = clock(); e_time = c_stop - c_start - d_time; #if DEBUG result.close(); result.open(result_file, ios::in); if (!result) error("Cannot open file", result_file); if (check_on && !files_equal(result, companion)) error("The two methods do not produce equal results!"); cout << "Average execution time in seconds: " << '\t'; #endif cout << double(e_time) / double(repetitions * CLOCKS_PER_SEC) << endl; return(0); }