function brc_naive ( filename )

%******************************************************************************80
%
%% brc_naive() is a naive solution of the billion record challenge.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    20 October 2024
%
%  Author:
%
%    Original C version by Danny van Kooten.
%    This version by John Burkardt.
%

%
%  Read the data file.
%
  fprintf ( 1, "  Reading file '%s'\n", filename );
  data = readtable ( filename );
  city_list = data{:,1};
  temp_list = data{:,2};
  record_num = length ( temp_list );
%
%  Process each record.
%
  unique_num = 0;
  unique_city = {};

  for record = 1 : record_num

    city = city_list{record};
    temp = temp_list(record);

    c = city_match ( city, unique_city, unique_num );

    if ( c < 0 )
      unique_num = unique_num + 1;
      c = unique_num;
      unique_city{c} = city;
      unique_sum(c) = temp;
      unique_max(c) = temp;
      unique_min(c) = temp;
      unique_count(c) = 1;
    else
      unique_sum(c) = unique_sum(c) + temp;
      unique_max(c) = max ( unique_sum(c), temp );
      unique_min(c) = min ( unique_sum(c), temp );
      unique_count(c) = unique_count(c) + 1;
    end
  end
%
%  Write results to a file.
%
  filename = 'results.txt';
  output = fopen ( filename, 'wt' );
  for c = 1 :unique_num
    fprintf ( output, '%s, %g, %g, %g\n', ...
      unique_city{c}, ...
      unique_min(c), ...
      unique_sum(c) / unique_count(c), ...
      unique_max(c) );
  end
  fclose ( output );

  fprintf ( 1, "  Unsorted results written to '%s'\n", filename );

  return
end
function c = city_match ( city, unique_city, unique_num ) 

%*****************************************************************************80
%
%% city_match() returns the index of a given city in the list of cities.
%
%  Licensing:
%
%   This code is distributed under the MIT license. 
%
%  Modified:
%
%    20 October 2024
%
%  Author:
%
%    Original C version by Danny van Kooten.
%    This version by John Burkardt.
%
%  Input:
%
%    string city: the city to be indexed.
%
%    string unique_city[ncities]: the list of cities.
%
%    integer unique_num: the number of cities;
%
%  Output:
%
%    integer c: the index of the city, or -1 if it is not in the list.
%
  c = -1;

  for i = 1 : unique_num
    if ( strcmp ( city, unique_city{i} ) )
      c = i;
      return
    end
  end

  return
end

