As a second example of file modularization, consider the stable marriage program of Section 2.5.2. That program is structured with the data abstractions pairings, with its operations mate, read_mens_mates, and create_womens_mates placed together, and preferences, with its operations mostpreferred, nextpreferred, prefers, read_preferences, and create_priorities also placed together. Grouping each data abstraction facilitates finding its data structures, function declarations, and definitions, whenever modifications to them are required. Such changes will not necessitate changing any other part of the program, since it was written treating pairings and preferences as data abstractions. Also, changes to one data abstraction do not require changes in the other. The program in Section 2.5.2 is written as a single source file. We now place the two data abstractions in modules. The modules and the program file structure of the program using file modularization are printed below. The headers, operations and program file are, respectively, pairings.h, pairings.c, preferences.h, preferences.c, and stablemarriages.c.
The File pairings.h
/* header for pairings */
#define MAXN 50
typedef int mates[MAXN];
extern mate(/* int, pairings */);
/* Returns the mate of person int */
extern read_mens_mates(* int, pairings */);
/* Reads the mates of the men(int of them)
into pairings
*/
extern create_womens_mates(/* int,pairings_1,pairings_2 */);
/* Creates the pairings_2 of the
men(int of them), based on
the mens' mates specified by
pairings_1
*/
The File pairings.c
/* operations allowed on pairings */
#include "pairings.h"
mate (person,pairs)
/* Returns persons mate
as specified by pairs */
int person;
pairings pairs;
{
return(pairs[person]);
}
read_mens_mates(n,pairs)
/* Reads the n wives into pairs */
int n;
pairings pairs;
{
int i;
printf("\n mpairs = ? \n");
for (i=1;i <=n;i++)
scanf("%d",&pairs[i]);
}
create_womens_mates(n,pairs1,pairs2)
/* Pairs 1 specifies the n wives, one
for each man. Pairs2 is set to
specify the n men, one for each woman.
*/
int n;
pairings pair;
{
int i,j;
for (i=1;i<=n;i++)
{
for (j=1;pairs1[j] != i;j++)
;
pairs2 [i] = j;
}
}
The File preferences.h
/* header file for preferences */
#define MAXN 50
typedef int preferences[MAXN][MAXN];
extern mostpreferred(/* int,preferences */);
/* Returns the mate most preferred by
person int as specified by pref */
extern nextpreferred(/* int_1,int_2,pref */);
/* Returns the mate preferred most, after
the mate given by index int_2, by person
int_1 as specified by pref
*/
extern prefers(/* int_1,int_2,int_3,pref */);
/* Returns true if individual int_1 prefers
person int_2 to individualsmate int_3
as specified by pref
*/
extern read_preferences(/* int,pref */);
/* Reads int number of rows of
preferences into pref
*/
extern create_priorities(/* int,pref_1,pref_2 */);
/* Creates int number of individual
priorities in pref_2 based on the
individual preferences specified
by pref_1
*/
The File preferences.c
/* operations allowed on preferences */
#include "preferences.h"
mostpreferred (person,pref)
/* Returns the mate most preferred by
person as specified by pref */
int person;
preferences pref;
{
return(pref[person][1]);
}
nextpreferred(person,index,pref)
/* Returns the mate preferred most, after
the mate given by index, by person
as specified by pref
*/
int person,index;
preferences pref;
{
return(pref[person][index]);
}
prefers(individual,person,individualsmate,priority)
/* Returns true if individual prefers person to
individuals mate as specified by priority
*/
int individual,person,individualsmate;
preferences priority;
{
return(priority[individual][person] <
priority [individual] [individualsmate]);
}
read_preferences (n,pref)
/* Reads n rows of
preferences into pref
*/
int n;
preferences pref;
{
int i,j;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf ("%d",&pref[i][j]);
}
create_priorities(n, pref, priority)
/* Creates n individual priorities
in priority based on the
individual preferences specified
by pref
*/
int n;
preferences pref,priority;
{
int i,j;
for (i=1,i<=n;i++)
for (j=1;j<=n;j++)
priority[i][pref[i][j]] = j;
}
The File stablemarriages.c
#include <stdio.h>
#include "pairings.h"
#include "preferences.h"
#define TRUE 1
#define FALSE 0
main()
{
pairings mpairs,wpairs;
preferences mpref,wpref,mpriority,wpriority;
int i,j,n;
printf("\n n = ?\n");
scanf("%d",&n);
read_mens_mates(n,mpairs);
create_womens_mates(n,mpairs,wpairs);
printf("\n Enter mens preferences \n");
read_preferences(n,mpref);
printf("\n Enter womens preferences \n");
read_preferences(n,wpref);
create_priorities(n,mpref,mpriority);
create_priorities(n,wpref,wpriority);
if (stabilitycheck(n,mpairs,wpairs,mpref,wpref,mpriority,
wpriority))
printf(" The pairings are stable \n");
else
printf(" The pairings are not stable \n");
printf(" The pairings are: \n");
printf(" man wife \n");
for (i=1;i<=n;i++)
printf("%d %d \n",i,mate (i,mpairs));
}
stabilitycheck(n,mpairs,wpairs,mpref,wpref,mpriority,wpriority)
int n;
pairings mpairs,wpairs
preferences mpref,wpref,mpriority,wpriority;
{
int man,wife,individual,individualsmate,index,stable;
stable = TRUE;
for(man=1;(stable && man<=n);man++)
{
wife = mate(man,mpairs);
individual = mostpreferred(man,mpref);
index = 1;
while (stable && (individual != wife))
{
individualsmate = mate(individual, wpairs);
if(prefers(individual,man,individualsmate,
wpriority))
stable = FALSE;
else
{
index++;
individual = nextpreferred(man,index,mpref);
}
}
individual = mostpreferred(wife,wpref);
index = 1;
while(stable && (individual != man))
{
individualsmate = mate(individual,mpairs);
if(prefers(individual,wife,individualsmate,
mpriority))
stable = FALSE;
else
{
index++;
individual = nextpreferred(wife,index,wpref);
}
}
}
return(stable);
}
The include "pairings.h" and include "preferences.h" were put at the top of the program file because their contents are needed from that point in the file. In the program file for primestest.c, the include "collection.h" appears just before the primes function, since its contents are only needed from that point on in the file.
The files pairings.c, preferences.c, and stablemarriages.c can be compiled separately. To run stable marriages.c, they must first all be linked by the linker.
Any time changes are made in pairings.h, the files pairings.c and stablemarriages.c must be recompiled. Changes in pairings.c also require that pairings.c be recompiled . The same applies for preferences.h and preferences.c. However, changes to only one of pairings.c, preferences.c, or stablemarriages.c require only its individual recompilation. Relinking is always required before execution.