Sei sulla pagina 1di 4

Curtin University of Technology Department of Electrical and Computer Engineering Engineering Programming 210

Laboratory Session 6 1 Exercises

Exercise 1. Suppose that we have two arrays of integers in ascending order, say a[ ] and b[ ]. If we want to merge them into another array in ascending order, say c[ ], then the algorithm to do so is simple. First compare a[0] and b[0]. Whichever is smaller, say b[0], put into c[0]. Next compare a[0] and b[1]. Whichever is smaller, say b[1], put into c[1]. Next compare a[0] and b[2]. Whichever is smaller, say a[0], put into c[2]. Next compare a[1] and b[2], and so on. Eventually one of the arrays a[ ] or b[ ] will be exhausted. At that point the remainder of the elements in the other array must be copied into c[ ]. The function that does this is the following. /* Merge a[] of size m and b[] of size n into c[].*/ void merge(int a[], int b[], int c[], int m, int n) { int i = 0, j = 0, k = 0; while (i < m && j < n) if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; while (i < m) c[k++] = a[i++]; while (j < n) c[k++] = b[j++]; } where the array c[ ] is assumed to contain enough space to hold both a[ ] and b[ ]. The programmer must make certain that the bounds on c[ ] are not overrun. A sort based on the above merge algorithm is called a merge sort and very ecient in constrast to a bubble sort. One version of the merge sort acts on an array, which has a size that is a power of 2. The algorithm works in a number of passes. After the rst pass we want successive pair of integers to be in order. After the second pass we want each successive quiate of integers to be in order. After the third

/* pick up any remainder */

pass we each successive octet of integers to be in order. At each stage merge() is used to accomplish the desired ordering. The following function implements the algorithm and illustrates the power of pointer arithmetic. /* Mergesort: Use merge() to sort an array of size n */ #include #include <stdio.h> <stdlib.h>

void mergesort(int key[], int n) { int j, k, m, *w; for (m = 1; m < n; m *= 2) ; if (m != n) { printf("ERROR: Size of the array is not a power of 2 - bye!\n"); exit(1); } w = calloc(n, sizeof(int)); /* allocate workspace */ for (k = 1; k < n; k *= 2) { for (j = 0; j < n - k; j += 2 * k) merge(key + j, key + j + k, w + j, k, k); /* merge into w */ for (j = 0; j < n; ++j) key[j] = w[j]; /* write w back into key */ } free(w); /* free the workspace */ } a) Write a program to test mergersort() on the following 16 integers: 4, 3, 1, 67, 55, 8, 0, 4, 5, 37, 7, 4, 2, 9, 1, 1 and to print out the array after each pass. b) Modify mergesort() so that it can be used with an array of any size, not just with a size that is a power of two. Recall that any positive integer can be expressed as a sum of powers of two. For example, 27 = 16 + 8 + 2 + 1 Consider the array as a collection of subarrays of sizes that are powers of two. Sort the subarrays and then use merge() to produce the nal sorted array.

Exercise 2. A common use of function pointers is in text-based menu-driven systems. A user is prompted to select an option from a menu (possibly from 1 to 5) by typing the menu items number. Each option is serviced by a dierent function. Pointers to each function are stored in an array of pointers to functions. The users choice is used as a subscript in the array, and the pointer in the array is used to call the function. The program in Figure 1 provides a generic example of the mechanics of dening and using an array of pointers to functions. We dene three functions function1, function2 and function3that each take an integer argument and return nothing. We store pointers to these three functions in array f, which is dened in line 12. The denition is read beginning in the leftmost set of parentheses, f is an array of 3 pointers to functions that each take an int as an argument and return void. The array is initialized with the names of the three functions. When the user enters a value between 0 and 2, the value is used as the subscript into the array of pointers to functions. In the function call (line 24), f[choice] selects the pointer at location choice in the array. The pointer is dereferenced to call the function, and choice is passed as the argument to the function. Each function prints its arguments value and its function name to demonstrate that the function is called correctly. The following shows how the program interacts with the user.

Figure 1 The program for demonstrating an array of pointers to functions. a) Create a text-based, menu-driven program that allows the user to choose whether to calculate the circumference of a circle, the area of a circle or the volume of a sphere. The program should then input a radius from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives a double parameter. The corresponding functions should each display messages indicating which calculation was performed, the value of the radius and the result of the calculation. b) Create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corresponding functions should each display messages indicating which calculation was performed, the values of the parameters and the result of the calculation.

Potrebbero piacerti anche