// FILE: DoubleMin.cpp // FIND THE INDEX OF THE SMALLEST VALUE IN A FLOATING POINT SUBARRAY // CONSISTING OF ELEMENTS x[start_index] THROUGH x[end_index] int find_index_of_min (const apvector & x, // IN: array of elements int start_index, // IN: subscript of first element int end_index) // IN: subscript of last element // Returns the subscript of the smallest element in the subarray. // Returns -1 if the subarray bounds are invalid. // Pre: The subarray is defined and 0 <= start_index <= end_index. // Post: x[min_index] is the smallest value in the array. { // Local data ... int min_index; // index of the smallest element found int i; // index of the current element // Validate subarray bounds if ((start_index < 0) || (start_index > end_index)) { cerr << "Error in subarray bounds" << endl; return -1; // return error indicator } // Assume the first element of subarray is smallest and check the rest. // min_index will contain subscript of smallest examined so far. min_index = start_index; for (i = start_index + 1; i <= end_index; i++) if (x[i] < x[min_index]) min_index = i; // All elements are examined and min_index is // the index of the smallest element. return min_index; // return result for valid subarray } // end find_index_of_min