Use function pointers to create a polymorphic function that can operate on char
,
int
, and double
datatypes, separating them based upon different
characteristics.
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.
array: An array of void pointers.
|
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 |
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 |
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 |
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.
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 F | F930 |
11:30 am - 1:20 pm F | F1130 |
1:30 pm - 3:20 pm F | F130 |
3:30 pm - 5:20 pm F | F330 |
9:30 am - 11:20 am R | R930 |
11:30 am - 1:20 pm R | R1130 |
3:30 pm - 5:20 pm R | R330 |
11:30 am - 1:20 pm T | T1130 |
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.