# include # include # include using namespace std; int main ( int argc, char *argv[] ); double cpu_time ( void ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for SUM_MILLION. // // Discussion: // // SUM_MILLION is a sketch of a program to generate the numbers // from 1 to 1,000,000, add them, and print the result. // // This program is only a sketch. Your job is to finish it, // debug it, and get it to run. // { # define N 1000000 double ctime; double ctime1; double ctime2; double error; double exact = 500000500000.0; int flops; int i; double mflops; double total; double x[N]; cout << "\n"; cout << "SUM_MILLION:\n"; cout << " C++ version.\n"; cout << "\n"; cout << " Task: Add the numbers from 1 to " << N << "\n"; // // Step 1: // Load the array X with the values. // for ( i = 0; i < N; i++ ) { x[i] = i + 1; } // // Step 2: // Start the clock. // Add up the entries in X. // Stop the clock. // Compute elapsed CPU time; // Set the number of FLOPs. // Compute the MFLOPS rate. // Compute the error. // MISSING CODE // // Step 3: // Print results; // cout << "\n"; cout << " Total = " << total << "\n"; cout << " Error = " << error << "\n"; cout << " CPU time = " << ctime << "\n"; cout << " MFLOPS = " << mflops << "\n"; return 0; # undef N } //****************************************************************************80 double cpu_time ( void ) //****************************************************************************80 // // Purpose: // // CPU_TIME reports the elapsed CPU time. // // Modified: // // 27 September 2005 // // Author: // // John Burkardt // // Parameters: // // Output, double CPU_TIME, the current total elapsed CPU time in second. // { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; }