Starting from:
$29.99

$23.99

Homework # 2 Solution

In this homework you will learn and use function pointers. If you did not attend the PS about function pointers, you may find good information about function pointers from the web.

 

First read and understand standard C functions such as lsearch, qsort, and lfind. The page at http://www.cplusplus.com/reference/clibrary/cstdlib/bsearch/ is a good source of information for these types of functions.

 

Then write a global C function that returns the array index of the first mode element of a given array. The prototype for the function should be like

 

int return_mode (const void * base, size_t num, size_t size,

bool (* equals) (const void *, const void *));

 

 

The returned value should be array index of the mode element of the array. The array mode is the element that occurs most frequently in the array. For example, if the array is {1,3,3,1,4,5,3,1,7,3} then the mode is 3 because it occurs 4 times in the array. Your function will return 1 for the above array because the first occurrence of 3 is at index 1.

 

Here are the rules for the function

 

•      It should work for any array data type such as int, double, char, DayOfYear, Person. There might be many more array types, do not limit your function for the above types.

•    The returned address should be the first element that is equal to the tmode

•    It should not modify the array

•    It should not use any other standard C functions

 

Test your function at least 6 times with arrays of int, double, Person, and DayOfYear. Write and implement any classes required for your testing. You will also need equals functions for each type.

 

Notes:

 

        You should submit your work to the moodle page.

More products