CS240 Spring 2011: LAB06


Goal

Use function pointers to create a polymorphic function that can operate on char, int, and double datatypes, separating them based upon different characteristics.

Function Pointers

In this lab, you will implement a function, separate(), that will make use of three other functions isalpha_separator(), positiveInt_separator(), and iswholeDouble_separator() to separate char, int, and double datatypes in their own arrays. The aim is to illustrate how to use function pointers as well as their use in creating polymorphic functions.

You will use the files found here to complete this lab. This includes Makefile, main.c, separate.c, and separate.h.

For this lab, you are required only to implement the functions in separate.c.

void separate(void **array, const int size, int(*separator)(const void*)); This function takes an array of void pointers to values and rearranges those void pointers using a separator function.
Each void pointer is fed to the separator function which returns either 1 or 0.
Those pointers that resulted in a 0 should occupy the first (lowest) indices of the array.
Those pointers that resulted in a 1 should occupy the last (highest) indices of the array.
Aside from placing the 0 results in front of the 1 results, the order of the pointers should not be changed.

Example: array's pointers point to {a, b, c, 1, 2, 3, x, y, z, 0} which return {1, 1, 1, 0, 0, 0, 1, 1, 1, 0} using the isalpha_separator function.
The array should then be ordered {1, 2, 3, 0, a, b, c, x, y, z} at the end of the function.

array: An array of void pointers.
size: The number of void pointers in to traverse through in the array.
separator: A function pointer to the function used to separate the values pointed to by the void pointers in the array. For this lab, this will always be isalpha_separator, positiveInt_separator, or iswholeDouble_separator.

int isalpha_separator(const void *arg) This function returns 0 if the char is not alphabetic (a-z or A-Z), returns non-zero otherwise.

arg: a void pointer referencing a char

int positiveInt_separator(const void *arg) This function returns 0 if the char is not positive (less than 0), returns non-zero otherwise.

arg: a void pointer referencing an int

int iswholeDouble_separator(const void *arg); This function returns 0 if the double is not a whole number, returns non-zero otherwise.
The double is a whole number if it contains no fractions and is greater than zero.
HINT: look at ceil() and floor in math.h

arg: a void pointer referencing a double

Begin your work by creating a folder named lab06 and downloading the supplied files into it.

Please be sure that your code compiles using the supplied Makefile before submitting it.

Feel free to modify (or replace) the supplied main.c to further test your code.

Submit

Before you submit make sure to test your implementation on LORE using the Makefile provided.
Your code must compile using the provided Makefile and run on LORE for you to earn points for this lab.

Type cd .. in lab06 and change working directory to the parent directory of lab06.

In the parent directory of lab06, type turnin -v -c cs240=XXX -p lab06 lab06 to turnin your work. Replace XXX with your section number.

9:30 am - 11:20 am FF930
11:30 am - 1:20 pm FF1130
1:30 pm - 3:20 pm FF130
3:30 pm - 5:20 pm FF330
9:30 am - 11:20 am RR930
11:30 am - 1:20 pm RR1130
3:30 pm - 5:20 pm RR330
11:30 am - 1:20 pm TT1130

Now, you may use the command, turnin -c cs240=XXX -p lab06 -v to verify your submission.

This lab is due on Monday, March 29 by 11:59PM.