// ic210 sec 4002 //6 nov 2009 #include #include using namespace std; int maxIndex(int *array, int size); void selectionSort(int *arrayIn, int sizeIn); void swap(int& a, int& b); int search(int value, int* arrayIn, int size); bool isBefore(int, int); int main() { // create array and initialize with values int size ; cout << "How many integers do you want to enter? "; cin >> size; int* array = new int[size]; cout << "Enter a list of " << size << " integers: " <> array[i]; // sort the array selectionSort(array, size); // display the array for (int j = 0; j < size; j++) cout << array[j] << ' '; cout << endl; return 0; } // Returns the index of the maximum element in the array int maxIndex(int *array, int size) { int maxIndex = 0; //int maxValue = array[0]; for (int i=1; i < size; i++) { if (isBefore(array[maxIndex],array[i])) { maxIndex = i; //maxValue = array[i]; } } return maxIndex; } // What does this function do? //sorting the array //selection sort void selectionSort(int *arrayIn, int sizeIn) { for(int size = sizeIn; size > 1; size--) { //int k = maxIndex(arrayIn,size); swap(arrayIn[maxIndex(arrayIn,size)], arrayIn[size-1]); //int temp = arrayIn[size-1]; //arrayIn[size-1] = arrayIn[k]; //arrayIn[k] = temp; } } void swap(int& a, int& b) { int temp = a; a = b; b = temp; } //return size if not found //return position if found int search(int value, int* arrayIn, int size) { int i; for (i = 0; i < size; i++) { if (arrayIn[i] == value) { return i; } //The following works only if input is sorted!!! else if (arrayIn[i] > value) { return size; } } return i; } bool isBefore(int a, int b) { return abs(a) > abs(b); /* if (a < b) { return true; } else { return false; } */ }