//============================================================================ // Name : increasing-numbers-v2.cpp // Author : R. W. Hasker // Version : // Description : Rewrite of increasing-numbers.cpp using functions and doubles. //============================================================================ #include using namespace std; const int MAX_SCORES = 100; // prototypes - note the semicolons at the ends of the lines // Function comments are given below, but it would be more // traditional to put the documentation here (where clients will see it) int readScores(double scores[], int); bool increasing(double nums[], int size); int main() { double scores[MAX_SCORES] = {0.0}; cout << "Enter up to " << MAX_SCORES << " scores: "; int num_scores = readScores(scores, MAX_SCORES); if (increasing(scores, num_scores)) cout << "Always increasing." << endl; else cout << "Decreased in at least one case."; return 0; } // Read up to max values into the array scores. // The data read is returned through the array, and the actual number // of scores read is returned as the function's value. int readScores(double scores[], int max) { double s; cin >> s; int count = 0; while (count < max && cin.good()) { scores[count] = s; ++count; cin >> s; } return count; } // return true iff values first size values in nums are strictly increasing // Note that nums[] can also be declared as a pointer: // bool increasing(double* nums, int size); // without any other changes to the function. nums[] could also be marked // as const. bool increasing(double nums[], int size) { // terminate: find i such that nums[i] > nums[i+1] or i+1>size auto i = 0; while (i < size - 1 && nums[i] <= nums[i+1]) i++; return i >= size - 1; }