Department of Electrical Engineering and Computer Science
6.087: Practical Programming in C
IAP 2010
Problem Set 6 – Solutions
Part 2: Function pointers, hash table
Out: Thursday, January 21, 2010. Due: Friday, January 22, 2010.
Problem 6.1
In this problem, we will use and create function that utilize function pointers. The file ’call
back.c’ contains an array of records consisting of a fictitious class of celebrities. Each record consists
of the firstname, lastname and age of the student. Write code to do the following:
• Sort the records based on first name. To achieve this, you will be using the qsort() function
provided by the standard library: void qsort(void∗ arr,int num,int size,int (∗fp)(void∗ pa,void∗pb)).
The function takes a pointer to the start of the array ’arr’, the number of elements ’num’ and
size of each element. In addition it takes a function pointer ’fp’ that takes two arguments.
The function fp is used to compare two elements within the array. Similar to strcmp(), it
is required to return a negative quantity,zero or a positive quantity dependeing on whether
element pointed to by ’pa’ is ”less” than, equal to or ”greater” the element pointed to by
’pb’. You are required to write the appropriate callback function.
• Now sort the records based on last name. Write the appropriate callback function.
• The function void apply (...) iterates through the elements of the array calling a function for
each element of the array. Write a function isolder() that prints the record if the age of
the student is greater 20 and does nothing otherwise.
1
, Answer: Here’s one possible implementation:
#include <s t d i o . h>
#include <s t r i n g . h>
#include < s t d l i b . h>
#define MAX STUDENTS 10
struct s t u d e n t
{
char fname [ 1 0 0 ] ;
char lname [ 1 0 0 ] ;
int year ;
i n t age ;
};
struct s t u d e n t c l a s s [ ] = {
" Sean " , " Penn " , 2 , 2 1 ,
" Sean " , " Connery " , 4 , 2 5 ,
" Angelina " , " Jolie " , 3 , 2 2 ,
" Meryl " , " Streep " , 4 , 2 9 ,
" Robin " , " Williams " , 3 , 3 2 ,
" Bill " , " Gates " , 3 , 1 7 ,
" Jodie " , " Foster " , 4 , 2 5 ,
" John " , " Travolta " , 1 , 1 7 ,
" Isaac " , " Newton " , 2 , 1 9 ,
" Sarah " , " Palin " , 2 , 1 9
};
/∗
@function compare first name
@desc compares f i r s t name o f two r e c o r d s .
∗/
i n t c o m p a r e f i r s t n a m e ( const void ∗ a , const void ∗ b )
{
struct s t u d e n t ∗ s a =( struct s t u d e n t ∗ ) a ;
struct s t u d e n t ∗ sb=( struct s t u d e n t ∗ ) b ;
return strcmp ( sa−>fname , sb−>fname ) ;
}
/∗
@ f u n c t i o n compare lname name
@desc compares l a s t name o f two r e c o r d s .
∗/
i n t c o m p a r e l a s t n a m e ( const void ∗ a , const void ∗ b )
{
struct s t u d e n t ∗ s a =( struct s t u d e n t ∗ ) a ;
struct s t u d e n t ∗ sb=( struct s t u d e n t ∗ ) b ;
return strcmp ( sa−>lname , sb−>lname ) ;
}
/∗ !
@ f u n c t i o n app l y
@desc applies
∗/
void a p p l y ( struct s t u d e n t ∗ s a r r , i n t nrec , void ( ∗ f p ) ( void ∗ prec , void ∗ a r g ) , void ∗ a r g )
{
i n t i =0;
f o r ( i =0; i <n r e c ; i ++)
2