// example of arrays, and functions using arrays #include void PrintArray(const int[] , const int); void SwapElements(int[] , int, int); int main() { int list1[5] = {2, 6, 9, -10, 12}; // array of 5 integers int i; // for loop indexing int list2[20]; // array size 20, uninitialized printf("\nlist1 = "); PrintArray(list1, 5); printf("\n"); // compute the sum of the list elements int total = 0; for (i = 0; i < 5; i++) total = total + list1[i]; printf("Sum of list1 elements = %d\n", total); // initialize the array to 2, 4, 6, 8, 10, ... for (i = 0; i < 20; i++) list2[i] = 2 * i + 2; printf("\nlist2 = "); PrintArray(list2, 20); // display the contents printf("\n"); // create a third list, and get user to enter the data int list3[10]; printf("Enter 10 integer values: "); for (i = 0; i < 10; i++) scanf("%d", &list3[i]); // user enter the items printf("\nlist3 = "); PrintArray(list3, 10); // display the contents printf("\n"); // find the largest element of list3 int max = list3[0]; // largest so far for (i = 1; i < 10; i++) if (list3[i] > max) max = list3[i]; printf("Largest number in list3 is: %d \n", max); printf("Swapping elements 4 and 7\n"); SwapElements(list3, 4, 7); printf("\nlist3 = "); PrintArray(list3, 10); printf("\n\n"); return 0; } 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; }