Massachusetts Institute of Technology
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>
#de fine MAX STUDENTS 10
st ruc t st ud en t
{
char fname [ 1 0 0 ] ;
char lname [ 1 0 0 ] ;
int year ;
int age ;
} ;
st ruc t st ud en 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
} ;
/∗
@funct i o n c o m p a r e fi r s t na m e
@desc compares f i r s t name o f two re c o r d s .
∗/
int co m p a r e fir s t n a m e ( const void ∗ a , const void∗ b )
{
st ruct st ud en t ∗ sa =( struct st ud en t ∗) a ;
st ruct st ud en t
∗ sb=( s tru ct stu de nt ∗)b ;
return strcmp ( sa
−>fname , sb−>fname ) ;
}
/∗
@funct i o n compare lname name
@desc compares l a s t name o f two re c o r d s .
∗/
int co mpar e la s t name ( const void∗ a , const void∗ b )
{
st ruct st ud en t ∗ sa =( struct st ud en t ∗) a ;
st ruct st ud en t
∗ sb=( s tru ct stu de nt ∗)b ;
return strcmp ( sa
−>lname , sb−>lname ) ;
}
/∗ !
@funct i o n apply
@desc a p p l i e s
∗/
void apply ( str uct s t u d e n t ∗ s a r r , i nt nrec , void ( ∗ fp ) ( void ∗ prec , void∗ arg ) , void∗ a r g )
{
int i =0;
for ( i =0; i <n re c ; i ++)
2
{
/∗ c a l l b a c k ∗/
fp (& s a r r [ i ] , a rg ) ;
}
}
/∗
@funct i o n p r i n t r e c
@desc p r i n t s st u d e n t re c o r d
∗/
void p r i n t r e c ( void∗ prec , void ∗ arg )
{
st ruct st ud en t ∗ pstud=( struct stud en t ∗) pre c ;
p r i n t f ( " % -20s % -20 s %2 d %2 d\n" , pstud
−>fname , pstud−>lname , pstud−>year , pstud−>age ) ;
}
/∗
@funct i o n i s o l d e r
@desc p r i n t s st u d e n t re c o r d
∗/
void i s o l d e r ( void∗ prec , void∗ arg )
{
int ∗ age=( int ∗) arg ;
st ruct st ud en t ∗
i f ( pstud−>age <
return ; /
∗ do
e l s e
p r i n t f ( " % -20s
}
int main ( )
{
pstud=( struct stu d en t ∗) pre c ;
∗ age )
not hin ∗/
% -20 s %2d %2d\n" , pstud−>fname , pstud−>lname , pstud−>year , pstud−>age ) ;
int ns t u de nt s=s i z e o f ( c l a s s )/ s i z e o f ( struct
int age ;
puts ( " Raw records :" ) ;
puts ( " ----------------- --- -----------" ) ;
apply ( cl a s s , nstud e nts , p r i n t r e c ,NULL) ;
/∗ s o r t based on f i r s t name∗/
puts ( " Sorted by first name :" ) ;
puts ( " ----------------- --- -----------" ) ;
st ud en t ) ;
q s o r t ( c l a s s , n st ud ents , s i z e o f ( struct s tu den t ) , c o m p a r e
apply ( c l a s s , n s t u dent s , p r i n t r e c ,NULL ) ;
f i r s t n a m e ) ;
/∗ s o r t based on l a s t name∗/
puts ( " Sorted by last name :" ) ;
puts ( " ---------------- --- ------------" ) ;
q s o r t ( c l a s s , n st ud ents , s i z e o f ( struct s tu den t ) , c ompa r e
apply ( c l a s s , n s t u dent s , p r i n t r e c ,NULL ) ;
last nam e ) ;
/∗ p r i n t p e o p l e o l d e r than 20 ∗/
puts ( " People older than 20: " ) ;
puts ( " ---------------- --- ------------" ) ;
age =20;
apply ( c l a s s , n s t u dent s , i s o l d e r ,& age ) ;
return 0 ;
}
3
Problem 6.2
A useful data structure for doing lookups is a hash table. In this problem, you will be imple
menting a hash table with chaining to store the frequency of words in a file. The hash table is
implemented as an array of linked lists. The hash function specifies the index of the linked list to
follow for a given word. The word can be found by following this linked list. Optionally, the word
can be appended to this linked list. You will require the code file ’hash.c’ and data file ’book.txt’
for this problem. You are required to do the following
• The function lookup() returns a pointer to the record having the required string. If not
found it returns NULL or optionally creates a new record at the correct location. Please
complete the rest of the code.
• Complete the function cleartable() to reclaim memory. Make sure each call to malloc()
is matched with a free()
4
Answer: one possible implementation is shown below:
#include <s t d i o . h>
#include <s t d l i b . h>
#include <s t r i n g . h>
#de fine MAX BUCKETS 1000
#de fine MULTIPLIER 31
#de fine MAX LEN 100
st ruc t wordrec
{
char∗ word ;
unsigned long count ;
st ruct wordrec
∗ next ;
} ;
st ruc t wordrec ∗ w a l l o c ( const char∗ s t r )
{
st ruct wordrec ∗ p=( s truct wordrec ∗) m a ll o c ( s i z e o f ( struct wordrec ) ) ;
i f ( p!=NULL)
{
p−>count =0;
p−>word=s trd u p ( s t r ) ; /∗ c r e a t e s a d u p l i c a t e ∗/
p−>next=NULL;
}
return p ;
}
/∗ hash bucket ∗/
st ruc t wordrec ∗ t a b l e [MAX LEN ] ;
/∗
@funct i o n h a s h s t r i n g
@desc p rod u ces hash code f o r a s t r i n g
m u l t i p l i e r s 31 ,35 have been found to work w e l l
∗/
unsigned long h a s h s t r i n g ( const char∗ s t r )
{
unsigned long hash =0;
while (
∗ s t r )
{
hash= hash ∗MULTIPLIER+∗ s t r ;
s t r ++;
}
return hash%MAX BUCKETS;
}
/∗
@funct i o n lookup
@desc re t u r n s a p o i n t e r to the word or c r e a t e s
i t i f r e q u i r e d
∗/
st ruc t wordrec ∗ looku p ( const char∗ s t r , int c r e a t e )
{
unsigned long hash=h a s h s t r i n g ( s t r ) ;
st ruct wordrec
∗ wp=t a b l e [ hash ] ;
5