function PEX2()
%---------------------------------------------------------------
%       USAGE: PEX2()
%
%      AUTHOR: ??                        DATE: ??     
%
% DESCRIPTION: The program plots the rankings of baby names used
%              from 1990 to 2000 using USA census data.
%
%      INPUTS: None
%
%     OUTPUTS: None
%
%  REFERENCES: ??
%---------------------------------------------------------------

clc();

% Read data
[Baby_names Name_rankings] = Read_names_data();
if isempty(Baby_names)
  return
end

% Process user options
Quit = false;
while ~ Quit
  Option = Get_user_option();
  
  switch (Option)
    case 1  % Plot a name's rankings
      % Add code or function call here -- REMOVE THIS COMMENT
      
    case 2  % Plot a group of name rankings that all start with the same letters
      % Add code or function call here -- REMOVE THIS LINE
      
    case 3  % Clear the graph
      % Add code or function call here -- REMOVE THIS COMMENT
      
    case 4  % Calculate statistics about the names
      % Add code or function call here -- REMOVE THIS COMMENT
      
    case 5  % Display a bar chart on the changes in names from census to census
      % Add code or function call here -- REMOVE THIS COMMENT
      
    case 0
      close(gcf());
      Quit = true;
      
    otherwise % Invalid option -- ignore input
  end % switch
end % while

end % PEX2

%==========================================================================
function Option = Get_user_option()
%--------------------------------------------------------------------------
% DESCRIPTION: Display the user options and read the user response.
%
%      INPUTS: None
%
%     OUTPUTS: Option - An integer indicating the user choice.
% 
%--------------------------------------------------------------------------
  fprintf('\n');  
  fprintf('Please select from the following options:\n');
  fprintf('  1: Plot a single name''s rankings\n');
  fprintf('  2: Plot a set of similiar name rankings\n');
  fprintf('  3: Clear graph\n');
  fprintf('  4: Calculate name statistics\n');
  fprintf('  5: Display a bar chart describing the changes in names from census to census\n');
  fprintf('  0: Quit\n');
  fprintf('\n');
  Option = input('Enter command option : ');
  clc();
end % Get_user_option

%==========================================================================
function [Baby_names Name_rankings] = Read_names_data()
%--------------------------------------------------------------------------
% DESCRIPTION: Read the baby names and their rankings from the data file.
%
%      INPUTS: None
%
%     OUTPUTS: Baby_names   - a 2D array of characters where each row
%                             contains a unique baby name.
%              Name_ranking - a 2D array of integer rankings where the
%                             jth row contains the census rankings for the jth name.
%                             Name_ranking(j,1)  - 1900 census ranking
%                             Name_ranking(j,2)  - 1910 census ranking
%                             etc.
%                             Name_ranking(j,11) - 2000 census ranking
% 
%--------------------------------------------------------------------------

fid = fopen('names-data.txt', 'r');
if fid == -1
  fprintf('Error - file "name-data.txt" could not be opened.\n');
  Baby_names = [];
  Name_rankings = [];
else    
  fprintf('Reading data file ');
  
  Count = 0;
  while ~ feof(fid)
    Count = Count + 1;
    
    Name = fscanf(fid,'%s', 1);
    Baby_names(Count, 1:length(Name)) = Name;
    Name_rankings(Count,:) = fscanf(fid, '%d%d%d%d%d%d%d%d%d%d%d\n', 11);
      
    if floor(Count / 100) == Count / 100
      fprintf('.'); % Display a dot for each 100 names read
    end
  end
  
  fprintf('\n');
  fclose(fid);
end

end % Read_names_data



