/* arrayfunc2.c * Another example of arrays and functions, as well as the selection sort * function. This example was written up in lecture class */ #include int Maximum(const int list[], const int size); int Minimum(const int list[], const int size); void PrintArray (const int arr[], const int size); void SwapElements(int arr[] , int a, int b); void SelectionSort(int list[], int size); int main() { int array1[10] = {21, 7, 3, -12, 5, 0, 4, 3, 10, 8}; printf("Array 1:\n"); PrintArray(array1, 10); printf("\n"); printf("Maximum of the array = %d\n", Maximum(array1, 10)); printf("Minimum of the array = %d\n", Minimum(array1, 10)); SelectionSort(array1, 10); printf("Array 1 after the Selection sort:\n"); PrintArray(array1, 10); printf("\n"); return 0; } void SelectionSort(int list[], int size) { int pass; for (pass = 1; pass < size; pass++) { int i, largeindex = 0; for (i = 1; i <= size - pass; i++) { if (list[i] > list[largeindex]) largeindex = i; } // once I reach here, the largest value is list[largeindex] // I NEED to SWAP: position largeindex with the LAST GUY SwapElements(list, largeindex, size-pass); } } void PrintArray(const int arr[], const int size) { int i; printf("{ "); for (i = 0; i < size-1; i++) printf("%d, ", arr[i]); printf("%d }", arr[size-1]); // print last item } void SwapElements(int arr[] , int a, int b) // swap arr[a] and arr[b] { int temp; temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } int Maximum(const int list[], const int size) { int max = list[0]; // largest so far int i; for (i = 1; i < size; i++) if (list[i] > max) max = list[i]; return max; } int Minimum(const int list[], const int size) { int min = list[0]; // largest so far int i; for (i = 1; i < size; i++) if (list[i] < min) min = list[i]; return min; }