1: //timing_summation2.cpp 2: //Finds the sum of integers over a given range. 3: //Attempts to measure the time taken to sum these integers. 5: #include <iostream> 6: #include <ctime> 7: using namespace std; 9: int main() 10: { 11: cout << "\nThis program computes the sum of all integers in a " 12: "user-chosen range, and\nattempts to compute as well the time " 13: "taken to perform the summation."; 14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 16: cout << "\nEnter the smaller integer, then the larger: "; 17: int small, large; 18: cin >> small >> large; cin.ignore(80, '\n'); cout << endl; 20: int sum = 0; 21: clock_t startTime = clock(); 22: for (int numberToAdd=small; numberToAdd<=large; numberToAdd++) 23: sum += numberToAdd; 24: clock_t endTime = clock(); 26: cout << "The sum of all integers from " << small << " to " 27: << large << " (inclusive) is " << sum << ".\n"; 28: cout << "The amount of time taken to add them up was " 29: << double(endTime - startTime)/CLOCKS_PER_SEC << " seconds."; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 31: }