function hb_exact_write ( output_unit, nrow, nrhs, rhscrd, rhsfmt, rhstyp, ...
  exact )

%*****************************************************************************80
%
%% HB_EXACT_WRITE writes the exact solution vectors to an HB file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 November 2003
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Iain Duff, Roger Grimes, John Lewis,
%    User's Guide for the Harwell-Boeing Sparse Matrix Collection,
%    October 1992.
%
%  Input:
%
%    integer OUTPUT_UNIT, the unit to which data is written.
%
%    integer NROW, the number of rows or variables.
%
%    integer NRHS, the number of right hand sides.
%
%    integer RHSCRD, the number of lines in the file for
%    right hand sides.
%
%    character ( len = 20 ) RHSFMT, the format for reading values
%    of the right hand side.
%
%    character ( len = 3 ) RHSTYP, the right hand side type.
%    First character is F for full storage or M for same as matrix.
%    Second character is G if starting "guess" vectors are supplied.
%    Third character is X if exact solution vectors are supplied.
%    Ignored if NRHS = 0.
%
%    real EXACT(NROW,NRHS), the exact solution vectors.
%
  if ( 0 < rhscrd )

    if ( rhstyp(3:3) == 'X' )
%
%  From the FORTRAN format string RHSFMT, determine the number of
%  values per line, and their width.
%
      line = rhsfmt(2:length(rhsfmt));
      [ rhs_nperline, count, errmsg, nextindex ] = sscanf ( line, '%d' );
      line = line(nextindex+1:length(line));
      [ rhs_len, count, errmsg, nextindex ] = sscanf ( line, '%d' );
      s = int2str ( rhs_len );
      rhs_format = [ '%' s 'f' ];

      k = 0;
      for j = 1 : nrhs
        for i = 1 : nrow
          fprintf ( output_unit, rhs_format, exact(i,j) );
          k = k + 1;
          if ( mod ( k, rhs_nperline ) == 0 ) 
            fprintf ( output_unit, '\n' );
          end
        end
      end

      if ( mod ( nrow * nrhs, rhs_nperline ) ~= 0 )
        fprintf ( output_unit, '\n' );
      end

    end

  end

  return
end
