function C = CensusData
% Packages the data that is in the files Names.dat, Pop.dat, and Rep.dat
% to facilitate the analysis of US census data and how it is used to
% determine the number of congressional seats for each state.

% There are 22 census dates: 1790, 1800,..., 2000.
% Let k be 50, the number of states (most recent census).
%
% C is a length 22 structure array where C(j) is a structure corresponding
% to the jth census with these fields:
% year The year of the census. (1790, 1800,...,2000).
% states k-by-16 char array that names existing states during the census.
% pop k-by-1 real array that specifies the state populations.
% reps k-by-1 real array that specifies the state apportionments.
% method apportionment method used corresponding to the jth census

nCensus= 22; % Number of census dates
nStates= 50; % Number of states

% Read in state names...
fid = fopen('Names.dat','r');
Names = [];
for k=1:nStates
Names = [Names; fgetl(fid)];
end
fclose(fid);

% Read in state populations...
fid = fopen('Pop.dat','r');
Pop = zeros(nStates,nCensus);
for i=1:nStates
str = fgetl(fid);
for j=1:nCensus
Pop(i,j) = str2double(str(10*(j-1)+1:10*j));
end
end
fclose(fid);

% Read in state apportionments...
fid = fopen('Reps.dat','r');
Reps = zeros(nStates,nCensus);
for i=1:nStates
str = fgetl(fid);
for j=1:nCensus
Reps(i,j) = str2double(str(4*(j-1)+1:4*j));
end
end
fclose(fid);

Method = {'Jefferson','Jefferson','Jefferson','Jefferson','Jefferson',...
'Webster','Hamilton','Hamilton','Hamilton','Hamilton',...
'Hamilton','Hamilton',...
'MajorFractions','MajorFractions','MajorFractions',...
'EqualProportion','EqualProportion','EqualProportion','EqualProportion',...
'EqualProportion','EqualProportion','EqualProportion'};

% Set up the structure array C...
for j=1:nCensus
% The indices of those states that existed during the year of
% the jth census...
idx = find(Pop(:,23-j)>0);
C(j) = struct('year',1790+(j-1)*10,...
'states',Names(idx,:),...
'pop',Pop(idx,23-j),...
'reps',Reps(idx,23-j),...
'method',Method{j});
end