
% Homework 1 Solutions
% speed.m
% CS 1109, Summer 2011
% July 13

% This script prompts the user to type in a randomly generated string that
% is 10 characters long. It times how long the user took to complete this
% task and prints the user's typing speed (in characters/sec) to the
% screen. It also displays how accurate the user's typing was.

% Generate a random 10 character string
randomString = genString();

fprintf('Type in the following string: %s\n', randomString);

% Time how long it takes the user to type in the displayed string
tic;
userString = input('', 's');
time = toc;

% Display results
fprintf('You typed at a rate of %.2f characters per second.\n', 10/time);
fprintf('Your typing accuracy was %d percent.\n', ...
    100 * accuracy(randomString, userString));

% Common mistakes / other observations:
%
% 1) Placing tic and toc in the incorrect place and/or using multiple tocs
% - note that we *only* want to time how long the user takes to type, not
% the rest of the computations
% 
% 2) Many people reported the seconds/character, instead of
% characters/second.
%
% 3) Observe how the long fprintf lines were split up across multiple lines
% of code to make them easier to read.
%