1: //timing_summation4.cpp 2: //Finds the sum of integers over a given range. 3: //Allows us to measure the *relative* time taken to sum the integers 4: //over ranges of various sizes by introducing an artificial delay into 5: //each step of the computation, and uses a Stopwatch class object to 6: //start and stop a "timer" which has appropriate interface functions 7: //for our purposes. 9: #include <iostream> 10: using namespace std; 12: #include "utilities.h" 13: using Scobey::Pause; 14: using Scobey::Stopwatch; 16: int main(int) 17: { 18: cout << "\nThis program computes the sum of all integers in a " 19: "user-chosen range, and\nattempts to compute as well the time " 20: "taken to perform the summation.\n"; 21: Pause(); 23: cout << "Enter the smaller integer, then the larger: "; 24: int small, large; 25: cin >> small >> large; cin.ignore(80, '\n'); cout << endl; 27: int sum = 0; 28: Stopwatch timer; 29: timer.start(); 30: for (int numberToAdd=small; numberToAdd<=large; numberToAdd++) 31: { 32: sum += numberToAdd; 33: timer.delay(); 34: } 35: timer.stop(); 37: cout << "The sum of all integers from " << small << " to " 38: << large << " (inclusive) is " << sum << ".\n"; 39: cout << "The amount of time taken to add them up was " 40: << timer.getSeconds() << " seconds.\n"; 41: Pause(); 42: }